If else语句程序中的运行时错误“ 1004”

时间:2019-08-27 13:14:18

标签: excel vba

我正在尝试使用两个单元格来确定要返回的值。目的是读入单元格Z3中,以确定要使用的高度,然后读取单元格AA3中,根据提供的高度值确定要使用的类。找到班级后,我希望它打印到AB3单元格,然后对下一行重复,依此类推。

我当前遇到运行时错误1004。我不确定是什么原因导致的,我查了一下并说这可能是由空变量引起的,但我没有看到它。任何帮助将非常感谢!

这是针对一个正在工作的小型项目,我在VBA上并不出色,只是想尝试使一项任务变得简单而又不需要花费很长时间。

     Private Sub CRIC_CLASS_Click()
         Dim POLES As Worksheet
         Dim cell_value As String     ' Length
         Dim cell_value2 As String    ' MPCRIC
         Dim i As Integer             ' first intger for for loop
         Dim Class As String          ' Stores class value to return
         Dim Row As Integer   
         Set POLES = ActiveSheet
             For i = 3 To 500
             cell_value = POLES.Cells(i, "Z").Value
             cell_value2 = POLES.Cells(i, "AA").Value                       
                 If cell_value = 50 Then            
                     Row = 7                
                 ElseIf cell_value = 55 Then            
                     Row = 8            
                 ElseIf cell_value = 60 Then            
                     Row = 9            
                 ElseIf cell_value = 65 Then            
                     Row = 10            
                 ElseIf cell_value = 70 Then            
                     Row = 11            
                 ElseIf cell_value = 75 Then            
                     Row = 12            
                 ElseIf cell_value = 80 Then            
                     Row = 13            
                 ElseIf cell_value = 85 Then            
                     Row = 14            
                 ElseIf cell_value = 90 Then            
                     Row = 15            
                 ElseIf cell_value = 95 Then             
                     Row = 16            
                 ElseIf cell_value = 100 Then            
                     Row = 17            
                 End If            
' Check for height is 50            
            If Row = 7 And cell_value2 <= 34 Then
                Class = 4
            ElseIf cell_value2 <= 36.5 Then
                Class = 3
            ElseIf cell_value2 <= 39.3 Then
                Class = 2
            ElseIf cell_value2 <= 42.1 Then
                Class = 1
            ElseIf cell_value2 <= 44.6 Then
                Class = H1
            ElseIf cell_value2 <= 47.4 Then
                Class = H2
            Else: Class = NA
            End If            
' Check for height is 55            
            If Row = 8 And cell_value2 <= 35.4 Then
                Class = 4
            ElseIf cell_value2 <= 37.8 Then
                Class = 3
            ElseIf cell_value2 <= 40.7 Then
                Class = 2
            ElseIf cell_value2 <= 43.5 Then
                Class = 1
            ElseIf cell_value2 <= 46.4 Then
                Class = H1
            ElseIf cell_value2 <= 48.8 Then
                Class = H2
            Else: Class = NA
            End If            
' Check for height is 60            
            If Row = 9 And cell_value2 <= 36.3 Then
                Class = 4
            ElseIf cell_value2 <= 39.2 Then
                Class = 3
            ElseIf cell_value2 <= 42 Then
                Class = 2
            ElseIf cell_value2 <= 44.9 Then
                Class = 1
            ElseIf cell_value2 <= 47.7 Then
                Class = H1
            ElseIf cell_value2 <= 50.6 Then
                Class = H2
            Else: Class = NA
            End If                        
 ' Check for height is 65            
            If Row = 10 And cell_value2 <= 37.7 Then
                Class = 4
            ElseIf cell_value2 <= 40.5 Then
                Class = 3
            ElseIf cell_value2 <= 43.4 Then
                Class = 2
            ElseIf cell_value2 <= 46.3 Then
                Class = 1
            ElseIf cell_value2 <= 49.1 Then
                Class = H1
            ElseIf cell_value2 <= 52 Then
                Class = H2
            Else: Class = NA
            End If                       
' Check for height is 70            
            If Row = 11 And cell_value2 <= 38.6 Then
                Class = 4
            ElseIf cell_value2 <= 41.9 Then
                Class = 3
            ElseIf cell_value2 <= 44.8 Then
                Class = 2
            ElseIf cell_value2 <= 47.6 Then
                Class = 1
            ElseIf cell_value2 <= 50.5 Then
                Class = H1
            ElseIf cell_value2 <= 53.3 Then
                Class = H2
            Else: Class = NA
            End If            
' Check for height is 75            
            If Row = 12 And cell_value2 <= 42.8 Then
                Class = 3
            ElseIf cell_value2 <= 45.7 Then
                Class = 2
            ElseIf cell_value2 <= 49 Then
                Class = 1
            ElseIf cell_value2 <= 51.9 Then
                Class = H1
            ElseIf cell_value2 <= 55.1 Then
                Class = H2
            Else: Class = NA
            End If            
 ' Check for height is 80            
            If Row = 13 And cell_value2 <= 43.7568 Then
                Class = 3
            ElseIf cell_value2 <= 47.1 Then
                Class = 2
            ElseIf cell_value2 <= 50.4 Then
                Class = 1
            ElseIf cell_value2 <= 53.2 Then
                Class = H1
            ElseIf cell_value2 <= 56.1 Then
                Class = H2
            Else: Class = NA
            End If            
 ' Check for height is 85            
            If Row = 14 & cell_value2 <= 44.6772 Then
                Class = 3
            ElseIf cell_value2 <= 47.9778 Then
                Class = 2
            ElseIf cell_value2 <= 51.2785 Then
                Class = 1
            ElseIf cell_value2 <= 54.5791 Then
                Class = H1
            ElseIf cell_value2 <= 57.4462 Then
                Class = H2
            Else: Class = NA
            End If            
 ' Check for height is 90            
            If Row = 15 And cell_value2 <= 45.5952 Then
                Class = 3
            ElseIf cell_value2 <= 49.3333 Then
                Class = 2
            ElseIf cell_value2 <= 52.2024 Then
                Class = 1
            ElseIf cell_value2 <= 55.506 Then
                Class = H1
            ElseIf cell_value2 <= 58.8095 Then
                Class = H2
            Else: Class = NA
            End If            
  ' Check for height is 95            
            If Row = 16 And cell_value2 <= 50.4157 Then
                Class = 2
            ElseIf cell_value2 <= 53.2921 Then
                Class = 1
            ElseIf cell_value2 <= 57.0449 Then
                Class = H1
            ElseIf cell_value2 <= 60.3596 Then
                Class = H2
            Else: Class = NA
            End If            
   ' Check for height is 100            
            If Row = 16 And cell_value2 <= 51.4894 Then
                Class = 2
            ElseIf cell_value2 <= 54.8138 Then
                Class = 1
            ElseIf cell_value2 <= 58.1383 Then
                Class = H1
            ElseIf cell_value2 <= 61.4628 Then
                Class = H2
            Else: Class = NA
            End If            
        Cells(i, "AB").Value = Class

        Next i

     End Sub

0 个答案:

没有答案