我知道这个问题在互联网上有无数的变体。但我要说的是,没有任何答案对我有帮助。我正在尝试将此数据库从一个工作簿复制到另一个工作簿。
我想在最后一个填充行之后立即粘贴这些数据。按照图像更好地可视化我的问题:
Esta é a minha tabela no workbook 1.
这是我在工作簿 2 中的表格,我在其中随机填充了一些单元格作为示例,以执行复制和粘贴测试。
同样,我的目的是将数据从工作簿 1 传递到工作簿 2,并立即将其粘贴在最后一行内容之后。
我已经尝试过了。但它给出错误“1004:应用程序定义或对象定义错误”。我是vba新手。所以不完全是我哪里出错了。
另请注意,在我的代码中,我正在重新排列新工作簿中的列,我可以做些什么来使其更有条理吗?
Sub copying()
Application.ScreenUpdating = False
Workbooks.Open "C:\Users\est_acpinheiro\Desktop\pypdf\table.xlsx"
Workbooks.Open "C:\Users\est_acpinheiro\Desktop\pypdf\exporting.xlsx"
Workbooks("table.xlsx").Worksheets("Sheet1").Range("A:A").Copy _
Workbooks("exporting.xlsx").Worksheets("Plan1").Range("E:E" & Rows.Count).End(xlUp).Offset(1, 0) 'A-E
Workbooks("table.xlsx").Worksheets("Sheet1").Range("B:B").Copy _
Workbooks("exporting.xlsx").Worksheets("Plan1").Range("G:G" & Rows.Count).End(xlUp).Offset(1, 0) 'B-G
Workbooks("table.xlsx").Worksheets("Sheet1").Range("C:C").Copy _
Workbooks("exporting.xlsx").Worksheets("Plan1").Range("A:A" & Rows.Count).End(xlUp).Offset(1, 0) 'C-A
Workbooks("table.xlsx").Worksheets("Sheet1").Range("D:D").Copy _
Workbooks("exporting.xlsx").Worksheets("Plan1").Range("C:C" & Rows.Count).End(xlUp).Offset(1, 0) 'D-C
Workbooks("table.xlsx").Worksheets("Sheet1").Range("E:E").Copy _
Workbooks("exporting.xlsx").Worksheets("Plan1").Range("B:B" & Rows.Count).End(xlUp).Offset(1, 0) 'E-B
Workbooks("table.xlsx").Worksheets("Sheet1").Range("F:F").Copy _
Workbooks("exporting.xlsx").Worksheets("Plan1").Range("I:I" & Rows.Count).End(xlUp).Offset(1, 0) 'F-I
Workbooks("table.xlsx").Worksheets("Sheet1").Range("G:G").Copy _
Workbooks("exporting.xlsx").Worksheets("Plan1").Range("K:K" & Rows.Count).End(xlUp).Offset(1, 0) 'G-K
Workbooks("table.xlsx").Worksheets("Sheet1").Range("I:I").Copy _
Workbooks("exporting.xlsx").Worksheets("Plan1").Range("H:H" & Rows.Count).End(xlUp).Offset(1, 0) 'I-H
Application.CutCopyMode = False
End Sub
这是我目前能为您提供的最有组织/最干净的,但是有一些假设:
Sheet1中没有页眉,否则需要从A2开始,而不是A1
您想保留格式/等。来自 Sheet1,否则您不需要
.Copy
但只想设置值(也更快,但数字上的前导零(作为文本)会丢失)
您的 A 列通常始终有一个值,因此我们可以以此为基础来确定最后一行
子复制()
Dim wbS As Workbook, wbD As Workbook, wsS As Worksheet, wsD As Worksheet
Dim lRowS As Long, lRowD As Long
Application.ScreenUpdating = False
Set wbS = Workbooks.Open("C:\Users\est_acpinheiro\Desktop\pypdf\table.xlsx")
Set wsS = wbS.Worksheets("Sheet1")
lRowS = wsS.Range("A" & Rows.Count).End(xlUp).Row 'assuming your A-column is usually always filled
Set wbD = Workbooks.Open("C:\Users\est_acpinheiro\Desktop\pypdf\exporting.xlsx")
Set wsD = wbD.Worksheets("Plan1")
lRowD = wsD.Range("A" & Rows.Count).End(xlUp).Row + 1
Dim arrS: arrS = Array("A", "B", "C", "D", "E", "F", "G", "I")
Dim arrD: arrD = Array("E", "G", "A", "C", "B", "I", "K", "H")
Dim i As Long
For i = 0 To UBound(arrS, 1)
wsS.Range(arrS(i) & "1:" & arrS(i) & lRowS).Copy wsD.Range(arrD(i) & lRowD) 'I guess you don't have headings?
Next i
Application.CutCopyMode = False
Application.ScreenUpdating = True
结束子