VBA Code Alteration

Justinian

Well-known Member
Joined
Aug 9, 2009
Messages
1,557
Office Version
  1. 365
Platform
  1. Windows
How do I alter the following code so that instead of autofill stopping at B68, the autofill stops at the last row of data in column A?

Sub VLOOKUP()

' Run VLOOKUP formula starting in B9
Range("B9").Select
Selection.FormulaR1C1 = _
"=IFERROR(IF(OR(LEFT(RC[-1],1)=""9"",LEFT(RC[-1],2)<=""32""),VLOOKUP(RC[-1],Gardens,2,FALSE),RC[-1]),"""")"
Selection.AutoFill Destination:=Range("B9:B68")
Range("B9:B68").Select
Range("A1").Select
End Sub
 
That works but only for cells formatted as text. The cells that are already formatted as a number return a #VALUE error.
 
Upvote 0

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Don't see why that should be. If you add zero to a number you get the number. Post some examples of your data showing both numbers and text types.
 
Upvote 0
Ok.

Here is the workbook after I paste data from an external source:

Move-Ins.xlsm
ABCDEFG
934-3405E06/12/206/12-4
1036-3612K06/12/206/13-20
1140-4011F06/12/206/14-3
1244-4407A06/12/206/15-9
1312-122806/13/206/16-4
1419-195306/13/20   
1520-201306/13/20   
1633-3305F06/13/20 
1734-3407A06/13/20 
1834-3408H06/13/20 
1934-3410L06/13/20
2038-3811C06/13/20
2140-4003F06/13/20
2242-4200H06/13/20
2342-4209F06/13/20
2442-4212L06/13/20
2543-4308E06/13/20
2646-4604G06/13/20
2746-4604M06/13/20
2847-4705A06/13/20
2947-4705C06/13/20
3049-4902D06/13/20
3150-5001B06/13/20
3226A-26A2006/13/20
3341-4109C06/14/20
3446-4604L06/14/20
3548-4807J06/14/20
3632-322106/15/20
3734-3410M06/15/20
3835-3505L06/15/20
3937-3711J06/15/20
4037-3712J06/15/20
4138-3807C06/15/20
4238-3807D06/15/20
4348-4801L06/15/20
4449-4912C06/15/20
4535-3512D06/16/20
4638-3805A06/16/20
4745-4501B06/16/20
4850-5005G06/16/20
Report
Cell Formulas
RangeFormula
E9:E15E9=IFERROR(IF(ROWS($E$8:E8)<2,MIN($C$9:$C$96),AGGREGATE(15,6,$C$9:$C$96/($C$9:$C$96>E8),1)),"")
G9:G15G9=IFERROR(IF(E9="","",COUNTIF($C$9:$C$100,E9)),"")
F9:F15F9=IF(E9="","","-")
F16:F18F16=IF(E16=FALSE,"","-")


Here is the same workbook after I run the macro (you can see the code runs but ignores the cells formatted as text:

Move-Ins.xlsm
ABCDEFG
93405E3405E06/12/206/12-4
103612K3612K06/12/206/13-20
114011F4011F06/12/206/14-3
124407A4407A06/12/206/15-9
131228 06/13/206/16-4
141953 06/13/20   
152013 06/13/20   
163305F3305F06/13/20 
173407A3407A06/13/20 
183408H3408H06/13/20 
193410L3410L06/13/20
203811C3811C06/13/20
214003F4003F06/13/20
224200H4200H06/13/20
234209F4209F06/13/20
244212L4212L06/13/20
254308E4308E06/13/20
264604G4604G06/13/20
274604M4604M06/13/20
284705A4705A06/13/20
294705C4705C06/13/20
304902D4902D06/13/20
315001B5001B06/13/20
3226A20505 S. Curson #26A-2006/13/20
334109C4109C06/14/20
344604L4604L06/14/20
354807J4807J06/14/20
363221 06/15/20
373410M3410M06/15/20
383505L3505L06/15/20
393711J3711J06/15/20
403712J3712J06/15/20
413807C3807C06/15/20
423807D3807D06/15/20
434801L4801L06/15/20
444912C4912C06/15/20
453512D3512D06/16/20
463805A3805A06/16/20
474501B4501B06/16/20
485005G5005G06/16/20
Report
Cell Formulas
RangeFormula
E9:E15E9=IFERROR(IF(ROWS($E$8:E8)<2,MIN($C$9:$C$96),AGGREGATE(15,6,$C$9:$C$96/($C$9:$C$96>E8),1)),"")
G9:G15G9=IFERROR(IF(E9="","",COUNTIF($C$9:$C$100,E9)),"")
F9:F15F9=IF(E9="","","-")
F16:F18F16=IF(E16=FALSE,"","-")
B9:B48B9=IFERROR(IF(OR(LEFT(A9,1)="9",LEFT(A9,2)<="32"),VLOOKUP(A9,Gardens,2,FALSE),A9),"")
Named Ranges
NameRefers ToCells
Gardens='Garden Addresses'!$A$1:$B$1622B9:B48
 
Upvote 0
Sorry, I don't understand what you mean by: " you can see the code runs but ignores the cells formatted as text: ". And what you show in post #13 doesn't seem consistent with what you said in post #11.
 
Upvote 0
In my second screenshot in post #13, the RIGHT formula ran as well as the VLOOKUP but cells such as B13:B15 are blank because A13:A15 is formatted as text. When I select A13:A15, that yellow diamond-shape with the exclamation point inside it appears and says "the number in this cell is formatted as text." When I click that yellow shape and select "convert to number," B13:B15 now run the VLOOKUP formula. In other words, the macro works in all cells formatted as numbers; those formatted as text are ignored.
 
Upvote 0
Try removing the quote marks around the numbers in your col B formula like this:

=IFERROR(IF(OR(LEFT(A10,1)=9,LEFT(A10,2)<=32),VLOOKUP(A10,Gardens,2,FALSE),A10),"")
 
Upvote 0
That will not work because then the result is the contents of column A instead of the VLOOKUP. So 1228 in A13 becomes 1228 in B13 instead of the look up value.
 
Upvote 0
I don't see why this formula in col B won't work:

=IFERROR(IF(OR(LEFT(A9,1)+0=9,LEFT(A9,2)+0<=32),VLOOKUP(A9,Gardens,2,FALSE),A9),"")

regardless of whether the A cell holds a number or text.
 
Upvote 0
When I add those zeros per your formula above, now the VLOOKUP does not work and cells in column A are still formatted as text:

Move-Ins.xlsm
ABCDEFG
8Building/UnitStreet AddressScheduled Move-In Date
90953 06/15/206/15-27
101823 06/15/206/16-6
111953 06/15/206/17-6
123221 06/15/206/18-7
133305F 06/15/20   
143408H 06/15/20   
153410L 06/15/20   
163410M 06/15/20 
173505L 06/15/20 
183612K 06/15/20 
193711J 06/15/20
203712J 06/15/20
213805A 06/15/20
223807C 06/15/20
233807D 06/15/20
Report
Cell Formulas
RangeFormula
E9:E15E9=IFERROR(IF(ROWS($E$8:E8)<2,MIN($C$9:$C$96),AGGREGATE(15,6,$C$9:$C$96/($C$9:$C$96>E8),1)),"")
G9:G15G9=IFERROR(IF(E9="","",COUNTIF($C$9:$C$100,E9)),"")
F9:F15F9=IF(E9="","","-")
F16:F18F16=IF(E16=FALSE,"","-")
B9:B23B9=IFERROR(IF(OR(LEFT(A9,1)+0="9",LEFT(A9,2)+0<="32"),VLOOKUP(A9,Gardens,2,FALSE),A9),"")
Named Ranges
NameRefers ToCells
Gardens='Garden Addresses'!$A$1:$B$1622B9:B23
 
Upvote 0
That's because you didn't use the formula I posted - the formula you show still has quote marks around the 9 and 32 values.
 
Upvote 0

Forum statistics

Threads
1,216,045
Messages
6,128,480
Members
449,455
Latest member
jesski

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