这是我尝试使用的代码,但它并不常用。因为我还在学习VBA,所以我不确定自己做错了什么。任何人都可以帮助我实现这个目标吗?
Sub BringDataToMasterSheet()
Dim WB As ThisWorkbook
Dim x As WorkSheet
Dim y As WorkSheet
Dim z As WorkSheet
'## Open Workbooks first:
Set x = Worksheet.Open("Delinquent Promises Detail")
Set y = Worksheet.Open("Delinquent Shipments Detail")
Set z = Worksheet.Open("Master")
'Now, copy what you want from x:
x = Worksheet("Delinquent Promises Detail").Range("A2").lastrow.Copy
z = Worksheet("Master").Range("A").lastrow.PasteSpecial
y = Worksheet("Delinquent Shipments Detail").Range("A2").lastrow.Copy
z = Worksheet("Master").Range("A").lastrow.PasteSpecial
End Sub
答案 0 :(得分:1)
您无法打开工作表。您打开工作簿,似乎这些工作簿位于ThisWorkbook
。您无法将工作表定义为值(复制或粘贴)。当您将x
,y
和z
声明为Worksheet
时,您需要去定义范围。但是,当您复制/粘贴它们时,不需要该声明,您可以直接执行此操作。见下文:
Sub BringDataToMasterSheet()
Dim WB As ThisWorkbook
Dim x As WorkSheet
Dim y As WorkSheet
Dim z As WorkSheet
'## Set worksheets first: (You don't open worksheets)
Set x = Worksheets("Delinquent Promises Detail")
Set y = Worksheets("Delinquent Shipments Detail")
Set z = Worksheets("Master")
'Now, copy what you want:
x.Range("A2").End(xlDown).Copy
z.Range("A2").End(xlDown).Offset(1,0).PasteSpecial xlPasteAll
y.Range("A2").End(xlDown).Copy
z.Range("A2").End(xlDown).Offset(1,0).PasteSpecial xlPasteAll
End Sub
以下是您可以使用的一些行,通过打开工作表或复制范围,同时将其设置为某些内容来确定您的意思:
这会打开一个工作簿:
Set myWB = Workbooks.Open("Path/to/the/file.xlsx")
这在工作簿中设置了一个工作表:
Set xyz = myWB.Worksheets("Sheet1")
设置范围:
Dim myRange As Range
Set myRange = xyz.Range("A2")
此复制该范围并将其粘贴到同一工作表中的其他位置(原始范围的边框除外):
myRange.Copy
xyz.Range("H50").PasteSpeciall xlPasteAllExceptBorders