使用Sub或Function未定义的错误循环INDEX-MATCH VBA

时间:2015-06-01 20:21:17

标签: excel vba excel-vba

我正在尝试调试循环的INDEX-MATCH VBA,因为它经常抛出Sub或Function未定义的错误。通过我在本网站上已经找到的内容,我能够检查我的参考文献,但我似乎仍然遗漏了一些东西(" Solver"已经过检查)。在这一点上,额外的一双眼睛将是最有帮助的!

Private Sub Looping_Index_Match()
Application.ScreenUpdating = False
Dim rng As Range
Dim cell as range
Dim IndexRange as range
Dim MatchRange as range

Set rng = ActiveSheet.Range("D42:D241")

 With  Workbook("WorkCenter.xlsm").Sheets(ComboBox1.Value)
     Set IndexRange=Range(.Range("M2"),.Range(“M2”).end(xlup))
     Set MatchRange= Range(.Range("L2"),.Range(“L2”).end(xlup))
 End With

For Each cell In rng
    Cell.Offset(0,1)=Application.WorksheetFunction.Index(IndexRange,Application.WorksheetFuntion.Match(cell,Application.WorksheetFunction.Match(cell,MatchRange,0))
Next

Application.ScreenUpdating=True
End Sub

注意:涉及两个工作簿。来自"工作中心的数据"正在检索工作簿列M并将其输入到"摘要"工作簿,与L列中的序列号相匹配。

3 个答案:

答案 0 :(得分:1)

我将分享一个我建立的快速示例: 我使用1个组合框和一个命令按钮创建了一个简单的用户表单。到组合框我只添加了第一张纸的名称。命令按钮调用存储在单独模块中的另一个宏调用Looping_Index,传递组合框值

Private Sub CommandButton1_Click()
Call Looping_Index(UserForm1.ComboBox1.Value)
End Sub

Private Sub UserForm_Initialize()
ComboBox1.AddItem "Sheet1"
End Sub

Sub Looping_Index(hoja As String)

Sheets(hoja).Activate
Unload UserForm1
End Sub

这是一个关于如何使用userforms和传递值的简单示例。希望它有所帮助

答案 1 :(得分:0)

以下代码适合我(同一模块):

Private Sub CommandButton1_Click()
Dim yoursheet As Worksheet
Set yoursheet = Sheets(ComboBox1.Value)


With yoursheet
Set IndexRange = Range(.Range("M2"), .Range("M2").End(xlDown))
Set MatchRange = Range(.Range("L2"), .Range("L2").End(xlDown))

End With



ActiveSheet.Range("e42").Formula = "=index(" & yoursheet.Name & "!" & IndexRange.Address & ",match(d42" & "," & yoursheet.Name & "!" & MatchRange.Address & ",0))"
ActiveSheet.Range("e42:e241").FillDown

Unload UserForm1

End Sub

Private Sub UserForm_Initialize()
ComboBox1.AddItem "Sheet2"
End Sub  

答案 2 :(得分:0)

Private Sub CommandButton1_Click()
Dim yoursheet As Worksheet
Dim yourworkbook As Workbook


Set yourworkbook = Workbooks("Book3.xlsx")
Set yoursheet = yourworkbook.Sheets(ComboBox1.Value)


With yoursheet
Set IndexRange = Range(.Range("M2"), .Range("M2").End(xlDown))
Set MatchRange = Range(.Range("L2"), .Range("L2").End(xlDown))

End With



ActiveWorkbook.ActiveSheet.Range("e42").Formula = "=index([" & yourworkbook.Name & "]" & yoursheet.Name & "!" & IndexRange.Address & ",match(d42" & ",[" & yourworkbook.Name & "]" & yoursheet.Name & "!" & MatchRange.Address & ",0))"
ActiveWorkbook.ActiveSheet.Range("e42:e241").FillDown

ActiveWorkbook.ActiveSheet.Range("e42:e241").Copy
ActiveWorkbook.ActiveSheet.Range("e42:e241").PasteSpecial xlValues
Application.CutCopyMode = False
Unload UserForm1

End Sub

Private Sub UserForm_Initialize()
ComboBox1.AddItem "Sheet2"
End Sub