2016-06-13 18 views
0

セルに検証リストを割り当てようとしています。バリデーションリストは、あるセルの値に応じて可変であり、例えば、セル「C6」の値が28の場合、バリデーションリストは、範囲「Sheet4.Range( "b4:b20")である。あなたが検証リストは、これを実行する別のsheet.inオーダーからである見ることができるように私は今、何が起こる以下のコードVBAを使用したセル検証

ValrStart = Sheet4.Cells(rowno, 4).Address ‘rowno is the row in which the validation list starts and its value comes from another part of the code 
ValrEnd = Sheet4.Cells(rownoEnd, 4).Address rownoEnd is the row in which the validation list ends and its value comes from another part of the code 
Rng = "Sheet4.Range(""" & ValrStart & Chr(58) & ValrEnd & """" & ")" 
With Cells(20, 3).Validation 
      .Delete 
      .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _ 
      Operator:=xlBetween, Formula1:=Rng 
      .ErrorMessage = "Invalid value. Select one from the dropdown list." 
      check = Cells(20, 3).Validation.Value 
      If check = False Then 
      Cells(20, 3).ClearContents 
      Exit Sub 
      End If 
     End With 

を書いたものをセルに私には見えるが、文字列の値がないRNGされていることですそれが表す範囲。 誰でも助けることができますか?問題は、「=」その始まり

で私はあなたが好きなのためにExcelがハードワークを行う必要があるだろう持っている必要がありますFormula1パラメータ、である おかげ

答えて

1

は次のとおりです。

Dim rng As Range '<~~ set rng as of a Range type 
With Sheet4 
    Set rng = .Range(.Cells(rowno, 4), .Cells(rownoEnd, 4)) '<~~ set rng to the wanted range 
End With 

With Cells(20, 13).Validation 
    .Delete 

    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _ 
    Operator:=xlBetween, Formula1:="=" & rng.Address(, , , True) '<~~ have Address function take out the correct formula, just add a "=" at the beginning 

    .ErrorMessage = "Invalid value. Select one from the dropdown list." 
    check = Cells(20, 3).Validation.Value 
    If check = False Then 
     Cells(20, 3).ClearContents 
     Exit Sub 
    End If 
End With 
関連する問題