excel - 从打开的工作簿的指定工作表复制数据并将其粘贴到另一个指定的关闭工作簿工作表
问题描述
我想将数据从打开的工作簿中的指定工作表复制到关闭的工作簿中的另一个指定工作表。
我有这样的代码:
Private Sub CommandButton1_Click()
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
'Set variables for copy and destination sheets
Set wsCopy = Workbooks("Form Marketing Calendar1.xlsm").Worksheets("Form Single")
Set wsDest = Workbooks.Open("Database Marketing Calendar.xlsx").Worksheets("Sheet1")
'2. Find first blank row in the destination range based on data in column A
'Offset property moves down 1 row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "B").End(xlUp).Offset(1).Row
'3. Copy & Paste Data
wsCopy.Range("B22:AF25").Copy
wsDest.Range("B" & lDestLastRow).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
wsCopy.Range("B26:AF30").Copy
wsDest.Range("G" & lDestLastRow).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Selection.ClearContents
wsDest.Parent.Close True
End Sub
实际上,它复制了数据,但它必须先打开目标文件然后自动关闭它。
它也只复制第一个数据(this: wsCopy.Range("B22:AF25").Copy
),但第二个数据没有复制(that: wsCopy.Range("B26:AF30").Copy
)。
解决方案
这里:
Option Explicit
Private Sub CommandButton1_Click()
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRowB As Long
Dim lDestLastRowG As Long
'It is not possible to get data from a workbook without openning it, but this is the closest
With Application
.ScreenUpdating = False 'won't show any changes on your screen
.Visible = Flase 'will hide Excel. CAUTION: if the macro fails make sure to change this back to True
End With
'Set variables for copy and destination sheets
Set wsCopy = Workbooks("Form Marketing Calendar1.xlsm").Worksheets("Form Single")
Set wsDest = Workbooks.Open("Database Marketing Calendar.xlsx").Worksheets("Sheet1")
'2. Find first blank row in the destination range based on data in column A
'Offset property moves down 1 row
With wsDest 'with allows you to refer to the variable without writting it
lDestLastRowB = .Cells(.Rows.Count, "B").End(xlUp).Row + 1 'You can use also +1 since it's a number
'you are not copying the same amount of rows, so maybe the data won't be on the same row on both columns
lDestLastRowG = .Cells(.Rows.Count, "G").End(xlUp).Row + 1
End With
'3. Copy & Paste Data
wsCopy.Range("B22:AF25").Copy
wsDest.Range("B" & lDestLastRowB).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
wsCopy.Range("B26:AF30").Copy
wsDest.Range("G" & lDestLastRowG).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
'when you manually copy and paste you will see that the paste range gets selected. This line erases selected content so this is deleting it
'Selection.ClearContents
wsDest.Parent.Close True
With Application
.ScreenUpdating = True
.Visible = True
End With
End Sub
推荐阅读
- paypal - Paypal API - checkout.js - 解析响应错误
- c# - 在一个 Json 中创建多个表而不使用 JArray?
- reactjs - React-select multi 无法按照文档中定义的方式工作
- entity-framework - Cosmos.Sql:在类型'Newtonsoft.Json.Linq.JToken'和'Status'(EnumType)之间没有定义强制运算符
- deployment - 使用 GitLab CI 使发布分支的 DEPLOY 作业自动执行,其他人手动执行
- cocoa - 如何在边界更改时为 NSCollectionViewLayout 的重新布局设置动画?
- cordova - 无法使用 Ionic 4 创建新项目:下载集成 cordova - 失败
- php - PHP CodeIgniter:成功的 ion-auth 登录刷新、注销并重定向到登录
- c# - 在 Docker 中获取文件
- mysql - 从数据库中返回第 N 个单词