Excel VBA 需要的原因 Wb.activate
Excel VBA Reason for needing Wb.activate
我有一个包含多个文件的文件夹。我也有一份主文件。我想将每个文件的值分配给主文件中的一个范围。例如,如果文件在 A1 中包含单词“Chocolate”,我希望它在 C1:C5.
的 Masterfile 中出现 5 次
我注意到如果我不使用 Workbook.Activate 函数,我的代码将不起作用,如果我使用 Range(Cells(),Cells()) 格式在 Masterfile 中指定范围复制到。我想知道是否有人可以给我一个理由,因为我听说人们应该尽可能远离这个功能。
这是我的代码:
Sub AddDataToMasterfile()
Dim wb As Workbook 'Workbook from which I want to copy
Dim masterWb As Workbook 'Masterfile to which I want to paste
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
Set masterWb = ThisWorkbook 'macro will be stored in module in Masterfile
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo Done
'Target File Extension (must include wildcard "*")
myExtension = "*.xls*"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook from which to copy
Set wb = Workbooks.Open(Filename:=myPath & myFile)
'Set value in Masterfile range equal to value in one cell in wb
masterWb.Activate 'error if I do not include this and use cells range format as below
masterWb.Worksheets("Federer").Range(Cells(1, 1), Cells(5, 1)).Value = wb.Worksheets(1).Range("A1")
'works if I change Range format to "A1:A5", regardless of 'activate' function above
'Close Workbook
wb.Close SaveChanges:=False
'Get next file name
myFile = Dir
Loop
Done:
MsgBox "Import complete"
End Sub
我知道在我的示例中每个循环都会覆盖我的 Masterfile 中的当前值,但与我关于 Workbook.Activate
的问题无关
您的 Cells
引用不完全合格,因此它们假定为活动 sheet,这就是为什么您需要那行额外的代码。
您应该可以安全地将其更改为这个并删除 Activate
行。
With masterWb.Worksheets("Federer")
.Range(.Cells(1, 1), .Cells(5, 1)).Value = wb.Worksheets(1).Range("A1")
End With
注意:Range
中的 Cells
引用不会自动假定您想要引用相同的作品sheet,即使逻辑上看起来是这样应该。
我有一个包含多个文件的文件夹。我也有一份主文件。我想将每个文件的值分配给主文件中的一个范围。例如,如果文件在 A1 中包含单词“Chocolate”,我希望它在 C1:C5.
的 Masterfile 中出现 5 次我注意到如果我不使用 Workbook.Activate 函数,我的代码将不起作用,如果我使用 Range(Cells(),Cells()) 格式在 Masterfile 中指定范围复制到。我想知道是否有人可以给我一个理由,因为我听说人们应该尽可能远离这个功能。
这是我的代码:
Sub AddDataToMasterfile()
Dim wb As Workbook 'Workbook from which I want to copy
Dim masterWb As Workbook 'Masterfile to which I want to paste
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
Set masterWb = ThisWorkbook 'macro will be stored in module in Masterfile
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo Done
'Target File Extension (must include wildcard "*")
myExtension = "*.xls*"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook from which to copy
Set wb = Workbooks.Open(Filename:=myPath & myFile)
'Set value in Masterfile range equal to value in one cell in wb
masterWb.Activate 'error if I do not include this and use cells range format as below
masterWb.Worksheets("Federer").Range(Cells(1, 1), Cells(5, 1)).Value = wb.Worksheets(1).Range("A1")
'works if I change Range format to "A1:A5", regardless of 'activate' function above
'Close Workbook
wb.Close SaveChanges:=False
'Get next file name
myFile = Dir
Loop
Done:
MsgBox "Import complete"
End Sub
我知道在我的示例中每个循环都会覆盖我的 Masterfile 中的当前值,但与我关于 Workbook.Activate
的问题无关您的 Cells
引用不完全合格,因此它们假定为活动 sheet,这就是为什么您需要那行额外的代码。
您应该可以安全地将其更改为这个并删除 Activate
行。
With masterWb.Worksheets("Federer")
.Range(.Cells(1, 1), .Cells(5, 1)).Value = wb.Worksheets(1).Range("A1")
End With
注意:Range
中的 Cells
引用不会自动假定您想要引用相同的作品sheet,即使逻辑上看起来是这样应该。