我想制作一个从我通过电子邮件收到的打开的工作簿中复制数据的宏。工作簿名称始终以“打开订单监控”[...]开头,以变量结束。我希望宏识别前三个单词,然后选择要从中复制的正确的打开工作簿。我想从数据被粘贴的工作簿中运行宏:
Sub CopyData()
Dim Wb1 As Workbook, wb2 As Workbook
'Copy from open workbook that always start with the name
'Open Order Monitoring[...]'. I need a variable
Set Wb1 = XXXXXXXX 'needs a variable here I think
'Paste into the Workbook I run the macro from (aka ThisWorkbook??)
Set Wb2 = ThisWorkbook
'Copy Data from Wb1.Sheet1 to Wb2.sheet2
Wb1.Sheets(1).Range("A2").Range(.Cells(1, 1),
.End(xlDown).Cells(1, 39)).Copy wb2.Sheets(2).Range("B5")
End Sub
我对VBA不太了解......
tl; dr - 从一个打开的工作簿(带有变量名)复制到另一个打开的工作簿(我从中运行宏)
答案 0 :(得分:1)
查看打开的工作簿并选择符合您要求的工作簿:
For Each wB in Application.Workbooks
If Left(wB.Name, 21) = "Open Order Monitoring" Then
Set Wb1 = wB
Exit For
End If
Next
这将为您提供以该名称开头的第一个打开的工作簿,并为您设置为Wb1。