繁体   English   中英

在VBA中填充不同工作表中的单元格

[英]Populating cells from a different sheet in VBA

我正在创建一个工具,该工具可从第二张图纸(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

因为您没有初始化'acount'变量,所以它为空,所以会出现错误1004。我也建议您使用'optionexplicit'声明来防止此类错误。 这是缺少部分的代码:

    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

暂无
暂无

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

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