Excel 工作簿设置为不可见时的奇怪行为

Excel weird behavior when workbook set to not visible

我在尝试 excel 填写模板表单中的某些字段然后 copy/paste 在另一个 WorkSheet 上填写时遇到了最奇怪的经历。然后它必须重复这个过程有限的次数,总是把表格粘贴到之前的表格下面。当页面已满时,它必须复制第 1 页的页眉并将其粘贴到下一页的顶部,然后进行表单 copy/paste 过程。

这个例程是我目前正在(尝试)开发的 VB6 小计算软件的一部分。

问题是:excel无法复制我想要的数据并将其粘贴到第二个作品中的正确位置sheet。有时它根本不复制数据,当我将其导出为 pdf 时,我得到一个空白 sheet;有时它会复制所有内容,但会将填写的模板表单粘贴到错误的单元格中。

奇怪的行为:仅当 Excel 工作簿设置为不可见时才会发生。如果我暂停执行并将其设置为可见,例程运行得很好,最后我得到一份完美的 pdf 报告。

对发生这种情况的原因有任何想法and/or 解决方案吗?

提前致谢。

我认为这可能是由于 VB6 exe 处理速度太快 excel 无法处理,所以我在代码行之间添加了几个 "Sleep 1000" 希望 excel能够赶上。没用。

    With xlsm.Worksheets("Template Cabos")
        .Activate
        Sleep 1000
        ActiveSheet.Range(Cells(1, 1), Cells(5, 36 + ONS)).Copy 'Copies the filled in form
        Sleep 1000
    End With

    With xlsm.Worksheets("Mem. Cabos")
        .Activate
        Cells(lin, 1).Select
        .Paste                         'Pastes it in the report workbook
        lin = lin + 6

        If (lin + 6) > (pag * 33) Then          'Checks if the next form would fit into the page    Checa se cabe outra ficha na página
            lin = pag * 33 + 1                  'If not, the marker "lin" goes to the top of page 2   Se não couber, o marcador "lin" vai pro topo da pág. 2
            .Range("A1:AJ5").Copy               'Copies the header of page 1                        Copia o cabeçalho da pág. 1
            Cells(lin, 1).Select
            .Paste                              'Pastes on the top of page 2                        Cola no topo da pág. 2
            pag = pag + 1                       'Acknowledges that now the report has one more page       Sinaliza que agora o relatório tem 2 páginas
            Cells(lin + 4, 35).Value = pag      'Types "2" in the field "page" of last page's header   Escreve "2" no campo "Pág." do cabeçalho da pág. 2
            lin = lin + 7                       'Moves the marker 7 line down to receive the next form    Move o marcador 7 linhas abaixo para receber a próx. ficha

        End If
    End With

以这种方式复制和粘贴时,您可能会在程序运行时不小心清除剪贴板运行,或者您可能手动(单击鼠标)更改活动作品sheet。将数据从 sheet 复制到 sheet 时,这可能会导致所有类型的问题。

更好的方法是直接引用 sheet。您不必担心正确的 sheet 处于活动状态或以意想不到的方式操纵剪贴板。这是您的第一个 copy/paste 工作原理的示例。

Sheets("Mem. Cabos").Cells(lin, 1).Value = Sheets("Template Cabos").Range(Cells(1, 1), Cells(5, 36 + ONS)).Value
 With xlsm.Worksheets("Template Cabos")
    .Range(.Cells(1, 1), .Cells(5, 36 + ONS)).Copy _
           destination:=xlsm.Worksheets("Mem. Cabos"). Cells(lin, 1)
  End With    


With xlsm.Worksheets("Mem. Cabos")


    lin = lin + 6

    If (lin + 6) > (pag * 33) Then  
        lin = pag * 33 + 1                 
        .Range("A1:AJ5").Copy .Cells(lin, 1)
        pag = pag + 1                      
        Cells(lin + 4, 35).Value = pag    
        lin = lin + 7                       

    End If
End With

看完@cybernetic.nomad提供的link,我尝试了:

    For 'paremeters

    'Code lines to fill in the template forms

    Dim tempRng As Range
    Dim repRng  As Range
    Dim xTemplate As Worksheet
    Dim xReport As Worksheet

    Set xTemplate = xlsm.Worksheets("Template Cabos")
    Set xReport = xlsm.Worksheets("Mem. Cabos")

    Set tempRng = xTemplate.Range(Cells(1, 1), Cells(5, 36 + ONS))
    Set repRng = xReport.Cells(lin, 1)

    tempRng.Copy repRng
    lin = lin + 6

    If (lin + 6) > (pag * 33) Then                                  'Checks if the next form would fit into the page    Checa se cabe outra ficha na página
        lin = pag * 33 + 1                                          'If not, the marker "lin" goes to the top of page 2   Se não couber, o marcador "lin" vai pro topo da pág. 2
        Set tempRng = xReport.Range("A1:AJ5")
        Set repRng = xReport.Cells(lin, 1)
        tempRng.Copy repRng                                         'Copies the header of page 1                        Copia o cabeçalho da pág. 1
                                                                    'Pastes on the top of page 2                        Cola no topo da pág. 2

        pag = pag + 1                       'Acknowledges that now the report has one more page       Sinaliza que agora o relatório tem 2 páginas
        xReport.Cells(lin + 4, 35).Value = pag      'Types "2" in the field "page" of last page's header   Escreve "2" no campo "Pág." do cabeçalho da pág. 2
        lin = lin + 7                       'Moves the marker 7 line down to receive the next form    Move o marcador 7 linhas abaixo para receber a próx. ficha

    End If


    PleaseWait.ProgressBar1.Value = PleaseWait.ProgressBar1.Value + Round((60 / xlsm.Worksheets("Cabos").Range("B40").Value), 0)

  Next

    Set tempRng = xTemplate.Range("A8:G13")             'Escreve a tabela de temperaturas admissíveis
    Set repRng = xReport.Cells(lin, 1)
    tempRng.Copy repRng

收到错误:

运行-时间错误1004: 对象 "Worksheet_" 的方法 "Range" 失败