VBA Code to Convert to Number

Status
Not open for further replies.

Justinian

Well-known Member
Joined
Aug 9, 2009
Messages
1,557
Office Version
  1. 365
Platform
  1. Windows
How do I alter this code so the results of the VLOOKUP and RIGHT formulas are converted from text to number?

Sub VLOOKUP()
Dim LastRw As Long
LastRw = Cells(Rows.Count, "A").End(xlUp).Row

' Run VLOOKUP formula starting in B6
With Range("B6")
.FormulaR1C1 = _
"=IFERROR(IF(OR(LEFT(RC[-1],1)=""9"",LEFT(RC[-1],2)<=""32""),VLOOKUP(RC[-1],Gardens,2,FALSE),RC[-1]),"""")"
.AutoFill Destination:=Range("B6:B" & LastRw)
End With

' Applies RIGHT formula to column I and pastes resulting values into column A
With Range("I6:I" & Range("A" & Rows.Count).End(xlUp).Row)
.FormulaR1C1 = "=RIGHT(RC[-8],LEN(RC[-8])-FIND(""-"",RC[-8],1))"
.Copy
Range("A6").PasteSpecial xlPasteValues
.ClearContents
End With

ActiveSheet.PageSetup.PrintArea = Range("A4").CurrentRegion.Address

End Sub
 
How do I do that? When I ran the RIGHT formula, everything is formatted as general.
 
Upvote 0

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Ok, here is what the data looks like prior to running any code:

Move-Ins.xlsm
ABC
5Building/UnitStreet AddressScheduled Move-In Date
610-101507/21/20
710-102307/21/20
819-195507/21/20
923-234207/21/20
1024-241107/21/20
1128-285607/21/20
1233-3309J07/21/20
1334-3400H07/21/20
1436-3608L07/21/20
Report


Here is what the data looks like after running the RIGHT formula:

Move-Ins.xlsm
ABC
5Building/UnitStreet AddressScheduled Move-In Date
6101507/21/20
7102307/21/20
8195507/21/20
9234207/21/20
10241107/21/20
11285607/21/20
123309J07/21/20
133400H07/21/20
143608L07/21/20
Report


Finally, here is what the data looks like after running VLOOKUP:

Move-Ins.xlsm
ABC
5Building/UnitStreet AddressScheduled Move-In Date
61015 07/21/20
71023 07/21/20
81955 07/21/20
92342 07/21/20
102411 07/21/20
112856 07/21/20
123309J3309J07/21/20
133400H3400H07/21/20
143608L3608L07/21/20
Report
Cell Formulas
RangeFormula
B6:B14B6=IFERROR(IF(OR(LEFT(A6,1)="9",LEFT(A6,2)<="32"),VLOOKUP(A6,Gardens,2,FALSE),A6),"")
Named Ranges
NameRefers ToCells
Gardens='Garden Addresses'!$A$1:$B$1622B6:B14


The function works but because rows 6-11 are formatted as text, I have to manually convert to numbers in order to gain the final result:

Move-Ins.xlsm
ABC
5Building/UnitStreet AddressScheduled Move-In Date
610156120 W. 1st Street # 10-1507/21/20
710236104 W. 2nd Street # 10-2307/21/20
81955593 1/2 S. 10th Street # 19-5507/21/20
923426117 W. 2nd Street # 23-4207/21/20
1024116061 W. Fifth Street # 24-1107/21/20
1128565906 W. 1st Street # 28-5607/21/20
123309J3309J07/21/20
133400H3400H07/21/20
143608L3608L07/21/20
Report
Cell Formulas
RangeFormula
B12:B14B12=IFERROR(IF(OR(LEFT(A12,1)="9",LEFT(A12,2)<="32"),VLOOKUP(A12,Gardens,2,FALSE),A12),"")
Named Ranges
NameRefers ToCells
Gardens='Garden Addresses'!$A$1:$B$1622B12:B14
 
Upvote 0
Ok, try using this formula
VBA Code:
=iferror(RIGHT(RC[-8],LEN(RC[-8])-FIND(""-"",RC[-8],1))+0,=RIGHT(RC[-8],LEN(RC[-8])-FIND(""-"",RC[-8],1)))
 
Upvote 0
I never got a reply back so I thought a new thread might be less confusing than a bump.

I tried your code but the debugger gets hung up on that new code you gave me.
 
Upvote 0
I never got a reply back so I thought a new thread might be less confusing than a bump.
In that case I will close this thread. Please do not make duplicate threads in future.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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