Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.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 Userform listbox显示范围内的值_Vba_Excel_Excel 2010 - Fatal编程技术网

Vba Userform listbox显示范围内的值

Vba Userform listbox显示范围内的值,vba,excel,excel-2010,Vba,Excel,Excel 2010,我试图在Excel中创建一个Userform,其中有一个组合框,根据所选的值,来自一系列单元格的值将显示在Userform的列表框中 到目前为止,我有这个 Private Sub UserForm_Initialize() With ComboBox1() .AddItem "Item1" .AddItem "Item2" .AddItem "Item3" .AddItem "Item4" .AddItem "Item5" .AddItem "Item6"

我试图在Excel中创建一个Userform,其中有一个组合框,根据所选的值,来自一系列单元格的值将显示在Userform的列表框中

到目前为止,我有这个

Private Sub UserForm_Initialize()

With ComboBox1()
   .AddItem "Item1"
   .AddItem "Item2"
   .AddItem "Item3"
   .AddItem "Item4"
   .AddItem "Item5"
   .AddItem "Item6"
   .AddItem "Item7"
End With

End Sub


Sub ComboBox1_Change()

    If ComboBox1.ListIndex = Outlook Then
        Range("A3:B11").Show
    Else
        If ComboBox1.ListIndex = NoNetwork Then
            Range("C3:D11").Show
        End If
    End If

End Sub
如果我更改
范围(“A3:B11”)。将
显示为
范围(“A3:B11”)。选择
它将选择此范围


如何在UserForm上显示此范围的数据?

这里有一种方法:

1。将一些命名范围添加到工作表中

范围
B2:C3
已分配给名为
Name1
的命名范围。分配给
Name2
等的范围
E2:F3


2。使用组合框和列表框创建用户表单


3。将以下代码放入相关模块:

Userform1模块

任何标准模块


4。从标准模块中运行
ShowUserform
sub并选择一个名称


表格上的数据填充到哪里?一个列表框或什么的?是的,至少,这是我想要实现的。Show只是滚动窗口,简而言之,它的行为就像转到那个单元格。在您的示例中,您没有显示尝试将范围的内容加载到表单上的文本框、列表框和其他组合框等中的任何地方。如果要将其加载到列表框组合框中,则只需循环遍历单元格和
。AddItem CellValue
Private Sub UserForm_Initialize()
    ComboBox1.List = Array("Name1", "Name2", "Name3", "Name4")
End Sub

Private Sub ComboBox1_Change()
    Dim n As Name
    Set n = ThisWorkbook.Names(ComboBox1.Value)

    If Not n Is Nothing Then
        ListBox1.RowSource = n.RefersToRange.Address
        ListBox1.ColumnCount = n.RefersToRange.Columns.Count
    End If
End Sub
Public Sub ShowUserform()
    With New UserForm1
        .Show vbModal
    End With
End Sub