如何从excel获取userform texbox文本取决于条件 [英] how to get userform texbox text from excel depends on condition
本文介绍了如何从excel获取userform texbox文本取决于条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我创建了一个与销售相关的表格。
字段就像Item一样,将所有这些数据定价存放在Excel工作表中。
当在userform中选择Item时,相关价格文本框值需要默认获取价格怎么办这个。
Hi All,
I have created one sales related form.
the fields are like Item, price all these data stored in excel sheet.
When select Item in userform the relavent price textbox value need to get price by default how to do this.
推荐答案
要做的步骤:
Steps to do:
- 添加新的shee并更改其名称为
数据
- 让
A1:B1
范围为标题:项目
和价格
分别 - 添加样本数据
- 转到VBA代码编辑(ALT + F11)
- 添加模块(工具 - >插入 - >模块)
- 复制并粘贴下面的代码
- Add new shee and change its name to "
Data
" - Lets
A1:B1
range will be the headers:Item
andPrice
respectively - Add sample data
- Go to VBA code editor (ALT+F11)
- Add module (Tools->Insert->Module)
- Copy and paste below code
Option Explicit
'loads data into 2 columns listbox from desired range
Sub Load2Columns(ByRef oLst As MSForms.ListBox, ByRef wsh As Worksheet, Optional ByVal sFirstColumn As String = "A", Optional ByVal iFirstRow As Integer = 2)
Dim i As Integer
With oLst
Do While wsh.Range(sFirstColumn & iFirstRow) <> ""
.AddItem ""
i = .ListCount - 1
.Column(0, i) = wsh.Range(sFirstColumn & iFirstRow)
.Column(1, i) = wsh.Range(sFirstColumn & iFirstRow).Offset(ColumnOffset:=1)
iFirstRow = iFirstRow + 1
Loop
End With
End Sub
- 一个ListBox
- Add UserForm (Tools->Insert->UserForm
- From ToolBox add <il>
- one ListBox
- 一个TextBox
- 两个标签
- 一个按钮
Option Explicit
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub ListBox1_Change()
Me.TextBox1 = Me.ListBox1.List(Me.ListBox1.ListIndex, 1)
End Sub
Private Sub UserForm_Initialize()
Load2Columns Me.ListBox1, ThisWorkbook.Worksheets("Data")
With Me.ListBox1
.ColumnCount = 2
.ColumnWidths = ";0"
.ListIndex = 0
End With
End Sub
Here[^] you can download an example.
这篇关于如何从excel获取userform texbox文本取决于条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文