VBA Variable type

bensonsearch

Well-known Member
Joined
May 26, 2011
Messages
844
Hi All,

may be easy but i need a variable type to hold 13 digits in it. I first did long but it aint long enough.

It is told hold a barcode.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
the code im testing in is
Code:
Dim barcode As Variant
Dim name As Variant
Dim stock As Variant
Dim barcode2 As Double 'error with this one
 
barcode = InputBox("Barcode Of Item?")
If barcode = "" Then
Exit Sub
Else
ThisWorkbook.Sheets("Money In").Range("c1").Value = barcode
If IsNumeric(barcode) Then
        barcode2 = ThisWorkbook.Sheets("Money In").Range("c1").Value
        ThisWorkbook.Sheets("Money In").Range("c1").Value = ""
       
        name = Application.VLookup(barcode2, Worksheets("Item").Range("A2:H15000"), 4)
         'MsgBox (barcode2)
            If IsError(name) Then
            MsgBox ("Sorry barcode not found, Please try again")
            Else
            TextBox1.Text = name
     
            stock = Application.VLookup(barcode2, Worksheets("Item").Range("A2:H15000"), 6)
                If IsError(stock) Then
                Exit Sub
                Else
                TextBox2.Text = stock
                TextBox1.Visible = True
                TextBox2.Visible = True
                Label9.Visible = True
                Label10.Visible = True
                Label12.Visible = True
                Label13.Visible = True
                End If
               
            End If
    Else
    MsgBox ("Please enter numbers only")
    Exit Sub
    End If
End If
 
Upvote 0
You have invoked binary search Vlookup. You need to invoke linear search.

E.g:
Code:
name = Application.VLookup(barcode2, Worksheets("Item").Range("A2:H15000"), 4[B][COLOR="Red"], False[/COLOR][/B])
 
Upvote 0
You have invoked binary search Vlookup. You need to invoke linear search.

E.g:
Code:
name = Application.VLookup(barcode2, Worksheets("Item").Range("A2:H15000"), 4[B][COLOR=red], False[/COLOR][/B])


that would help, now it says barcode not found..... when i search my item sheet its there :(
 
Upvote 0
Click on the barcode in the sheet (the one that you says does exist).

Open the VBE and in the immediate window* type:

?TypeName(ActiveCell.Value)

What result is displayed back in the immediate window?

*If the immediate window isn't already displayed hit Ctl+G.
 
Upvote 0
Click on the barcode in the sheet (the one that you says does exist).

Open the VBE and in the immediate window* type:

?TypeName(ActiveCell.Value)

What result is displayed back in the immediate window?

*If the immediate window isn't already displayed hit Ctl+G.


for some reason it calls it a string. Can i change that to be number keeping 0's? or can vlookup do string as well?
 
Upvote 0
Well if they are always strings then you could use that data type for your barcode. If there are a mix of strings and doubles in the sheet then you'd be best to convert them all to doubles. The best way would be:

Select column A.
Go to Format Cells and choose custom format and enter 0000000000000
Next go to Text to Column > Delimited > Next > Finish

I don't like using a mix of data types but where your VlookUp fails you could also do a secondary lookup and use the CStr coercion function to convert from double to string:

name = Application.Vlookup(CStr(barcode),.......)
 
Upvote 0
Well if they are always strings then you could use that data type for your barcode. If there are a mix of strings and doubles in the sheet then you'd be best to convert them all to doubles. The best way would be:

Select column A.
Go to Format Cells and choose custom format and enter 0000000000000
Next go to Text to Column > Delimited > Next > Finish

I don't like using a mix of data types but where your VlookUp fails you could also do a secondary lookup and use the CStr coercion function to convert from double to string:

name = Application.Vlookup(CStr(barcode),.......)


I have converted to double via method above. thank you thank you thank you

help is very much appriciated
 
Upvote 0

Forum statistics

Threads
1,215,028
Messages
6,122,753
Members
449,094
Latest member
dsharae57

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