VBA索引匹配不循环,给出相同的答案

时间:2017-06-21 19:24:27

标签: excel vba excel-vba indexing match

我有一个索引匹配函数,我试图保存为VBA,以便我可以将它添加到多个工作簿。我对它进行编码的方式一直给我相同的答案,并且不会循环到下一个单元格来查找不同的关键字。我希望它能看看N76中的内容,并在O76中找到答案,但是对于N77,它给出了与O76相同的答案,即使N77中的关键字不同。

Sub Covenant()

Dim Covenant_1 As Integer
Dim Dashboard As Worksheet
Dim Covenant_Sheet As Worksheet
Dim Cov_Date As Range
Dim Cov_Dates As Range
Dim Cov_Type As Variant
Dim DB_Cov As Variant
Dim Cov_Type2 As Variant




Set Dashboard = Sheets("Dashboard")
Set Covenant_Sheet = Sheets("Covenants")
Set Cov_Date = Dashboard.Range("N74")
Set Cov_Dates = Covenant_Sheet.Range("B4:AB4")
Set Cov_Type = Covenant_Sheet.Range("B6:AB13")
Set DB_Cov = Dashboard.Range("L76:L80")
Set Cov_Type2 = Covenant_Sheet.Range("B6:B13")



For Each Cell In Dashboard.Range("N76:N80")
Cell.Value = Application.Index(Cov_Type, Application.Match(DB_Cov, Cov_Type2, 0), Application.Match(Cov_Date, Cov_Dates, 0))


Next

End Sub

1 个答案:

答案 0 :(得分:0)

我认为我没有很好地解释我的问题,但下面是答案。我需要添加一个x变量来运行循环。还有一些其他的添加,但下面应该可以运行索引/匹配循环。

Sub Covenant()

Dim Covenant_1 As Integer
Dim Dashboard As Worksheet
Dim Covenant_Sheet As Worksheet
Dim Cov_Date As Range
Dim Cov_Dates As Range
Dim Cov_Type As Variant
Dim DB_Cov As Variant
Dim Cov_Type2 As Variant
Dim Cov_Next As Range



Set Dashboard = Sheets("Dashboard")
Set Covenant_Sheet = Sheets("Covenants")
Set Cov_Date = Dashboard.Range("M74")
Set Cov_Dates = Covenant_Sheet.Range("B4:AB4")
Set Cov_Type = Covenant_Sheet.Range("B6:AB13")
Set DB_Cov = Dashboard.Range("K75")
Set Cov_Type2 = Covenant_Sheet.Range("B6:B13")
Set Cov_Next = Dashboard.Range("P74")

For x = 1 To 5
    For y = 1 To 5
Cells(x + 75, 13) = Application.IfError(Application.Index(Cov_Type, Application.Match(DB_Cov.Offset(x, 0), Cov_Type2, 0), Application.Match(Cov_Date, Cov_Dates, 0)), "")

    Cells(y + 75, 16) = Application.IfError(Application.Index(Cov_Type, Application.Match(DB_Cov.Offset(y, 0), Cov_Type2, 0), Application.Match(Cov_Next, Cov_Dates, 0)), "")
    Next y
Next x
End Sub