Why I received type mismatch error when running elseif statement?

tnkh12

New Member
Joined
Feb 28, 2016
Messages
6
I did not run into any problem when I used `Range` function for a specific observation (e,g, `Range("A4")`. But when I wanted the same result to be replicated for each row, I changed to `cells` function (e.g. `Cells(x,15)`), and received "Type Mismatch" error for ` ElseIf Cells(x, 1) <> "" And Cells(x, 2) <> "" And Cells(x, 15).Value <= 0 Then` Here are my part of my codes. I have tried hours debugging it but to no avail. Any help?
Code:
Dim x As Integer
For x = 4 To 100
            If Cells(x, 1) <> "" And Cells(x, 2) <> "" And Cells(x, 6).Value <= 0 Then                  
               Cells(x, 16).Value = 6
               Cells(x, 17).Value = -0.3179688
                            
                        ElseIf Cells(x, 1) <> "" And Cells(x, 2) <> "" And Cells(x, 15).Value <= 0 Then (*Received error for this line)
                            Cells(x, 16).Value = 1
                            Cells(x, 17).Value = 0.6820312
                                              
                        ElseIf Cells(x, 1).Value = "A. Agriculture, forestry and fishing" Then
                            Select Case LCase(Cells(x, 2).Value)
                                Case "all", "id", "sg"
                                        Select Case Cells(x, 15).Value
                                            Case Is > 4
                                                Cells(x, 16).Value = 5
                                                Cells(x, 17).Value = -0.2405524
                                            Case 2.01 To 4
                                                Cells(x, 16).Value = 4
                                                Cells(x, 17).Value = 0.0223717
                                            Case 1.01 To 2
                                                Cells(x, 16).Value = 3
                                                Cells(x, 17).Value = 0.112231
                                            Case 0.01 To 1
                                                Cells(x, 16).Value = 2
                                                Cells(x, 17).Value = 0.5928195
                                        End Select
                                Case "my", "th"
                                        Select Case Cells(x, 15).Value
                                            Case Is > 4.5
                                                Cells(x, 16).Value = 5
                                                Cells(x, 17).Value = -0.2405524
                                            Case 2.01 To 4.5
                                                Cells(x, 16).Value = 4
                                                Cells(x, 17).Value = 0.0223717
                                            Case 1.01 To 2
                                                Cells(x, 16).Value = 3
                                                Cells(x, 17).Value = 0.112231
                                            Case 0.01 To 1
                                                Cells(x, 16).Value = 2
                                                Cells(x, 17).Value = 0.5928195
                                        End Select
                                Case ""
                                    Cells(x, 16).Value = ""
                                    Cells(x, 17).Value = ""
                            End Select
             End if

Next x

End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
try putting .Value after the cell reference
seems to work ok for me
Code:
ElseIf Cells(x, 1).value <> "" And Cells(x, 2).value <> "" And Cells(x, 15).Value <= 0 Then
 
Last edited:
Upvote 0
Doesn't work. For information, the cell (x,15) contains an existing formula of
Code:
=(D4+E4)F4
in the spreadsheet. Not sure whether that's the source of issue.
 
Upvote 0
Hmm, used your formula in x,15 AND added the .Value....and it worked fine for me !!
Have you tried using it in a clean version of the workbook....copy the Macro.... close the workbook without saving....reopen and paste the code back in.
Sometimes the VBE will collect some invisible JUNK and screw the codes up.
 
Upvote 0
I noticed what causes the problem. Code runs totally fine if all cells in
Code:
=(D4+E4)F4
are filled. But if anyone of them isn't filled, I got this mismatch error. What code should I put in so that if anyone of these three cells is not filled,
Code:
cells(x,15)
will return nothing, and would not give me error?
 
Upvote 0
Depending on how they are populated, you use something like this
Rich (BB code):
Sub MM1()
Dim x As Integer
For x = 4 To 100
           If Cells(x, 1) = "" Or Cells(x, 2) = "" Or Cells(x, 15).Value = "" Or Cells(x, 6).Value = "" Then
           MsgBox "Some Cells are Missing data !!!....Please Retry Process !", vbOKOnly
           Exit Sub
           End If
            If Cells(x, 1) <> "" And Cells(x, 2) <> "" And Cells(x, 6).Value <= 0 Then
               Cells(x, 16).Value = 6
               Cells(x, 17).Value = -0.3179688
                            
                        ElseIf Cells(x, 1) <> "" And Cells(x, 2) <> "" And Cells(x, 15).Value <= 0 Then
                            Cells(x, 16).Value = 1
                            Cells(x, 17).Value = 0.6820312
                                              
                        ElseIf Cells(x, 1).Value = "A. Agriculture, forestry and fishing" Then
                            Select Case LCase(Cells(x, 2).Value)
                                Case "all", "id", "sg"
                                        Select Case Cells(x, 15).Value
                                            Case Is > 4
                                                Cells(x, 16).Value = 5
                                                Cells(x, 17).Value = -0.2405524
                                            Case 2.01 To 4
                                                Cells(x, 16).Value = 4
                                                Cells(x, 17).Value = 0.0223717
                                            Case 1.01 To 2
                                                Cells(x, 16).Value = 3
                                                Cells(x, 17).Value = 0.112231
                                            Case 0.01 To 1
                                                Cells(x, 16).Value = 2
                                                Cells(x, 17).Value = 0.5928195
                                        End Select
                                Case "my", "th"
                                        Select Case Cells(x, 15).Value
                                            Case Is > 4.5
                                                Cells(x, 16).Value = 5
                                                Cells(x, 17).Value = -0.2405524
                                            Case 2.01 To 4.5
                                                Cells(x, 16).Value = 4
                                                Cells(x, 17).Value = 0.0223717
                                            Case 1.01 To 2
                                                Cells(x, 16).Value = 3
                                                Cells(x, 17).Value = 0.112231
                                            Case 0.01 To 1
                                                Cells(x, 16).Value = 2
                                                Cells(x, 17).Value = 0.5928195
                                        End Select
                                Case ""
                                    Cells(x, 16).Value = ""
                                    Cells(x, 17).Value = ""
                            End Select
             End If

Next x

End Sub
 
Upvote 0

Forum statistics

Threads
1,217,972
Messages
6,139,688
Members
450,226
Latest member
DrKraGeN

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