AutoFill VBA on last row

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
733
Office Version
  1. 2021
  2. 2019
  3. 2016
  4. 2010
  5. 2007
Platform
  1. Windows
Hello - Having an issue modifying the below VBA to work when NOT on the sheet. When attempting to remove activate/select usage I cant seem to get the below to work when not on the sheet. is someone able to help? The problem is I am not on the "compare" sheet right before the action so it tries to paste formulas down on the sheet i dont want it to. then trying to see how to eliminate that last "select."

VBA Code:
    Dim UsdRws As Long

'paste formulas down in Compare
Sheets("COMPARE").Range("F13:U13").Select
UsdRws = Range("A" & rows.count).End(xlUp).row
If UsdRws > 13 Then Range("F13:U" & UsdRws).FillDown
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
The issue is if you are not on the sheet, and don't use the sheet reference when referring to your range, it will default to the active sheet.
So your code should look like this:
VBA Code:
Dim UsdRws As Long

'paste formulas down in Compare
UsdRws = Sheets("Compare").Range("A" & rows.count).End(xlUp).row
If UsdRws > 13 Then Sheets("Compare").Range("F13:U" & UsdRws).FillDown
 
Upvote 0
The issue is if you are not on the sheet, and don't use the sheet reference when referring to your range, it will default to the active sheet.
So your code should look like this:
VBA Code:
Dim UsdRws As Long

'paste formulas down in Compare
UsdRws = Sheets("Compare").Range("A" & rows.count).End(xlUp).row
If UsdRws > 13 Then Sheets("Compare").Range("F13:U" & UsdRws).FillDown
WORKS. Thank you for that!
 
Upvote 0
You are welcome.

Just remember, whenever you want to refer to a range on a sheet that is not the active sheet, you need to include the sheet reference in your range.
Otherwise, it will default to the active sheet.
 
Upvote 0

Forum statistics

Threads
1,213,514
Messages
6,114,078
Members
448,547
Latest member
arndtea

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