[我在运行vba时出现运行时错误1004 [重复]

问题描述 投票:0回答:2

我真的很擅长,并且遇到了一些问题。我正试图创建一个考勤表,出于安全原因,该表会全天定期更新。在具有所有可能名称的工作表上,我有一列用于不同公司,名称,营地,房间和现场的信息。我已经编写了我的代码,因此,如果有人在现场,则在“现场”列中会出现一个1,如果不在现场,则会出现一个0。当出现1时,我希望将其名称和所有其他信息传送到考勤表,以便仅出现的名称是现场的名称。如果他们在现场,我希望空间留为空白。我的代码有两个问题:

Sub onsite()

    x = 3   'start at row 3


    'start the loop
    Do While Cells(x, 6) <> ""

        'look for data with '1'
        If Cells(x, 6) = "1" Then

        'copy the row if it contains '1'
        Worksheets("Sheet1").Rows(x).Copy

        'go to main ERP.  activate it
        Worksheets("Sheet2").Activate

        **erow = Sheet2.Cells(Rows.Count, 6).End(x1Up).Offset(1, 0).Row**
        'paste data

         '**ERROR OCCURS HERE**
         ActiveSheet.Paste Destination:=Worksheets("Sheet2").Rows(erow)

         End If

         'go to all names and activate
         Worksheets("AllNames").Activate

         'loop through the other rows
         x = x + 1

    Loop

End Sub

第一个问题是,当我到达粗体行时,我收到一条错误“ 1004”消息,并且代码停止工作

另一个问题是,当一个人的站点列中的数字为0时,我不知道如何将'erow ='更改为跳过行的代码

请帮助!

vba
2个回答
1
投票
Sub onsite()
    Dim x as long, erow as Long
    Dim shtSrc as Worksheet, shtDest as worksheet

    Set shtSrc = Worksheets("Sheet1")
    Set shtDest = Worksheets("Sheet2")
    erow = shtDest.Cells(rows.count, 6).End(xlUp).Row+1

    x = 3       

    Do While shtSrc.Cells(x, 6) <> ""

        If shtSrc.Cells(x, 6) = "1" Then

            shtSrc.Rows(x).Copy shtDest.cells(erow,1)
            erow = erow+1

        End If
        x = x + 1

    Loop

End Sub

0
投票

[This MS Knowledgebase article描述错误的原因和解决方法。

基本上,以编程方式复制和粘贴整个行时会出现问题。您只需要选择该行中实际使用的单元格即可。

© www.soinside.com 2019 - 2024. All rights reserved.