我正在创建一个工具,从第二张(PARTS)上的零件清单中填充要审核的零件清单。这些部件在类(A,B或C)中,因此该工具使用随机数生成器来选择要检查零件类的行。当我尝试检查零件类时,我得到1004 runtime error
感谢一些帮助解决这个问题。这是给我一个错误的循环:
'While loop to obtain 6 A-Class part numbers to audit
Dim rand As Variant
Do While Acount <= 6
'Random number generator
rand = Int((6451 - 2 + 1) * Rnd + 2)
'checking part class ***If statement gives error***
If Sheets("PARTS").Cells(rand, 5).Value = "A" Then
Acount = Acount + 1
'audit list copies cell from parts list
Sheets("AUDIT POPULATOR").Cells(Acount, 1).Value = Sheets("PARTS").Cells(rand, 1).Value
Else
Acount = Acount
Sheets("AUDIT POPULATOR").Cells(Acount, 1).Value = ""
End If
Loop
答案 0 :(得分:0)
因为你没有初始化&#39; acount&#39;变量因此它是空的,因此你得到错误1004.我也建议使用&#39;选项显式&#39;声明以防止此类错误。这是你缺少部分的代码:
Option Explicit
Private Sub CommandButton1_Click()
'While loop to obtain 6 A-Class part numbers to audit
Dim rand As Variant
Dim Acount As Integer
Acount = 1 ' 1 or whatever number you would like to get start...
Do While Acount <= 6
'Random number generator
rand = Int((6451 - 2 + 1) * Rnd + 2)
'checking part class ***If statement gives error***
If Sheets("PARTS").Cells(rand, 5).Value = "A" Then
Acount = Acount + 1
'audit list copies cell from parts list
Sheets("AUDIT POPULATOR").Cells(Acount, 1).Value = Sheets("PARTS").Cells(rand, 1).Value
Else
Acount = Acount
Sheets("AUDIT POPULATOR").Cells(Acount, 1).Value = ""
End If
Loop
End Sub