Run formula in VBA until last row

evenyougreg

New Member
Joined
Oct 1, 2020
Messages
26
Office Version
  1. 365
Platform
  1. Windows
I'm trying to get this formula to run from D2 until the last detected row, and without using hard coded numbers like D2 until D1243 or whatever because the dataset changes all the time. I've had issues in the past with this on another forum so I will try my luck here.

the formula is

Excel Formula:
=XLOOKUP(INDIRECT("A2:A" & COUNTA(A:A)),VMs!A:A,VMs!B:B,"Not Found")

and the VBA code I have only populates D2, but would like it to run until the last row...

VBA Code:
Range("D2").Formula = "=XLOOKUP(INDIRECT(""A2:A"" & COUNTA(A:A)),VMs!A:A,VMs!B:B,""Not Found"")"

Any thoughts?:unsure:o_O
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Maybe ?
VBA Code:
For i = 1 To UsedRange.Rows.Count
Range("D" & i).Formula = ...
Next
 
Upvote 0
With this being an XLookup function, it should spill into all rows with data and you shouldn't need to copy into all the rows. When VBA writes the formula into D2, does it add any @ symbols anywhere in the formula?
 
Upvote 0
You can just use
VBA Code:
Range("D2").Formula2 = "=XLOOKUP(INDIRECT(""A2:A"" & COUNTA(A:A)),VMs!A:A,VMs!B:B,""Not Found"")"
 
Upvote 0
Solution
With this being an XLookup function, it should spill into all rows with data and you shouldn't need to copy into all the rows. When VBA writes the formula into D2, does it add any @ symbols anywhere in the formula?

Yes, outside of VBA and just running it in D2 it spills down just like you said. If I use the VBA code I have it looks like this afterwards:

Excel Formula:
=XLOOKUP(@INDIRECT("A2:A" & COUNTA(A:A)),VMs!A:A,VMs!B:B,"Not Found")
 
Upvote 0
Try what @Fluff suggested. If that doesn't work for some reason, you can also do a replace for the @ if there are any.

VBA Code:
    Sheets("Presc_By_Prov").Range("D:D").Select
    Cells.Replace What:="@", Replacement:="", LookAt:=xlPart, SearchOrder:= _
        xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False, _
        FormulaVersion:=xlReplaceFormula2
 
Upvote 0
No need to replace the @ sign, using formula2 rather than formula will do the trick.
 
Upvote 0
Maybe ?
VBA Code:
For i = 1 To UsedRange.Rows.Count
Range("D" & i).Formula = ...
Next

Thanks for your assistance, this is what I got:

Runtime error 424, Object required.

VBA Code:
Sub test()

    For i = 1 To UsedRange.Rows.Count
    Range("D" & i).Formula = "=XLOOKUP(INDIRECT(""A2:A"" & COUNTA(A:A)),VMs!A:A,VMs!B:B,""Not Found"")"
    Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,671
Messages
6,173,732
Members
452,529
Latest member
jpaxonreyes

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