Autofill Not Autofilling

jski21

Board Regular
Joined
Jan 2, 2019
Messages
155
Office Version
  1. 2016
Platform
  1. Windows
Good day all,

Trying to run a macro from a command button on one tab (Tab 1) to transform data on another tab (Tab 3). Conducting a VLOOKUP from Tab 3 into Tab 1 and then autofilling the data down with this code:

Sheets("Tab 3").Range("D2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP([@User],Tab 1!C[4]:C[6],3,FALSE)"
Range("D2").AutoFill Range("D2:D" & Range("C" & Rows.Count).End(xlUp).Row)

Seems right but after executing the 3rd line to autofill the VLOOKUP, nothing happens. Column C is completely full. What gives?


Thanks!

jski
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Please try this. When as sheet has a space in the name, apostrophes need to encompass the sheet name. I don't know about using autofill instead of copy, but this should work
VBA Code:
  Sheets("Tab 3").Activate
  Range("D2").Select
  ActiveCell.FormulaR1C1 = "=VLOOKUP([@User],'Tab 1'!C[4]:C[6],3,FALSE)"
  Range("D2").Copy Range("D2:D" & Range("C" & Rows.Count).End(xlUp).Row)
 
Upvote 0
This can all be simplified to this:
VBA Code:
  Sheets("Tab 3").Activate
  Range("D2:D" & Cells(Rows.Count,"C").End(xlUp).Row).FormulaR1C1 = "=VLOOKUP([@User],'Tab 1'!C[4]:C[6],3,FALSE)"
 
Upvote 0
Thanks for your time gentlemen...

So for Jeff's solution, I receive a run time 1004 error; "Select method of Range class failed" on line 2 and a 1004; "Method 'Range' of object'_Worksheet failed" on line 3.

With Joe4s', I receive a 1004 error; "Application-defined or object-defined on line 2.
 
Upvote 0
In all my code selecting a cell like Range("D2").Select has never failed for me. As for the 3rd line, I changed it so that it would work from me.


VBA Code:
Sheets("Tab 3").Activate
  Range("D2").Select
  ActiveCell.FormulaR1C1 = "=VLOOKUP([@User],'Tab 1'!D:F,3,FALSE)"
  Range("D2").Copy Range("D2:D" & Range("C" & Rows.Count).End(xlUp).Row)
 
Upvote 0
In all my code selecting a cell like Range("D2").Select has never failed for me. As for the 3rd line, I changed it so that it would work from me.


VBA Code:
Sheets("Tab 3").Activate
  Range("D2").Select
  ActiveCell.FormulaR1C1 = "=VLOOKUP([@User],'Tab 1'!D:F,3,FALSE)"
  Range("D2").Copy Range("D2:D" & Range("C" & Rows.Count).End(xlUp).Row)
It isn't a "problem", it is just totally unnecessary, and besides making the code longer, every unnecessary select actually slows the code down.
It may not be noticeable if just a single select, but if this were inside a loop, you could be doing hundreds, thousands, etc unnecessary select, and that will start to be noticeable.
Most of the time in VBA, it is not necessary to select ranges to work on them. They are usually the result of using the Macro Recorder to record code, which is very literal and records everything, every cell you select, every scroll you make. And those type of things are not necessary to make your code work.

So for Jeff's solution, I receive a run time 1004 error; "Select method of Range class failed" on line 2 and a 1004; "Method 'Range' of object'_Worksheet failed" on line 3.

With Joe4s', I receive a 1004 error; "Application-defined or object-defined on line 2.
That would seem to suggest that there is a problem with the VLOOKUP formula.
I noticed your original did NOT have single-quotes around the Sheet name of 'Tab 1', and Jeff's did. I worked off of his copy.
Did the original formula your code placed in cell D2 work for you? Did it do what it was supposed to, at least in that cell?
 
Upvote 0
Thanks everyone. Joe4--Yes, the VLOOKUP places the correct result in Cell D2. It just doesn't autofill the formula down. Here's the code that seems to work using Jeff's solution for Line 4:

'Insert VLOOKUP formula in Hourly column
Sheets("Tab 3").Activate
Sheets("Tab 3").Range("D2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP([@User],'Tab 1'!C[4]:C[6],3,FALSE)"
Range("D2").Copy Range("D2:D" & Range("C" & Rows.Count).End(xlUp).Row)

The only thing with Line 4 is it just glosses over the code without doing anything. I also used this in Line 4 with the same result:

Range("D2").AutoFill Range("D2:D" & Range("C" & Rows.Count).End(xlUp).Row)
Selection.AutoFill Destination:=Range("Tab 3[Hourly]")



It gets a bit weirder...I have another column that I use this code for that fulfills the VLOOKUP and autofills down with no issue:

'Insert VLOOKUP formula in PayNo column
Sheets("Tab 3").Select
Sheets("Tab 3").Range("H2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP([@[Start Date]],'Tab 1'!C[-7]:C[-6],2,FALSE)"


BTW, my data is in a table so shouldn't it automatically fill down if your referencing direct cells in a table?
 
Upvote 0
You are trying to look at column C on your "Tab 3" sheet to find how many rows to copy it down.
Is that correct?
Can you show us an image of "Tab 3"?
 
Upvote 0
Yes, using Column C was the intention. Here's an image:

Lead Chargeback 2024 Pay01-Pay12.xlsm
ABCDEFGHIJKLM
1ProjectGrantUserHourlyGroupBillableStart DatePayNoStart TimeEnd DateEnd TimeDuration (h)Duration (decimal)
2LarryABCUser 128.15Group 1Yes05/24/2024Pay1211:31 PM05/25/20247:31 AM8:008.00
3CurlyDEFUser 2Group 2Yes05/24/2024Pay124:00 PM05/24/20244:30 PM0:300.50
4MoeGHIUser 3Group 3Yes05/24/2024Pay123:00 PM05/24/20244:00 PM1:001.00
5ShempJKLUser 4Group 4Yes05/24/2024Pay122:00 PM05/24/20244:00 PM2:002.00
6JoeMNOUser 5Group 5Yes05/24/2024Pay1212:00 PM05/24/20244:00 PM4:004.00
7LarryPQRUser 6Group 6Yes05/24/2024Pay1212:00 PM05/24/20244:00 PM4:004.00
Clockify
 
Upvote 0
So the sheet name for column C where the last row is defined and the formula is being placed is actually Clockify?
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,198
Members
452,617
Latest member
Narendra Babu D

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