Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 用于定义数据验证列表范围excel的用户窗体_Vba_Excel - Fatal编程技术网

Vba 用于定义数据验证列表范围excel的用户窗体

Vba 用于定义数据验证列表范围excel的用户窗体,vba,excel,Vba,Excel,我有一个按钮,点击后循环浏览DV列表并将每个选择打印为PDF文档,理想情况下,我希望能够通过用户表单选择DV列表的长度,例如我在用户表单上选择选项一,将DV列表范围设置为50个单元格 子按钮_Click6() 我遇到的问题是,在尝试设置dv范围时,If和else之后出现了应用程序或对象定义的错误 谢谢 Sub Button_Click6() Dim cell As Excel.Range Dim rgDV As

我有一个按钮,点击后循环浏览DV列表并将每个选择打印为PDF文档,理想情况下,我希望能够通过用户表单选择DV列表的长度,例如我在用户表单上选择选项一,将DV列表范围设置为50个单元格

子按钮_Click6()

我遇到的问题是,在尝试设置dv范围时,If和else之后出现了应用程序或对象定义的错误

谢谢

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

错误发生在哪一行?而
LA
是否如你所期望的那样普及呢?LA运行得很好,如果公式不能在后面运行的话。ActiveSheet.Range(“B1”).Validation.Add xlValidateList,AlertStyle:=xlValidAlertStop,Operator:=\xlBetween,Formula1:=工作表(“数据”).Range(“B4:B56”)我认为如果您只需在单击按钮时显示
UserForm
,代码编写会更好。然后在用户窗体上有两个按钮,用户可以单击以获得特定选项。然后将代码放在每个按钮后面,以打印您想要的列表。此外,这里不需要进行数据验证。只需在要使用的特定单元格中循环。除非您以后需要进行验证。对不起,我没有很好地解释我自己,DV列表是需要的,因为活动表在列表中的每个选择上都会发生变化,它作为每个客户的声明。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