我正在构建一个页面,其中 Col H 应该是一个依赖于 Col A 的下拉框。
Col A 已经设置为使用在名为 Data 的隐藏工作表上指定的动态命名范围的验证列表。
此外,在数据表上,我指定了 3 个依赖于 Col A 的列表,并且已经将它们设为动态命名范围。
到目前为止,在 VB 代码中,我有
从 Col A 中的选择中取出逗号之前的第一个单词,并将其用作我的“组”标识符。
将输入到 Col B 的所有文本大写(不相关)。
现在,我需要在 Col H 中指定尽可能选择的内容。您可以在“桌面”的情况下看到我尝试这样做,但是它不起作用并给我一个“需要对象”错误。
旧代码:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Whoa
Application.EnableEvents = False
If Not Intersect(Target, Columns(1)) Is Nothing Then
If Target.Value <> "" And InStr(1, Target.Value, ",") Then
Select Case Split(Target.Value, ",")(0)
Case "Desktop": Range("H" & Target.row).Value =
Data.Range("List_Desktops").Address
Case "Laptop": Range("H" & Target.row).Value = "Laptop"
Case "Server": Range("H" & Target.row).Value = "Server"
Case Else: Range("H" & Target.row).Value = "N/A"
End Select
End If
ElseIf Not Intersect(Target, Columns(2)) Is Nothing Then
If Not Target.HasFormula Then Target.Value = UCase(Target.Value)
End If
LetsContinue:
Application.EnableEvents = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume LetsContinue
End Sub
新代码:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long, LastRow As Long, n As Long
Dim MyCol As Collection
Dim SearchString As String, TempList As String
On Error GoTo Whoa
Application.EnableEvents = False
'~~> Find LastRow in List_Descriptions
LastRow = Sheet2.Range("A" & Rows.Count).End(xlUp).row
If Not Intersect(Target, Columns(1)) Is Nothing Then
Set MyCol = New Collection
'~~> Get the data from List_Descriptions into a collection
For i = 1 To LastRow
If Len(Trim(Sheet2.Range("A" & i).Value)) <> 0 Then
On Error Resume Next
MyCol.Add CStr(Sheet2.Range("A" & i).Value), CStr(Sheet2.Range("A" & i).Value)
On Error GoTo 0
End If
Next i
'~~> Create a list for the DV List
For n = 1 To MyCol.Count
TempList = TempList & "," & MyCol(n)
Next
TempList = Mid(TempList, 2)
Range("A" & Target.row).ClearContents: Range("A" & Target.row).Validation.Delete
'~~> Create the DV List
If Len(Trim(TempList)) <> 0 Then
With Range("A" & Target.row).Validation
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=TempList
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End If
'~~> Capturing change in cell D1
ElseIf Not Intersect(Target, Range("A" & Target.row)) Is Nothing Then
SearchString = Range("A" & Target.row).Value
TempList = FindRange(Sheet2.Range("A1:A" & LastRow), SearchString)
Range("H" & Target.row).ClearContents: Range("H" & Target.row).Validation.Delete
If Len(Trim(TempList)) <> 0 Then
'~~> Create the DV List
With Range("H" & Target.row).Validation
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=TempList
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End If
End If
If Target.Value <> "" And InStr(1, Target.Value, ",") Then
Select Case Split(Target.Value, ",")(0)
Case "Desktop": Range("H" & Target.row).Value = "Desktop"
Case "Laptop": Range("H" & Target.row).Value = "Laptop"
Case "Server": Range("H" & Target.row).Value = "Server"
Case Else: Range("H" & Target.row).Value = "N/A"
End Select
End If
ElseIf Not Intersect(Target, Columns(2)) Is Nothing Then
If Not Target.HasFormula Then Target.Value = UCase(Target.Value)
End If
LetsContinue:
Application.EnableEvents = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume LetsContinue
End Sub
'~~> Function required to find the list from Col B
Function FindRange(FirstRange As Range, StrSearch As String) As String
Dim aCell As Range, bCell As Range, oRange As Range
Dim ExitLoop As Boolean
Dim strTemp As String
Set aCell = FirstRange.Find(what:=StrSearch, LookIn:=xlValues, _
lookat:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
ExitLoop = False
If Not aCell Is Nothing Then
Set bCell = aCell
strTemp = strTemp & "," & aCell.Offset(, 1).Value
Do While ExitLoop = False
Set aCell = FirstRange.FindNext(After:=aCell)
If Not aCell Is Nothing Then
If aCell.Address = bCell.Address Then Exit Do
strTemp = strTemp & "," & aCell.Offset(, 1).Value
Else
ExitLoop = True
End If
Loop
FindRange = Mid(strTemp, 2)
End If
End Function
样本工作簿:https ://docs.google.com/open?id=0B9ss2136xoWIVGxQYUJJX2xXc00