2017-08-14 102 views
-1

使用數據驗證輸入消息,對於命名範圍(「MyRange」)中的每個單元格,我希望匹配Table1 [Column1]和索引值表1 [列2]。Excel VBA:索引匹配的動態數據驗證輸入消息

我需要循環,因爲它不會,活動單元保持不變,整個範圍都有相同的輸入消息。我沒有承諾公式INDEX MATCH,它只需要執行該功能。

Sub Tester() 
    Dim mycell As String 
    mycell = ActiveCell 
     With Range("MyRange").Validation 
     .Delete 
     .Add Type:=xlValidateList, AlertStyle:=xlValidAlertInformation, _ 
     Operator:=xlBetween, Formula1:="=INDIRECT(Test1&Test2)" 
     .IgnoreBlank = True 
     .InCellDropdown = True 
     .InputTitle = "" 
     .ErrorTitle = "" 
     .InputMessage = Application.WorksheetFunction.Index(Range("Table1[Column2]"), _ 
     Application.WorksheetFunction.Match(mycell, Range("Table1[Column1]"), 0)) 
     .ErrorMessage = "" 
     .ShowInput = True 
     .ShowError = False 
    End With 
End Sub 

謝謝你的幫助!

+0

有沒有循環?您只需要進行範圍驗證...您希望它在哪裏循環? *至少*你會想在你第一次使用'Cells(activecell.row + 1,ActiveCell.Column)'後改變activecell。 ...但也不要這樣做,因爲它[最好避免使用'.Select' /'.Activate'](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select -in-Excel的VBA)。 – BruceWayne

+0

我不指望它。這是我想解決的問題。 :) – AlivePresumably

+0

你可以做一個網絡搜索有關在一個範圍內循環.... http://www.excel-easy.com/vba/examples/loop-through-defined-range.html – jsotola

回答

0

將此代碼包裹在您現在的代碼中。基本上,您想要將代碼粘貼到Debug.Print的行中。 。 。進行一些非常小的修改。

Sub LoopRange() 

    Dim rCell As Range 
    Dim rRng As Range 

    Set rRng = Sheet1.Range("A1:A6") 

    For Each rCell In rRng.Cells 
     Debug.Print rCell.Address, rCell.Value 
    Next rCell 

End Sub 

Loop through each cell in a range of cells when given a Range object