Dynamic AutoFill

datauser

New Member
Joined
Sep 4, 2024
Messages
15
Office Version
  1. 365
Platform
  1. Windows
Hello!

I need some help figuring out what I am missing here. I am trying to autofill a selection down the rows where the starting point of my autofill is dynamic and the ending point of my autofill is also dynamic.

Dim lastRowB As Long

lastRowB = ws.Cells(ws.Rows.Count, 2).End(xlUp).Row

ws.Cells(lastRowB + 4, 2).Formula2 = "=UNIQUE(TOCOL(Query2!C:C,1))"
ws.Cells(lastRowB + 4, 3).Formula2 = "=TRANSPOSE( UNIQUE(Query2[ID]))"

WDate = ws.Cells(lastRowB + 5, 2).Address(False, True) This is the "Week Beginning Dates"
PB = ws.Cells(lastRowB + 4, 3).Address(True, False) This is the "25530"

'ws.Cells(lastRowB + 5, 3).Formula2 = "= " & PB & ""
ws.Cells(lastRowB + 5, 3).Formula2 = "=SUMIFS(Query2[Total],Query2[ID]," & PB & ",Query2[''Dates'']," & WDate & ")"
StartCell = ws.Cells(lastRowB + 5, 3).Address
StartCell.AutoFill Range("" & StartCell & " :C" & Range("B" & Rows.Count).End(xlUp).Row)

1730438752351.png

Any Help is appreciated!
 

Attachments

  • 1730438729541.png
    1730438729541.png
    23.1 KB · Views: 3

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
It's not clear how your data is organized (sheet Query2 with table Query2 in it? amd one of columns called 'Dates', etc., what is ws sheet, etc.

But the clearly visible problem for me is using string, not range for autofill.
try for 2 last lines of code:

VBA Code:
Set StartCell = ws.Cells(lastRowB + 5, 3)
StartCell.AutoFill Range("" & StartCell.Address & " :C" & Range("B" & Rows.Count).End(xlUp).Row)

if you have above a definition of StartCell, change it to range type:
VBA Code:
dim StartCell as range
 
Upvote 0
Just a different way of writing it without .Address (please note the ws in red)

Rich (BB code):
Set StartCell = ws.Cells(lastRowB + 5, 3)
StartCell.AutoFill Range(StartCell, ws.Range("C" & ws.Range("B" & Rows.Count).End(xlUp).Row))

StartCell should be declared as Range and ws as WorkSheet
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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