簡體   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