Need to remove quote marks from vba vlookup result

Jim885

Well-known Member
Joined
Jul 8, 2012
Messages
663
The declarations for my code are set so that I receive decimal values for the values of x and y

I'm using this code;
VBA Code:
Private Sub Prct1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'=====================================================
' Immediately checks the percent and quantity entered
' and returns msgbox if entries cause excess of 100%
'=====================================================
Dim Lr1 As Long
Dim ws As Worksheet
Dim x
Dim y

Set ws = ActiveWorkbook.Worksheets("Main")
Lr1 = ws.Cells(Rows.Count, "A").End(xlUp).Row

    If Prct1.Text <> "" Then
        Qty1.Value = Application.VLookup((Me.CBox1.Value), ws.Range("A4:B" & Lr1), 2, 0) * Prct1.Value / 100
                 x = Application.VLookup((Me.CBox1.Value), ws.Range("A4:G" & Lr1), 7, 0)
                 y = Application.VLookup((Me.CBox1.Value), ws.Range("A4:H" & Lr1), 8, 0)
                 y = Format(y, "0.000%")
            If Qty1.Value > x Then
            MsgBox "The Percent/Quantity entered for this item - " & CBox1.Text & vbCr & _
                   "will exceed a 100% completion rate/value." & vbCr & vbCr & _
                   "The maximum Percentage remaining for this item is;  " & y & vbCr & _
                   "The maximum Quantity remaining for this item is;  " & x & vbCr & _
                   "Please adjust.", , "100% Valuation Exceeded"
            Application.EnableEvents = False
            Qty1.Text = ""
            Prct1.Text = ""
            Me.Qty1.SetFocus
            Application.EnableEvents = True
        Exit Sub
        End If
        Qty1.BackColor = RGB(205, 205, 205)  ' ---- Gray
    End If
End Sub

The below line of code returns Qty1 values in quotation marks, which causes a problem at "If Qty1.value > x Then"

VBA Code:
Qty1.Value = Application.VLookup((Me.CBox1.Value), ws.Range("A4:B" & Lr1), 2, 0) * Prct1.Value / 100

and is comparing values in quotation marks to values without quotation marks at this line of code;
VBA Code:
If Qty1.value > x Then

The end result is that a lower value in quote marks still shows as greater than x

I'd like to rid the quote marks coming from;

VBA Code:
Qty1.Value = Application.VLookup((Me.CBox1.Value), ws.Range("A4:B" & Lr1), 2, 0) * Prct1.Value / 100

Can someone please correct this?

Hope I've provided enough information.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
A shot:
Qty1.Value = Value(Application.VLookup((Me.CBox1.Value), ws.Range("A4:B" & Lr1), 2, 0) * Prct1.Value / 100)
 
Upvote 0
I guess qty1 is a textbox, so when you use it you must convert it to Value, use the Val function:

Change this:

If Qty1.Value > x Then

For this:

If Val(Qty1.Value) > x Then
 
Upvote 0
Thanks guys. But neither suggestion worked.

Yes, this code is for a textbox in a userform
 
Upvote 0
You can provide more information. exactly what values do you have in qty1 and in x.
What happens with my proposal and what happens with the proposal of dataluver.

change Val to Cdbl and try again

If cdbl(Qty1.Value) > x Then
 
Upvote 0
Thanks Dante. your proposal of using
VBA Code:
cdbl(Qty1.Value) > x
worked.

Using Val(Qty1.Value) > x did nothing ... quotes remained.

dataluvers suggestion resulted in a mismatch error.

Qty1 contained numerals with a decimal point

Thanks again!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,849
Members
449,051
Latest member
excelquestion515

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