Solved

Streamline macro

Posted on 2013-10-25
5
237 Views
Last Modified: 2013-10-25
I have recorded this macro but I was hoping I could streamline this by only having it run for the amount of records loaded. So if I am loading 3 lines it will run for only those 3 lines instead for the full 2046 lines I set it for...

As you see from the macro it runs for 12 times which rapresent months and each there is one cell that is absolute while the other changes.

Public Sub Forecast_Actual_Calc()
'---------------------------------------------------------------------------------------
' Procedure : Calculate Percentage allocation
' Purpose   : Calculate Percentage Allocation based on Hours imput
'---------------------------------------------------------------------------------------
    Range("J4").Select
    ActiveCell.FormulaR1C1 = "=RC[-2]/R2C10"
    Range("J4").Select
    Selection.AutoFill Destination:=Range("J4:J2046"), Type:=xlFillDefault
    Range("J4:J2046").Select
    Range("N4").Select
    ActiveCell.FormulaR1C1 = "=RC[-2]/R2C14"
    Range("N4").Select
    Selection.AutoFill Destination:=Range("N4:N2046"), Type:=xlFillDefault
    Range("N4:N2046").Select
    ActiveWindow.SmallScroll ToRight:=7
    Range("R4").Select
    ActiveCell.FormulaR1C1 = "=RC[-2]/R2C18"
    Range("R4").Select
    Selection.AutoFill Destination:=Range("R4:R2046"), Type:=xlFillDefault
    Range("R4:R2046").Select
    Range("V4").Select
    ActiveCell.FormulaR1C1 = "=RC[-2]/R2C22"
    Range("V4").Select
    Selection.AutoFill Destination:=Range("V4:V2046"), Type:=xlFillDefault
    Range("V4:V2046").Select
    Range("Z4").Select
    ActiveCell.FormulaR1C1 = "=RC[-2]/R2C26"
    Range("Z4").Select
    Selection.AutoFill Destination:=Range("Z4:Z2046"), Type:=xlFillDefault
    Range("Z4:Z2046").Select
    ActiveWindow.ScrollColumn = 13
    ActiveWindow.ScrollColumn = 14
    ActiveWindow.ScrollColumn = 15
    ActiveWindow.ScrollColumn = 16
    ActiveWindow.ScrollColumn = 17
    ActiveWindow.ScrollColumn = 18
    ActiveWindow.ScrollColumn = 19
    ActiveWindow.ScrollColumn = 20
    ActiveWindow.ScrollColumn = 21
    ActiveWindow.ScrollColumn = 22
    ActiveWindow.ScrollColumn = 23
    ActiveWindow.ScrollColumn = 24
    ActiveWindow.ScrollColumn = 25
    ActiveWindow.ScrollColumn = 26
    ActiveWindow.ScrollColumn = 27
    Range("AD4").Select
    ActiveCell.FormulaR1C1 = "=RC[-2]/R2C30"
    Range("AD5").Select
    ActiveWindow.ScrollColumn = 26
    ActiveWindow.ScrollColumn = 25
    Range("AD4").Select
    Selection.AutoFill Destination:=Range("AD4:AD2046"), Type:=xlFillDefault
    Range("AD4:AD2046").Select
    Range("AH4").Select
    ActiveCell.FormulaR1C1 = "=RC[-2]/R2C34"
    Range("AH4").Select
    Selection.AutoFill Destination:=Range("AH4:AH2046"), Type:=xlFillDefault
    Range("AH4:AH2046").Select
    Range("AL4").Select
    ActiveCell.FormulaR1C1 = "=RC[-2]/R2C38"
    Range("AL4").Select
    Selection.AutoFill Destination:=Range("AL4:AL2046"), Type:=xlFillDefault
    Range("AL4:AL2046").Select
    ActiveWindow.ScrollColumn = 26
    ActiveWindow.ScrollColumn = 27
    ActiveWindow.ScrollColumn = 28
    ActiveWindow.ScrollColumn = 29
    ActiveWindow.ScrollColumn = 30
    ActiveWindow.ScrollColumn = 31
    ActiveWindow.ScrollColumn = 32
    ActiveWindow.ScrollColumn = 33
    ActiveWindow.ScrollColumn = 34
    ActiveWindow.ScrollColumn = 35
    ActiveWindow.ScrollColumn = 36
    ActiveWindow.ScrollColumn = 37
    Range("AP4").Select
    ActiveCell.FormulaR1C1 = "=RC[-2]/R2C42"
    Range("AP4").Select
    Selection.AutoFill Destination:=Range("AP4:AP2046"), Type:=xlFillDefault
    Range("AP4:AP2046").Select
    Range("AT4").Select
    ActiveCell.FormulaR1C1 = "=RC[-2]/R2C46"
    Range("AT4").Select
    Selection.AutoFill Destination:=Range("AT4:AT2046"), Type:=xlFillDefault
    Range("AT4:AT2046").Select
    Range("AX4").Select
    ActiveCell.FormulaR1C1 = "=RC[-2]/R2C50"
    Range("AX4").Select
    Selection.AutoFill Destination:=Range("AX4:AX2046"), Type:=xlFillDefault
    Range("AX4:AX2046").Select
    ActiveWindow.ScrollColumn = 38
    ActiveWindow.ScrollColumn = 39
    ActiveWindow.ScrollColumn = 40
    ActiveWindow.ScrollColumn = 42
    ActiveWindow.ScrollColumn = 43
    ActiveWindow.ScrollColumn = 44
    ActiveWindow.ScrollColumn = 45
    ActiveWindow.ScrollColumn = 46
    ActiveWindow.ScrollColumn = 47
    ActiveWindow.ScrollColumn = 48
    Range("BB4").Select
    ActiveCell.FormulaR1C1 = "=RC[-2]/R2C54"
    Range("BB4").Select
    Selection.AutoFill Destination:=Range("BB4:BB2046"), Type:=xlFillDefault
    Range("BB4:BB2046").Select

Open in new window

0
Comment
Question by:joeserrone
  • 2
  • 2
5 Comments
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 39600533
Hi,

this is a simplified code
Public Sub Forecast_Actual_Calc()
'---------------------------------------------------------------------------------------
' Procedure : Calculate Percentage allocation
' Purpose   : Calculate Percentage Allocation based on Hours imput
'---------------------------------------------------------------------------------------
       Union(Range("J4:J2046"), Range("N4:N2046"), Range("R4:R2046"), _
        Range("V4:V2046"), Range("Z4:Z2046"), Range("AD4:AD2046"), _
        Range("AH4:AH2046"), Range("AL4:AL2046"), Range("AP4:AP2046"), _
        Range("AT4:AT2046"), Range("AX4:AX2046"), Range("BB4:BB2046")) _
        .FormulaR1C1 = "=RC[-2]/R2C"
End Sub

Open in new window

REgards
0
 
LVL 35

Expert Comment

by:mvidas
ID: 39600565
Hi Joe,

Try the following:
 Dim vRow As Long
 vRow = ActiveSheet.UsedRange.Row + ActiveSheet.UsedRange.Rows.Count - 1
 Intersect(Rows("4:" & vRow), Range("J:J,N:N,R:R,V:V,Z:Z,AD:AD,AH:AH,AL:AL,AP:AP,AT:AT,AX:AX,BB:BB")).FormulaR1C1 = "=RC[-2]/R2C"

Open in new window

Should do what you want.

Matt
0
 

Author Comment

by:joeserrone
ID: 39600808
Hi Matt,
Yes this does exactly what I needed, one question... Would it be possible for the macro to only modify the rows that have data, so in my example I copied 3 lines of data in my spreadsheet, yet the code runs all the way down in each column.

Is their a way to restrict this based on how many lines of data we add?
0
 
LVL 35

Accepted Solution

by:
mvidas earned 500 total points
ID: 39600852
Yes. I thought it was using just the data you added. Change line 2 to:
vRow = Cells(Rows.Count, 10).End(xlUp).Row

Open in new window

And that will use column J as your guide. So if you have data in J to J6, it will use put that formula in rows 4:6.
0
 

Author Closing Comment

by:joeserrone
ID: 39600887
Perfect~!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now