我尝试使用WorksheetFunctions而不是Excel公式运行索引/匹配查询。
该函数为exernal工作簿(extwbk)中的arrea编制索引,并将extwbk列A中的商店编号与ActiveWorkbook中的存储编号col A和extwbk的第1行中的月份(sCurrMth)匹配。 Match Store#&Aug to come back with store accounting period in fin year
我声明了变量,设置了一些值,但我得到了一个'运行时错误' -2147221080(800401a8)自动化错误'当我到达
使用fNameAndPath.Worksheets(" Sheet1")
当我从fNameAndPath.Worksheets(" Sheet1")更改为ActiveWorkbook.ActiveSheet时,我收到运行时错误' 424'不同行上需要的对象
.Cells(rw,10)= Application.WorksheetFunction.Index(x,Application.WorksheetFunction.Match(fNameAndPath.Range(" A2"),extwbk.Worksheets(" Info Sheet&# 34;)。范围(" A1:A"& Rows.Count),0),_ Application.WorksheetFunction.Match(sCurrMth,extwbk.Worksheets(" Info Sheet")。Range(" 1:1"),0))
我的代码如下,我将不胜感激任何指导
Sub IndexMatch()
'Index/Match store info and get store current period
Dim rw As Long, x As Range
Dim extwbk As Workbook, fNameAndPath As Workbook
Dim sCurrMth As String
Dim rStoreNo As Range
Dim rMth
Set fNameAndPath = ActiveWorkbook
Set extwbk = Workbooks.Open("H:\***\***\Master Store Info.xlsm", ReadOnly:=True)
Set x = extwbk.Worksheets("Info Sheet").Range("A1:z9999")
sCurrMth = "Aug"
Set rStoreNo = extwbk.Worksheets("Info Sheet").Range("A1:A" & Rows.Count)
With fNameAndPath.Worksheets("Sheet1")
For rw = 2 To .Cells(Rows.Count, 1).End(xlUp).Row
.Cells(rw, 10) = Application.WorksheetFunction.Index(x, Application.WorksheetFunction.Match(fNameAndPath.Range("A2"), extwbk.Worksheets("Info Sheet").Range("A1:A" & Rows.Count), 0) + _
Application.WorksheetFunction.Match(sCurrMth, extwbk.Worksheets("Info Sheet").Range("1:1"), 0))
Next rw
End With
Workbooks("Master Store Info.xlsm").Close
End Sub
答案 0 :(得分:0)
Sub IndexMatch()
'Index/Match store info and get store current period
Dim rw As Long, x As Range
Dim extwbk As Workbook, fNameAndPath As Workbook
Dim sCurrMth As String
Dim rStoreNo As Range
Dim rMth
Set fNameAndPath = ActiveWorkbook
Set extwbk = Workbooks.Open("H:\***\***\Master Store Info.xlsm", ReadOnly:=True)
Set x = extwbk.Worksheets("Info Sheet").Range("A1:z9999")
sCurrMth = "Aug"
Set rStoreNo = extwbk.Worksheets("Info Sheet").Range("A1:A" & Rows.Count)
With fNameAndPath.Worksheets("Sheet1")
'Start in row2 and look until the end of rows
For rw = 2 To .Cells(Rows.Count, 1).End(xlUp).Row
.Cells(rw, 10) = Application.WorksheetFunction.Index(x, Application.WorksheetFunction.Match(fNameAndPath.Worksheets("Sheet1").Range("A" & row), extwbk.Worksheets("Info Sheet").Range("A1:A" & Rows.Count), 0) + _
Application.WorksheetFunction.Match(sCurrMth, extwbk.Worksheets("Info Sheet").Range("1:1"), 0))
Next rw
End With
Workbooks("Master Store Info.xlsm").Close
End Sub