首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在VBA中填充来自不同工作表的单元格

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

Stack Overflow用户
提问于 2015-08-13 03:25:05
回答 1查看 69关注 0票数 1

我正在创建一个工具,该工具从第二个工作表(零件)上的零件列表中填充要审核的零件列表。部件位于类(A、B或C)中,因此该工具使用随机数生成器来选择要在其中检入部件类的行。当我尝试检查part类时,我得到了1004 runtime error,希望能得到一些帮助来解决这个问题。下面是给我一个错误的循环:

代码语言:javascript
运行
复制
'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
EN

回答 1

Stack Overflow用户

发布于 2015-08-13 03:44:49

因为你没有初始化'acount‘变量,所以它是空的,所以你会得到错误1004。此外,我还建议使用“option explicit”声明来防止此类错误。以下是您的代码中缺少的部分:

代码语言:javascript
运行
复制
    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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31973755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档