VBA Copy Formula

chaddres

Board Regular
Joined
Jun 14, 2014
Messages
143
Office Version
  1. 365
Platform
  1. Windows
I am trying to copy the formula (on line 4 of the code) down to the bottom of the range of data. The data starts in "lastcol3". My normal entry (for column B) would be "Range(B3:B & Range("A" & Rows.Count).End(xlUp).Row).FillDown" so I tried to use lastcol instead of the "B". Any help is greatly appreciated.


VBA Code:
Dim lastcol As Long
lastcol = Cells(ActiveCell.Row, Columns.Count).End(xlToLeft).Column
Cells(ActiveCell.Row, lastcol + 1).Select
ActiveCell.Value = "=COUNT(RC[" & (1 - lastcol) & "]:RC[-1])"
Range(lastcol & "3:" & lastcol & Range("A" & Rows.Count).End(xlUp).Row).FillDown
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Since lastcol is a number, you can't use that syntax. I'd suggest:

VBA Code:
Range(Cells(3, lastcol), Cells(Range("A" & Rows.Count).End(xlUp).Row, lastcol)).FillDown
 
Upvote 0
What about?

VBA Code:
With ActiveCell
  Cells(.Row, Columns.Count).End(xlToLeft).Offset(, 1).Resize(Range("A" & Rows.Count).End(xlUp).Row - .Row + 1).FormulaR1C1 = "=COUNT(RC2:RC[-1])"
End With
 
Upvote 0
Thank you so much. It did not work. Attaching the full code because I am very stuck Any help is appreciated. The attached picture shows my pivot table and the current formula (CD3). I need to copy the CD3 formula down for every row in the Pivot Table. The formula is a count of each row in the table from B to the last column of the table.

VBA Code:
'Multiple Rate Check
    Sheets("Combined Time").Select
    Dim rng2 As Range
    Dim sht2 As Worksheet
    Dim pTable2 As PivotTable
    Set rng2 = ActiveSheet.Cells(1, 1).CurrentRegion
    Set sht2 = ActiveWorkbook.Worksheets.Add
    sht2.Name = "Rate Check Pivot"
    Set pTable2 = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        rng2.Address, Version:=8).CreatePivotTable(TableDestination:= _
        sht2.Cells(1, 1), TableName:="PivotTable" & Format(Time, "hhmmss"))
    With pTable2
        With .PivotFields("SSN")
            .Orientation = xlRowField
            .Subtotals(1) = False
        End With
        With .PivotFields("Temp Rate")
            .Orientation = xlColumnField
            .Subtotals(1) = False
        End With
        .PivotFields("Reg Hours").Orientation = xlDataField
    End With
    Range("B3").Select
    Dim lastcol As Long
    lastcol = Cells(ActiveCell.Row, Columns.Count).End(xlToLeft).Column
    Cells(ActiveCell.Row, lastcol + 1).Select
    ActiveCell.Value = "=COUNT(RC[" & (1 - lastcol) & "]:RC[-1])"
    Range(Cells(3, lastcol), Cells(Range("A" & Rows.Count).End(xlUp).Row, lastcol)).FillDown
 

Attachments

  • Screenshot 2022-09-23 055420.png
    Screenshot 2022-09-23 055420.png
    37.7 KB · Views: 3
Upvote 0
Why are you putting a formula in lastcol + 1 column, but then trying to fill down the column to the left?
 
Upvote 0
So much closer!!!! The formula appeared in the column to the right of where I wanted it (not a big deal), but it is also including the "Grand Total" column. Any way around either of those? If not, I can make it work.
 

Attachments

  • Screenshot 2022-09-23 060330.png
    Screenshot 2022-09-23 060330.png
    12.6 KB · Views: 1
Upvote 0
Why are you putting a formula in lastcol + 1 column, but then trying to fill down the column to the left?
I am trying to just fill down below the formula :( .I am very stuck.
 
Upvote 0
Then you need to adjust that in the filldown line:

Code:
Range(Cells(3, lastcol + 1), Cells(Range("A" & Rows.Count).End(xlUp).Row - 1, lastcol + 1)).FillDown
 
Upvote 0
Solution

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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