我正在为一个文件创建一个宏,该文件将分发给一群人;该函数应该能够从不同的单元格中提取人名(在变量B中),在具有多个工作表的另一个工作簿中搜索该值(变量X),如果找到则复制特定范围的单元格从工作簿X到工作簿B。
我遇到以下代码时出现问题:
Sub Pull_data_Click()
Dim A As Variant 'defines name from first subroutine
Dim B As Workbook 'defines destination file
Dim X As Workbook 'defines existing report file as source
Dim Destination As Range 'defines destination range of data pulled from report
Dim wb As Workbook
Dim ws As Worksheet
Dim rng As Range
A = Workbooks("filenameB.xlsm").Worksheets("Summary").Range("A1").Value 'constant variable, does not change
Set B = Workbooks("filenameB.xlsm") 'constant variable, does not change
Set X = Workbooks.Open("filenameX.xlsm") 'dependent variable, new name for each new report
Set Destination = Workbooks("filenameB.xlsm").Worksheets("Input").Range("B2:S2") 'Range changes for each iteration, rows increase by 1
'check if name is entered
If A = "" Then
MsgBox ("Your name is not visible; please start from the Reference tab.")
B.Worksheets("Reference").Activate
Exit Sub
End If
With X.Worksheets
For Each ws In X.Worksheets
Set rng = Cells.Find(What:=A, After:=ActiveCell, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
ActiveCell.Activate
ActiveSheet.Range("$A$2:$DQ$11").AutoFilter Field:=1, Criteria1:=A
Range("A7:CD7").Select
Selection.Copy
B.Activate
Destination.Activate
Destination.PasteSpecial Paste:=xlPasteValues
Next ws
End With
Application.ScreenUpdating = False
End Sub
它能够成功编译并且没有运行时错误,并且当它运行时它似乎正在循环通过工作表...但它正在粘贴错误的信息。这件事我有什么设置不正确吗?