VBA 列表中的数据验证下拉列表(模块化子-无硬编码)

VBA Data validation dropdown from list (modular sub- no hardcode)

我想通过 VBA EXCEL 代码创建数据验证列表,每个验证来自一个动态更新的列表(例如,如果将项目添加到验证源列表,范围将更新通过代码,而不是手动)。

我有很多这样的个人验证。

首先,我使用硬编码范围为每个验证创建了单独的 VBA 子,但这不是模块化的,代码用类似的代码重复自身,每次我需要更新源代码和维护时都不容易维护目的地范围或添加新的验证。它对编码错误很敏感,不得不提醒自己在代码中究竟应该把范围写在哪里。 我想要一个通用的模块化 VBA 验证列表,我可以继续使用而无需一次又一次地重复相同的代码结构。

我有一个带有验证组合框的“数据”Sheet 和一个带有验证列表的“信息”sheet。

这是一个 VBA 验证结构的示例,它具有硬编码 sheet 和单元格范围。每次重写真是头疼:

Public Sub HardcodedValidate()
Dim lastRowSource10 As Long
Dim rangeCombo10  As Range
Const F1_TransalteTo As String = "O"
Const F2_TransalteTo As String = "E"
    lastRowCombo10 = 9999
    lastRowSource10 = Transalte.Cells(Transalte.Rows.Count, "F").End(xlUp).Row
    Set rangeCombo10 = Phones.Range(F2_TransalteTo & "2:" & F2_TransalteTo & lastRowCombo10)
    With rangeCombo10.Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
             xlBetween, Formula1:="=Transalte!$F:$F$" & lastRowSource10
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = vbNullString
        .ErrorTitle = vbNullString
        .InputMessage = vbNullString
        .ErrorMessage = vbNullString
        .ShowInput = True
        .ShowError = True
    End With
End Sub
Option Explicit

'' Validation list with scrollable dropdown.
'' * General Sub without hardcoded specific ranges (modular).
'' * No empty cells at the end of each dropdown.
'' * Calculated Source LastRow.
'' See at the end an example of Call of main sub by RunGeneralValidate

Public Sub GeneralValidate( _
ByVal sheetSource As Worksheet, ByVal columnSource As String, ByVal firstRowSource As Long, _
ByVal sheetCombo As Worksheet, ByVal columnCombo As String, ByVal firstRowCombo As Long, ByVal lastRowCombo As Long)

Dim rangeSource As Range
Dim rangeCombo As Range
Dim lastRowSource As Long

   lastRowSource = sheetSource.Cells(sheetSource.Rows.Count, columnSource).End(xlUp).Row

    Set rangeCombo = sheetCombo.Range(columnCombo & firstRowCombo & ":" & columnCombo & lastRowCombo)
    Set rangeSource = sheetSource.Range("$" & columnSource & "$" & firstRowSource & ":$" & columnSource & "$" & lastRowSource)
    
    With rangeCombo.Validation
        .Delete ''delete previous validation
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
             xlBetween, Formula1:="=" & "'" & sheetSource.Name & "'" & "!" & rangeSource.Address
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = vbNullString
        .ErrorTitle = vbNullString
        .InputMessage = vbNullString
        .ErrorMessage = vbNullString
        .ShowInput = True
        .ShowError = True
    End With      
End Sub
   
Public Sub RunGeneralValidate()
''Example of running the sub
Dim Info As Worksheet
Dim Data As Worksheet

Call GeneralValidate(Info, "A", 2, _
                     Data, "D", 4, 100)
                     
Call GeneralValidate(Info, "B", 2, _
                     Data, "E", 4, 100)

Call GeneralValidate(Info, "C", 2, _
                     Data, "F", 4, 100)
End Sub