|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Public dicValidation As Object
- Sub CreateValidation(ByVal bForce As Boolean)
- Dim vData As Variant, nRow As Long
-
- If bForce Or dicValidation Is Nothing Then
- Set dicValidation = CreateObject("Scripting.Dictionary")
- With Sheet2
- vData = .[B1].Resize(.Cells(.Rows.Count, 2).End(xlUp).Row).Value
- For nRow = 1 To UBound(vData)
- If Trim(vData(nRow, 1)) <> "" Then dicValidation(Trim(vData(nRow, 1))) = 0
- Next
- .[Z:Z].ClearContents
- If dicValidation.Count = 1 Then
- .[Z1] = dicValidation.Keys()(0)
- ElseIf dicValidation.Count > 0 Then
- .[Z1].Resize(dicValidation.Count) = Application.WorksheetFunction.Transpose(dicValidation.Keys())
- End If
- End With
-
- With Sheet1.[B3].Validation
- .Delete
- If dicValidation.Count > 0 Then
- .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=Sheet2!$Z$1:$Z$" & dicValidation.Count
- .IgnoreBlank = True
- .InCellDropdown = True
- .InputTitle = ""
- .ErrorTitle = ""
- .InputMessage = ""
- .ErrorMessage = ""
- .IMEMode = xlIMEModeNoControl
- .ShowInput = True
- .ShowError = True
- End If
- End With
- End If
- End Sub
复制代码 |
|