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?
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
You need to remove the quotes from the numbers, in that formula.

Also what version of Excel are you using?
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
You need to remove the quotes from the numbers, in that formula.

Also what version of Excel are you using?
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

Account details updated! I'm using Office 365 and Windows 10.

Removing the quotes doesn't work. It now displays the formula in column D.
 
Upvote 0
Thanks for that, you could also rewrite your formula like
Excel Formula:
=IF(C54=2,"Duplex",IF(AND(C54>=3,C54<=6),C54&"-Unit",""))
 
Upvote 0
Time to revisit this one. Here's where I'm at:

VBA Code:
Private Sub SearchModelCode_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
                ModelUnits.Text = .Cells(i, 3).Value
                ModelName.Text = .Cells(i, 5).Value
            Exit For
        End If
    Next
 End With
End Sub

Column 1 is formatted as General and consists of numbers. When I run the code above I get no matching. However, when the values in Column 1 are preceded by a ' it works.

I don't know if my code is wrong or the formatting of the cells in Column 1 is wrong.
 
Upvote 0
Declare ModelCode as Long
 
Upvote 0
Are you now saying the formula doesn't work, or the code doesn't, or both?
Column 1 is formatted as General and consists of numbers.
1559/1559 is not a number it's text.
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,215,440
Messages
6,124,882
Members
449,193
Latest member
PurplePlop

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