我正在研究为什么我在过去几个小时内无法设置我的工作簿。我要做的是浏览文件夹中的每个电子表格以刷新数据并使用Vlookup
在Set Book2 = workbooks("InfoPedia Page Views (ASG Compete)")
导入它们,这是我收到错误时
运行时错误'9':下标超出范围
如果有人能说出我的代码无效的原因,我真的很感激。
enter code here
Sub Test()
Dim Book2 As Workbook
Set fso = CreateObject("Scripting.FileSystemObject")
Set xl = CreateObject("Excel.Application")
xl.Visible = True
For Each f In fso.getfolder("C:\Users\v-gazhan\Desktop\Infopedia Quality Project\Infopedia Quality Dashboard\Page Views").Files
If LCase(fso.GetextensionName(f.Name)) = "xlsx" Then
Set wb = xl.workbooks.Open(f.Path)
' Set the correct month
wb.SlicerCaches("Slicer_Month_Name").VisibleSlicerItemsList = Array _
( _
"[Time].[Month Name].&[10]")
With Application.WorksheetFunction
If fso.getbasename(f.Name) = "InfoPedia Page Views (ASG Compete)" Then
***Set Book2 = workbooks("InfoPedia Page Views (ASG Compete)")***
Set SearchRange = Book2.Sheets("Page Views Sorted Most to Least").Range("B:D")
ActiveWorkbook.Sheets("Reference").Cells(1, 4).Value = .VLookup(Sheets("reference").Range("B1"), SearchRange, 3, False)
End If
End With
wb.Close
End If
Next
xl.Qu
End Sub