简体   繁体   English

复制和粘贴 Excel 工作表

[英]Copy and paste Excel sheets

Private Sub CommandButton1_Click()
Dim ws As Worksheet
    With Application.FileDialog(msoFileDialogFilePicker)
       .Show
       If .SelectedItems.Count <> 0 Then
       fldr = .SelectedItems(1)
       End If
    End With
    Sheets.link.Value = fldr
    For i = 1 To Worksheets.Count
        Set ws = Worksheets(i)
        If ws.Cells(2, 1) = "X" Then
            Sheets.ComboBox1.AddItem (ws.Name)
        End If
    Next i
    Workbooks.Open (fldr)
    Sheets.Show
End Sub

Private Sub Add_Click()

Dim x As String
Dim ws As Workbook

x = Right(link.Value, (Len(link.Value) - InStrRev(link.Value, "ild") - 3))

Workbooks("Test.xlsm").Activate

Worksheets(ComboBox1.Value).Copy Before:=Workbooks(x).Worksheets("Contract")

End Sub

So the basic idea is, you click a button on an Excel sheet.所以基本的想法是,您单击 Excel 工作表上的一个按钮。 The user then finds the file they want to copy the sheets to.然后用户找到他们想要将工作表复制到的文件。 It will find all of a specific type of sheet, put it in a forms combobox and open the selected Excel file.它将找到所有特定类型的工作表,将其放入表单组合框中并打开选定的 Excel 文件。 Then you choose a sheet from the combobox and copy it from one workbook to the other.然后从组合框中选择一张工作表并将其从一个工作簿复制到另一个工作簿。 It all works until the copying part.这一切都有效,直到复制部分。 I get a long error:我收到一个很长的错误:

Excel cannot insert the sheets into the destination workbook, because it contains fewer rows and columns that the source workbook. Excel 无法将工作表插入到目标工作簿中,因为它包含的行和列少于源工作簿。 To move or copy the data to the destination workbook, you can select the data and then use Copy and Paste commands to insert it into the sheets of another workbook.要将数据移动或复制到目标工作簿,您可以选择数据,然后使用复制和粘贴命令将其插入到另一个工作簿的工作表中。

If the destination workbook comes from an older version of Excel (extension .xls for instance, Excel 97 or Excel 2003), the limit of number of rows in old worksheets is 2^16-1, as the row number is encoded on 16 bits.如果目标工作簿来自旧版本的 Excel(例如,扩展名为 .xls,Excel 97 或 Excel 2003),则旧工作表中的行数限制为 2^16-1,因为行号以 16 位编码. In newer versions, this number is encoded on 32 bits.在较新的版本中,此数字以 32 位编码。

Hence, copying a worksheet "as a whole" from a newer version into a workbook from an older version raises this error.因此,将工作表从新版本“作为一个整体”复制到旧版本的工作簿中会引发此错误。 From my test, this error occurs even if the actually used range in the copied worksheet is small.根据我的测试,即使复制的工作表中实际使用的范围很小,也会发生此错误。

I had this same problem.我有同样的问题。 Following @ASG suggestion, I saved the old workbook with the new file format (xlsx), closed and reopened it and everything worked fine afterwards.按照@ASG 的建议,我使用新文件格式 (xlsx) 保存旧工作簿,关闭并重新打开它,之后一切正常。 Hope it helps.希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM