Solved

Streamline macro

Posted on 2013-10-25
5
252 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 50

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

856 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