• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 280
  • Last Modified:

Streamline macro

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
joeserrone
Asked:
joeserrone
  • 2
  • 2
1 Solution
 
Rgonzo1971Commented:
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
 
mvidasCommented:
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
 
joeserroneAuthor Commented:
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
 
mvidasCommented:
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
 
joeserroneAuthor Commented:
Perfect~!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now