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
 
It still does not work:

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
133305F3305F06/15/20   
143408H3408H06/15/20   
153410L3410L06/15/20   
163410M3410M06/15/20 
173505L3505L06/15/20 
183612K3612K06/15/20 
193711J3711J06/15/20
203712J3712J06/15/20
213805A3805A06/15/20
223807C3807C06/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:B22B9=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:B22
 
Upvote 0

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
I think you are getting errors in your formula in col B when there are no alpha characters in col A, but your IFERROR wrapper is covering them up. What happens if you remove the IFERROR wrapper on the formulas in B9:B11?
 
Upvote 0
Removing my IFEERROR wrap simply reveals the error:

Move-Ins.xlsm
ABCDEFG
8Building/UnitStreet AddressScheduled Move-In Date
90953#N/A06/15/206/15-27
101823#N/A06/15/206/16-6
111953#N/A06/15/206/17-6
123221#N/A06/15/206/18-7
133305F3305F06/15/20   
143408H3408H06/15/20   
153410L3410L06/15/20   
163410M3410M06/15/20 
173505L3505L06/15/20 
183612K3612K06/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)),"")
B9:B18B9=IF(OR(LEFT(A9,1)+0=9,LEFT(A9,2)+0<=32),VLOOKUP(A9,Gardens,2,FALSE),A9)
F9:F15F9=IF(E9="","","-")
F16:F18F16=IF(E16=FALSE,"","-")
Named Ranges
NameRefers ToCells
Gardens='Garden Addresses'!$A$1:$B$1622B9:B18
 
Upvote 0
So, that says your VLOOKUP is not able to find the lookup value in the first column of the named range Gardens. But the VLOOKUP is definitely being called, it's just returning #N/A. Can you post a sample of the range Gardens?
 
Upvote 0
The VLOOKUP is able to find the appropriate code but after the formula runs, I have to convert the cells without an alpha character to numbers, then the VLOOKUP works.

Here is a sample of the named range "Gardens:"

Move-Ins.xlsm
AB
58952394 S. Genesee # 09-52
59953392 S. Genesee # 09-53
60954390 S. Genesee # 09-54
61955388 S. Genesee # 09-55
62956386 S. Genesee # 09-56
Garden Addresses
 
Upvote 0
The Vlookup is working, the issue is you are trying to lookup values that don't exist in your lookup range.
 
Upvote 0
They do exist. If you look at post #23, 953 is the code and it exists in the range from post #25. Remember, once I run the VLOOKUP and convert column A to numbers, the correct address then appears in column B.
 
Upvote 0
They do exist. If you look at post #23, 953 is the code and it exists in the range from post #25. Remember, once I run the VLOOKUP and convert column A to numbers, the correct address then appears in column B.
Post #23 shows the text value 0953 in col A, not 953. You need to fix your lookup table. The Vlookup is looking for 0953 not 953. If 0953 isn't in the first column of the lookup table Gardens, Vlookup returns #N/A.
 
Upvote 0
I understand why you think that but that is not what is happening. When I change 0953 to 953 to match the Gardens range, the VLOOKUP still returns a blank until i select the cell and convert to number.

Also, look at 1823 in A9. In the Gardens range, 1823 is the code so the VLOOKUP formula should work but it does not, until I convert that to a number as well. Even removing the quotes surrounding 9 and 32 and adding the +0 as you suggested, the formula still does not work until I convert non alpha codes to numbers.

Move-Ins.xlsm
ABCDEFG
8Building/UnitStreet AddressScheduled Move-In Date
91823 06/16/206/16-11
10953 06/16/206/17-12
112421 06/16/206/18-7
123408H3408H06/16/206/19-7
Report
Cell Formulas
RangeFormula
E9:E12E9=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)),"")
F9:F12F9=IF(E9="","","-")
G9:G12G9=IFERROR(IF(E9="","",COUNTIF($C$9:$C$100,E9)),"")
B9:B12B9=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:B12
 
Upvote 0

Forum statistics

Threads
1,215,344
Messages
6,124,407
Members
449,157
Latest member
mytux

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