在EXCEL VBA中 在TextBox2中显示TextBox1中的内容在Sheet2表中所在行A列的数据,且当textbox1内容变更是,textbox2也随之变化
时间: 2024-05-29 22:08:42 浏览: 16
你可以使用如下代码实现:
Private Sub TextBox1_Change()
Dim searchValue As String
Dim searchRange As Range
searchValue = TextBox1.Value
Set searchRange = Sheet2.Range("A:A").Find(searchValue, , xlValues, xlWhole)
If Not searchRange Is Nothing Then
TextBox2.Value = searchRange.Offset(0, 1).Value
End If
End Sub
这段代码会在文本框1的内容变更时触发,在表格2的第一列中查找文本框1中的内容,并在文本框2中显示对应行A列的数据。当文本框1中的内容变更时,文本框2也会相应地更新。
相关问题
在EXCEL vba中,在sheet6的B列中搜索textbox1中输入的内容,返回对应在A列中的内容在textbox2中
可以使用VBA代码来实现这个需求。以下是一个示例代码:
```
Private Sub SearchButton_Click()
Dim searchValue As String
Dim searchRange As Range
Dim foundCell As Range
'获取搜索关键字
searchValue = TextBox1.Value
'定义搜索范围
Set searchRange = Sheets("Sheet6").Range("B1:B100")
'在搜索范围中查找关键字
Set foundCell = searchRange.Find(What:=searchValue, LookIn:=xlValues, LookAt:=xlWhole)
'如果找到了匹配的单元格,则将对应的A列单元格的值赋值给textbox2
If Not foundCell Is Nothing Then
TextBox2.Value = foundCell.Offset(0, -1).Value
Else
MsgBox "未找到匹配的记录!"
End If
End Sub
```
请将上述代码复制粘贴到Excel的VBA编辑器中,并将"SearchButton"、"TextBox1"和"TextBox2"替换成你实际使用的控件名。在使用时,用户可以在"TextBox1"中输入要搜索的关键字,单击"SearchButton"按钮后,程序会在Sheet6的B列中查找匹配的单元格,并将对应的A列单元格的值显示在"TextBox2"中。如果未找到匹配的记录,则会弹出一个提示框。
excel vba点击按钮查询sheet2表中的数据,并显示在文本框控件中
您可以使用 VBA 代码编写一个事件处理程序,在按钮点击时,查询 Sheet2 表格中的数据,然后将数据显示在指定的文本框控件中。以下是示例代码:
Private Sub CommandButton1_Click()
Dim searchValue As String
Dim searchResult As Range
searchValue = TextBox1.Value
Set searchResult = Sheets("Sheet2").Range("A1:A100").Find(What:=searchValue)
If Not searchResult Is Nothing Then
TextBox2.Value = searchResult.Offset(0, 1).Value
Else
MsgBox "未找到相关数据!"
End If
End Sub
在此示例中,我们设置两个文本框控件:一个用于输入查询值,另一个用于显示查询结果。我们使用 Find 方法来搜索 Sheet2 表中的数据,并将找到的结果显示在 TextBox2 中。如果没有找到相关数据,将弹出一个消息框提示用户。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)