用户表单定义数据验证列表范围excel

时间:2016-01-25 16:03:18

标签: excel vba excel-vba

我有一个按钮,一旦点击循环通过DV列表并将每个选择打印为PDF文档,理想情况下,我可以通过Userform EG选择DV列表的长度我在userform上选择一个选项将DV列表范围设置为50个单元格。

Sub Button_Click6()

Dim cell                  As Excel.Range
Dim rgDV                  As Excel.Range
Dim DV_Cell               As Excel.Range
Dim LA As Boolean



Set ws = ActiveSheet

LAform.Show
    Select Case LAform.Tag
        Case 0
            LA = False     'FALSE FOR Richmond, TRUE FOR Kingston
        Case 1
            LA = True
    End Select


If LA = True Then
ActiveSheet.Range("B1").Validation.Add xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=Worksheets("Data").Range("B4:B56")

ElseIf LA = False Then
ActiveSheet.Range("B1").Validation.Add xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
    xlBetween, Formula1:=Worksheets("Data").Range("B56:B104")
    'enter name and select folder for file
' start in current workbook folder

Set DV_Cell = Range("B1")

Set rgDV = Application.Range(Mid$(DV_Cell.Validation.Formula1, 2))
For Each cell In rgDV.Cells
    DV_Cell.Value = cell.Value
    Call PDFActiveSheet2
Next
End If
End Sub

我得到的问题是应用程序或对象定义的错误在If之后,如果尝试设置dv范围则为其他。

感谢。

1 个答案:

答案 0 :(得分:0)

Sub Button_Click6()

    Dim cell                  As Excel.Range
    Dim rgDV                  As Excel.Range
    Dim DV_Cell               As Excel.Range
    Dim La As Boolean


 Laform.Show
    Select Case Laform.Tag
        Case 0
            La = False     'FALSE FOR Richmond, TRUE FOR Kingston
        Case 1
            La = True
    End Select

Set DV_Cell = Range("B1")

Set rgDV = Application.Range(Mid$(DV_Cell.Validation.Formula1, 2))
For Each cell In rgDV.Cells
    DV_Cell.Value = cell.Value
    Call PDFActiveSheet2
Next

End Sub

根据我给出的建议,我将代码放入用户表单按钮,设置DV列表范围,而宏运行现在应该。

Private Sub Borough1_Click()
Range("B1:E1").Select
    With Selection.Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
    xlBetween, Formula1:="=Data!$B$57:$B$107"
    .IgnoreBlank = True
    .InCellDropdown = True
    .InputTitle = ""
    .ErrorTitle = ""
    .InputMessage = ""
    .ErrorMessage = ""
    .ShowInput = True
    .ShowError = True
    End With
    Me.Hide
End Sub

Private Sub CommandButton1_Click()
Range("B1:E1").Select
    With Selection.Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
    xlBetween, Formula1:="=Data!$B$4:$B$56"
    .IgnoreBlank = True
    .InCellDropdown = True
    .InputTitle = ""
    .ErrorTitle = ""
    .InputMessage = ""
    .ErrorMessage = ""
    .ShowInput = True
    .ShowError = True
    End With
 Me.Hide
End Sub