excel - 如何打开工作簿、复制数据并添加到另一个工作簿的最后一行?
问题描述
是否可以从当前工作簿打开另一个工作簿并将表格列上的数据复制并粘贴到当前工作簿中表格的最后一行?并删除源数据。
解决方案
调整第 5 个变量并尝试以下代码:
Option Explicit
Sub test()
Application.DisplayAlerts = False
Dim wb As Workbook
Set wb = ThisWorkbook
'*******************************************
'Adapt this vars
Dim path_wbToOpen As String
Dim myRange As String
Dim sheet_opened As String
Dim ws_final As Worksheet
Dim lastRow As Integer
path_wbToOpen = "C:\test\test.xlsx" 'path of workbook where is the data
myRange = "A1:D1" 'Range to cpy
sheet_opened = "sheet_opened" 'name in opened WB where is sheet data
Set ws_final = wb.Sheets("sh_test") 'Sheet in current WB to paste data
lastRow = ws_final.Range("A" & Rows.Count).End(xlUp).Row + 1 ' set the last row (adapt column to check last row)
'*******************************************
'open another workbook from current one
Dim wbToOpen As Workbook
Set wbToOpen = Workbooks.Open(path_wbToOpen)
'Copy Data from opened WB
wbToOpen.Sheets(sheet_opened).Range(myRange).Copy 'Copy the data
'current WB
ws_final.Range("a" & lastRow).PasteSpecial ' past to the last row
'Clear Data and close WB
wbToOpen.Sheets(sheet_opened).Range(myRange).Clear 'Clear Data
wbToOpen.Close 'close WB
Application.DisplayAlerts = True
End Sub
推荐阅读
- linux - 将 wilc3000 驱动程序添加到 Yocto 树莓派
- javascript - Webpack中vendor.js和application.js的关系
- sql - 使用列名和计数
- java - Tomcat 在 Eclipse 中无法正常工作
- swift - UITextView 委托方法未使用 swift 在 UITableViewCell 中调用
- swift - Swift 中的闭包:它们是什么?他们是如何工作的?
- kotlin - 在 Kotlin 中,是否有一种安全的方法来执行 ObjectInputStream.readObject?
- python - Python : "TypeError","evalue":"'list' 对象不可调用"
- python - 如何将 Selenium 点击元素与美丽的汤一起使用
- c++ - CMake 3.16.5 未添加系统包含目录