2016-06-13 5 views
0

유효성 검사 목록을 셀에 할당하려고합니다. 유효성 검사 목록은 특정 셀의 값에 따라 달라질 수 있습니다. 예를 들어 "C6"셀의 값이 28이면 유효성 검사 목록은 Sheet4.Range ("b4 : b20") 범위 여야합니다. 유효성 검증 목록이 작업을 수행하는 또 다른 sheet.in 순서에서입니다 볼 수 있듯이 나는 이제 어떻게 셀에 나에게 표시되는 내용을 문자열 RNG의 값이 아니라는 것을 무엇 아래의 코드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 

을 썼다 범위를 나타냅니다. 아무도 도와 줄 수 있습니까? 감사

답변

1

문제는이 있어야합니다 Formula1 매개 변수에있는 "="는 그것의 시작 부분에

내가 원하는 위해 엑셀 하드 작업을 할 거라고

은 다음 :

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 
관련 문제