treat numbers as text

kylefoley76

Well-known Member
Joined
Mar 1, 2010
Messages
1,553
The following code is not working:

Code:
 If Application.WorksheetFunction.VLookup(subj, sheet.Range("d1:f1048576"), 3, 0) = "indefinable" Then

I know the problem has to be because the subj is a number but I'm trying to treat it as a string. I've reformatted the cells as both text and general but that still does not work. The macro works on every other object in the d column except for those objects which are numbers. I have even tried the following code

If isnumeral(subj) Then
subj = CStr(subj)
End If

But that doesn't work either. The error message I'm getting is 1004.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
have you tried:
Code:
 If Application.WorksheetFunction.VLookup(format(subj, "@"), sheet.Range("d1:f1048576"), 3, 0) = "indefinable" Then
 
Upvote 0
Perhaps

If Application.WorksheetFunction.VLookup(subj & "", sheet.Range("d1:f1048576"), 3, 0) = "indefinable" Then
 
Upvote 0
Ok, I looked at some of my old drafts because I used to be able to do this and I noticed that in my old drafts I have that green triangle in the corner for the numbers. If I could get that green triangle back that might help things.
 
Upvote 0
1006 error is usually caused when It can't find the data (#N/A) values in worksheet....
vlookups in vba are tricky, this is one way to get rid of the error of when the vlookup finds nothing...


Code:
subj = 2    
    On Error Resume Next
        x = Application.WorksheetFunction.VLookup(subj, Range("D1:F1048576"), 3, 0)
    
        If Err <> 0 Then
            x = "Not Found"
        End If
        
    On Error GoTo 0


    If x = "indefinable" Then
       'Your Code Here
    End If

But you would still need to convert your data to text for it to work the best.
I have found that the best way to convert data to text is to use Text to Columns to convert,
here is a tidbit of code I sometimes use to do that...
Code:
    For x = 4 To 6  '-> Column Number
    
        Columns(x).Select
        Selection.TextToColumns Destination:=Range(Selection.Address), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
        :=Array(1, 2), TrailingMinusNumbers:=True
    
    Next x
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

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