我正在创建一个工具,该工具从第二个工作表(零件)上的零件列表中填充要审核的零件列表。部件位于类(A、B或C)中,因此该工具使用随机数生成器来选择要在其中检入部件类的行。当我尝试检查part类时,我得到了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发布于 2015-08-13 03:44:49
因为你没有初始化'acount‘变量,所以它是空的,所以你会得到错误1004。此外,我还建议使用“option explicit”声明来防止此类错误。以下是您的代码中缺少的部分:
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 Subhttps://stackoverflow.com/questions/31973755
复制相似问题