Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.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
Excel 列表框选择变为文本框选择_Excel_Vba_Userform - Fatal编程技术网

Excel 列表框选择变为文本框选择

Excel 列表框选择变为文本框选择,excel,vba,userform,Excel,Vba,Userform,我希望用户能够从列表框中单击一个值,并将所选值变为文本框值。我从Textbox1.Value=selectedItems.Text中得到一个编译错误:selectedItems的限定符无效。我希望选定的值也显示在文本框中 Private Sub ListBox1_Click() Dim selectedItems As String, i As Integer For i = 0 To ListBox1.ListCount - 1 If ListBox1.Selected

我希望用户能够从列表框中单击一个值,并将所选值变为文本框值。我从
Textbox1.Value=selectedItems.Text中得到一个编译错误:
selectedItems
的限定符无效。我希望选定的值也显示在文本框中

Private Sub ListBox1_Click()
Dim selectedItems As String, i As Integer
    For i = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(i) = True Then
        selectedItems = selectedItems & ListBox1.List(i) & vbNewLine
    End If
Next i

TextBox1.Value = selectedItems.Text

End Sub

selectedItems
是一个字符串,字符串没有任何属性,例如
Text

TextBox1.Value = selectedItems

将是您需要的

它是多行文本框吗?默认情况下不会。我删除了我的注释,因为我可以添加
myString=Left(myString,Len(myString)-1)
删除段落符号如果您只选择了一个
ListBox
项,则您当前的代码不正确,问题中的代码用于将
列表框1
中的多个选定项添加到文本框中的当前文本中。您不需要遍历每个
列表框1
项。如果要将单击的项目从
ListBox1
放入
TextBox1
中,则只需
TextBox1.Text=ListBox1
所有其他代码都多余。