Autofill Dynamic Selection Range

JayR33

New Member
Joined
Oct 17, 2023
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I currently use the below code to select the first empty cell on row 2 and then do a vlookup formula and finally auto fill that formula to the bottom of the data (I have not pasted the vlookup line as it is irrelevant).

Cells(2, Range("A2").End(xlToRight).Column + 1).Select - This selects the first empty cell on row 2

*VLOOKUP FORMULA*


Selection.AutoFill Destination:=Range("B2:B" & Range("A" & Rows.Count).End(xlUp).Row)
Range(Selection, Selection.End(xlDown)).Select - This auto fills to the bottom of the data for the cell selected in step 1. However I can no longer use this as I need the range to be dynamic.

My problem is that the second line of code states cells "B2:B" but this won't always be the same range. I need this range to be the same as the one selected during the first line of code.

Is there anything I can add to the auto fill code, to always select the same range as the one selected in step 1, rather than always selecting column B (which was the first set of data ran)?

Thanks in advance.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Try changing the line that calculates "c" to this:
VBA Code:
c = Cells(2, Range("A2").End(xlToRight).Column + 1).Column
Great, that works now! However I've noticed something else :(

My vlookup formula will also need to be dynamic won't it? Because at the moment it is filling in column B for example, but next time it will be filling in column C, and then column D next time etc. So if it's moving across 1 column each time, then my vlookup will be wrong as the reference point will be "RC-1" at the moment but next time it will need to be "RC-2".
 
Upvote 0
My vlookup formula will also need to be dynamic won't it? Because at the moment it is filling in column B for example, but next time it will be filling in column C, and then column D next time etc. So if it's moving across 1 column each time, then my vlookup will be wrong as the reference point will be "RC-1" at the moment but next time it will need to be "RC-2".
Then you just need to change your range reference from RELATIVE to ABSOLUTE, i.e.
change this:
VBA Code:
RC[-1]
which says to choose one column to the left of wherever you are
to this:
VBA Code:
RC1
which says pick the first column (column A).
 
Upvote 0
Then you just need to change your range reference from RELATIVE to ABSOLUTE, i.e.
change this:
VBA Code:
RC[-1]
which says to choose one column to the left of wherever you are
to this:
VBA Code:
RC1
which says pick the first column (column A).
Thanks a lot for the help Joe! Really appreciate it.
 
Upvote 0
You are welcome.
Glad I was able to help.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

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