Excel 2013.如何在TextBox中显示2个名称,数据来自Excel Sheet。 我可以根据员工ID显示单个名称,但是如果我想显示2个名称,则无法使用。我在TextBox2中启用了多行。
到目前为止代码:
Private Sub ComboBox1_Change()
Dim mySheet As Worksheet 'declaring mySheet as the Worksheet...
Dim x
Dim i As Long
Dim str As String
Set mySheet = Sheets("Sheet1")
x = mySheet.Range("A1").CurrentRegion.Value
For i = 2 To UBound(x, 1)
If x(i, 2) = Val(ComboBox1.Value) Then
If str = "" Then
str = x(i, 1)
Else
str = str & vbNewLine & x(i, 1)
End If
End If
Next i
If str <> "" Then
TextBox3.Value = str
Else
TextBox3.Value = "Match not found"
End If
End Sub
Private Sub UserForm_Initialize()
ComboBox1.Clear
With ComboBox1
.AddItem "Mon"
.AddItem "Tue"
.AddItem "Wed"
End With
End Sub
答案 0 :(得分:0)
遍历范围并将值连接到文本框中,如下所示:
If Not myRange Is Nothing Then
For Each c In myRange.Cells
TextBox3.Value = TextBox3.Value & " " & c.Value
Next
Else
TextBox3.Value = "Match not found"
End If
答案 1 :(得分:0)
将TextBox3的Multilie属性设置为True并尝试以下代码...
Private Sub TextBox1_Change()
Dim mySheet As Worksheet 'declaring mySheet as the Worksheet...
Dim x
Dim i As Long
Dim str As String
Set mySheet = Sheets("Sheet1")
x = mySheet.Range("A1").CurrentRegion.Value
For i = 2 To UBound(x, 1)
If x(i, 2) = Val(TextBox1.Value) Then
If str = "" Then
str = x(i, 1)
Else
str = str & vbNewLine & x(i, 1)
End If
End If
Next i
If str <> "" Then
TextBox3.Value = str
Else
TextBox3.Value = "Match not found"
End If
End Sub
如果你有ComboBox1而不是ComboBox1有文本值的TextBox1,你可以这样试试......
Private Sub ComboBox1_Change()
Dim mySheet As Worksheet 'declaring mySheet as the Worksheet...
Dim x
Dim i As Long
Dim str As String
Set mySheet = Sheets("Sheet1")
x = mySheet.Range("A1").CurrentRegion.Value
If Application.CountIf(mySheet.Columns(2), ComboBox1.Value) > 0 Then
For i = 2 To UBound(x, 1)
If x(i, 2) = ComboBox1.Value Then
If str = "" Then
str = x(i, 1)
Else
str = str & vbNewLine & x(i, 1)
End If
End If
Next i
TextBox3.Value = str
Else
TextBox3.Value = "Match not found"
End If
End Sub