【问题标题】:Excel weird behavior when workbook set to not visible工作簿设置为不可见时 Excel 的奇怪行为
【发布时间】:2019-08-29 14:27:59
【问题描述】:

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

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

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

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

对发生这种情况的原因有什么想法和/或解决方法吗?

提前致谢。

我认为这可能是由于 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

【问题讨论】:

  • 一般来说,你想在你的代码中avoid using activate and select
  • 但如果不选择单元格,我将无法将内容粘贴到其中。 .Cells(lin,1).paste 出现错误
  • 我刚刚意识到您在评论中给了我一个学习链接。这似乎非常有用。如果运气好的话,我会查一下,然后回来。谢谢。

标签: excel vb6


【解决方案1】:
 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

【讨论】:

    【解决方案2】:

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

    更好的方法是直接引用工作表。您不必担心正确的工作表处于活动状态或剪贴板以意想不到的方式被操纵。这是您的第一次复制/粘贴如何工作的示例。

    Sheets("Mem. Cabos").Cells(lin, 1).Value = Sheets("Template Cabos").Range(Cells(1, 1), Cells(5, 36 + ONS)).Value
    

    【讨论】:

      【解决方案3】:

      看了@cybernetic.nomad 提供的链接后,我尝试了:

          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_”的方法“范围”失败

      【讨论】:

        猜你喜欢
        • 2019-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多