VBA: Creating formula on all worksheets dynamic ranges

smilie224

New Member
Joined
Nov 18, 2014
Messages
19
Hi :) I am guessing there is an easy solution to this, but I have spent hours attempting to locate with no success. My apologies for the code potentially being klunky, as I piece together code over various searches based on my needs. This code is a piece of a much larger macro which is why it is not self-contained with the sub()

The problem: I am trying to create a formula based on a dynamic range (referring to column to left's amount of rows and autofill the formula down. I need this to run over multiple sheets, and the column "AD" I am referring to will change on the amount of rows on each sheet.

This code works fine for the first sheet, but I can't get it to loop through the other worksheets (worksheet names will also change in both name and quantity).

From what I have read, I think I need to "set" the range somehow, but I can't seem to do it. Any ideas? I'll paste my current code below:
Code:
 For Each WSD in Worksheets 'Note - WSD is dim'd earlier
'Adding the percentage on all sheets

    'Creating the percentage
    Range("AE3").Select
    'Formatting the word percentage
        With Selection.Font
        .Name = "Calibri (Theme Body)"
        .FontStyle = "Bold"
        .Size = 12
        End With
                
    ActiveCell.FormulaR1C1 = "Percentage:"
    'Choosing the last row available based on adjacent left column
       Dim LRAD As Long
    LRAD = Range("AD" & Rows.Count).End(xlUp).Row

    Range("AE4").Select
      
    ActiveCell.FormulaR1C1 = "=(RC[-1])/(RC[-2])"
   Range("AE4").AutoFill Destination:=Range("AE4:AE" & LRAD)
   Range("AE4:AE" & LRAD).Copy
       ActiveSheet.Range("AE4:AE" & LRAD).PasteSpecial xlPasteValues
    Application.CutCopyMode = False
    
    With Selection
        .HorizontalAlignment = xlRight
        .NumberFormat = "0.0%"
    End With
       With Selection.Font
        .Name = "Calibri (Theme Body)"
        .FontStyle = "Bold"
        .Size = 12
        End With
    Next
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
I found it!!! I had done the same thing later in my code. Running the test it just worked. Re-posting here (the new code) in case anyone else spends hours researching for the same misery :)
Code:
Range("A1").Select

For Each WSD In Worksheets
WSD.Activate    '<--- TWO SIMPLE LITTLE WORDS!!  :)
'Adding the percentage on all sheets
 
    'Creating the percentage
    Range("AE3").Select
    'Formatting the word percentage
        With Selection.Font
        .Name = "Calibri (Theme Body)"
        .FontStyle = "Bold"
        .Size = 12
        End With
                
    ActiveCell.FormulaR1C1 = "Percentage:"
    'Choosing the last row available based on adjacent left column
       Dim LRAD As Long
    LRAD = Range("AD" & Rows.Count).End(xlUp).Row

    Range("AE4").Select
      
    ActiveCell.FormulaR1C1 = "=(RC[-1])/(RC[-2])"
   Range("AE4").AutoFill Destination:=Range("AE4:AE" & LRAD)
   Range("AE4:AE" & LRAD).Copy
       ActiveSheet.Range("AE4:AE" & LRAD).PasteSpecial xlPasteValues
    Application.CutCopyMode = False
    
    With Selection
        .HorizontalAlignment = xlRight
        .NumberFormat = "0.0%"
    End With
       With Selection.Font
        .Name = "Calibri (Theme Body)"
        .FontStyle = "Bold"
        .Size = 12
        End With
    Next
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,635
Messages
6,173,479
Members
452,516
Latest member
archcalx

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top