简体   繁体   English

为什么在运行elseif语句时收到类型不匹配错误

[英]Why I received type mismatch error when running elseif statement

So I am trying to run the following code but received 'Type mismatch error" for line ElseIf Cells(x, 1) <> "" And Cells(x, 2) <> "" And Cells(x, 15).Value <= 0 Then . I am guessing it is something to do with the if statement that is not nested properly within the for and next statement. But they are all seem fine. I have spent hours on this trying to debug but to no avail. Who can tell me what goes wrong with my code? 因此,我尝试运行以下代码,但在ElseIf Cells(x, 1) <> "" And Cells(x, 2) <> "" And Cells(x, 15).Value <= 0 ThenElseIf Cells(x, 1) <> "" And Cells(x, 2) <> "" And Cells(x, 15).Value <= 0 Then收到“类型不匹配错误”。 ElseIf Cells(x, 1) <> "" And Cells(x, 2) <> "" And Cells(x, 15).Value <= 0 Then 。我猜测这与if语句未正确嵌套在fornext语句中有关,但它们似乎都很好。我花了数小时尝试调试,但无济于事。可以告诉我我的代码出了什么问题?

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, 5) = "" or Cells(x, 6) <> "" or Cells(x, 7) <> "" Then '(*Still Received error for this line)
        Cells(x, 16).Value = ""
        Cells(x, 17).Value = ""   

    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

Edited my code and it works. 编辑我的代码,它可以工作。

Dim x As Integer
For x = 4 To 100

   If Cells(x, 2).Value = "" Or Cells(x, 3).Value = "" Or Cells(x, 5).Value = "" Or Cells(x, 6).Value = "" Or Cells(x, 7).Value = "" Then
                       Cells(x, 17).Value = ""
                       Cells(x, 18).Value = ""

                    ElseIf Cells(x, 7).Value <= 0 Then
                       Cells(x, 17).Value = 6
                       Cells(x, 18).Value = -0.3179688

                    ElseIf Cells(x, 16).Value <= 0 Then
                        Cells(x, 17).Value = 1
                        Cells(x, 18).Value = 0.6820312

                    ElseIf Cells(x, 2).Value = "A. Agriculture, forestry and fishing" Then
                        Select Case LCase(Cells(x, 3).Value)
                            Case "all", "id", "sg"
                                    Select Case Cells(x, 16).Value
                                        Case Is > 4
                                            Cells(x, 17).Value = 5
                                            Cells(x, 18).Value = -0.2405524
                                        Case 2.01 To 4
                                            Cells(x, 17).Value = 4
                                            Cells(x, 18).Value = 0.0223717
                                        Case 1.01 To 2
                                            Cells(x, 17).Value = 3
                                            Cells(x, 18).Value = 0.112231
                                        Case 0.01 To 1
                                            Cells(x, 17).Value = 2
                                            Cells(x, 18).Value = 0.5928195
                                    End Select
                            Case "my", "th"
                                    Select Case Cells(x, 16).Value
                                        Case Is > 4.5
                                            Cells(x, 17).Value = 5
                                            Cells(x, 18).Value = -0.2405524
                                        Case 2.01 To 4.5
                                            Cells(x, 17).Value = 4
                                            Cells(x, 18).Value = 0.0223717
                                        Case 1.01 To 2
                                            Cells(x, 17).Value = 3
                                            Cells(x, 18).Value = 0.112231
                                        Case 0.01 To 1
                                            Cells(x, 17).Value = 2
                                            Cells(x, 18).Value = 0.5928195
                                    End Select
                        End Select
End if

Next x

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM