Formula doesn't work without ' in front of numbers?

jmpatrick

Active Member
Joined
Aug 17, 2016
Messages
477
Office Version
  1. 365
Platform
  1. Windows
This has been causing problems in several areas of my spreadsheet.

This formula will not work unless the number in column C is preceded by a single quotation mark:

VBA Code:
=IF(C54="1","",IF(C54="2","Duplex",IF(C54="3","3-Unit",IF(C54="4","4-Unit",IF(C54="5","5-Unit",IF(C54="6","6-Unit",""))))))

However, the single quotation mark breaks all my code like this:

VBA Code:
Private Sub cmdSearchModelCode_Click()
    Dim LastRow     As Long
 
    ModelCode = Trim(ModelCode.Text)
    If Len(ModelCode.Text) = 0 Then Exit Sub
 
    With wsModels
        LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
        For i = 1 To LastRow
            If .Cells(i, 1).Value = ModelCode Then
                ModelCode.Text = .Cells(i, 1).Value
                ModelName.Text = .Cells(i, 2).Value
                ModelUnits.Text = .Cells(i, 3).Value
    
             Me.cmdDeleteModel.Enabled = True
            Exit For
        End If
    Next
 End With
End Sub

Here's the formula not working in Column D (no single-quotation preceding the 2 in C2):

singlequote1.png


Here's the formula working correctly in Column D (with single-quotation preceding the 2 in C2). This works here but breaks the code above:

singlequote2.png


I've tried formatting the cell as text, formatting general and formatting as a number.

What am I doing wrong?
 
Assuming your codes actually are numeric (Fluff's comment suggests not), you'd add:

Code:
Dim ModelCode as Long

or change any existing variable declaration to Long.

Some of the values include a forward slash.
 
Upvote 0

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
In that case you'll need to convert only the numbers:

Code:
Dim ModelCode as Variant
ModelCode = Trim(ModelCode.Text)
If Isnumeric(ModelCode) then modelcode = CLng(modelcode)
 
Upvote 0
In that case you'll need to convert only the numbers:

Code:
Dim ModelCode as Variant
ModelCode = Trim(ModelCode.Text)
If Isnumeric(ModelCode) then modelcode = CLng(modelcode)

Changed it to this:

VBA Code:
Private Sub SearchModelCode_Click()
 
    Dim ModelCode As Variant
    ModelCode = Trim(ModelCode.Text)
    If IsNumeric(ModelCode) Then ModelCode = CLng(ModelCode)
    If Len(ModelCode.Text) = 0 Then Exit Sub
 
    With wsModels
        LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
        For i = 1 To LastRow
            If .Cells(i, 1).Value = ModelCode Then
                ModelCode.Text = .Cells(i, 1).Value
                ModelUnits.Text = .Cells(i, 3).Value
                ModelName.Text = .Cells(i, 5).Value
            Exit For
        End If
    Next
 End With
End Sub

Getting an error: Run-time error '424': Object required

I doesn't seem to like this: ModelCode = Trim(ModelCode.Text)
 
Upvote 0
Sorry - I had overlooked that you have a control by that name. Use:

Code:
Private Sub SearchModelCode_Click()
If Len(ModelCode.Text) = 0 Then Exit Sub
Dim theModelCode as Variant
theModelCode = Trim(ModelCode.Text)
If Isnumeric(theModelCode) then themodelcode = CLng(themodelcode)
    With wsModels
        LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
        For i = 1 To LastRow
            If .Cells(i, 1).Value = theModelCode Then
                ModelCode.Text = .Cells(i, 1).Value
                ModelUnits.Text = .Cells(i, 3).Value
                ModelName.Text = .Cells(i, 5).Value
            Exit For
        End If
    Next
 End With
End Sub
 
Upvote 0
Solution
Sorry - I had overlooked that you have a control by that name. Use:

Code:
Private Sub SearchModelCode_Click()
If Len(ModelCode.Text) = 0 Then Exit Sub
Dim theModelCode as Variant
theModelCode = Trim(ModelCode.Text)
If Isnumeric(theModelCode) then themodelcode = CLng(themodelcode)
    With wsModels
        LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
        For i = 1 To LastRow
            If .Cells(i, 1).Value = theModelCode Then
                ModelCode.Text = .Cells(i, 1).Value
                ModelUnits.Text = .Cells(i, 3).Value
                ModelName.Text = .Cells(i, 5).Value
            Exit For
        End If
    Next
 End With
End Sub

Works perfect. What did we just do?
 
Upvote 0
It converts numeric values to a real number before comparing them to the cell values.
 
Upvote 0

Forum statistics

Threads
1,214,936
Messages
6,122,340
Members
449,079
Latest member
rocketslinger

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