2016-04-21 27 views
0

希望你好。请在尝试了很多但没有成功后请专家帮忙。根据组合框中的两个标准返回Textbox1值,并且标签

我有3列在Sheet1的价格表:

医疗程序
类型
程序

用户窗体中的价值,我需要在Textbox1的返回程序的基础上的价值在combobox1中选择的条件(具有可在Sheet1中Medical Procedure列中找到的值)和label1中的标题(其中alrealdy中填充了可在Sheet1的Type列中遇到的值)。

我试过在用户B哈特(感谢,B哈特!)的计算器中发现此处,但我无法将其更改为以数值形式返回到文本框中(此vba将找到的值插入一个列表框)。另一个问题是,下面的标准是在两个组合框中。我需要两个标准在一个组合框中,另一个在标签中。

Private Sub GetCondStrandValue() 
Dim iRow As Long 
Dim strValue As String 

strValue = vbNullString 
If Me.ComboBox1.Value = vbNullString Or Me.ComboBox2.Value = vbNullString Then Exit Sub 

With Planilha1 
    For iRow = 2 To .Range("A65536").End(xlUp).Row 
     If StrComp(.Cells(iRow, 1).Value, Me.ComboBox1.Value, 1) = 0 And _ 
     StrComp(.Cells(iRow, 2).Value, Me.ComboBox2.Value, 1) = 0 Then 
      strValue = .Cells(iRow, 3).Value 
      Exit For 
     End If 
    Next 
End With 

If strValue = vbNullString Then Exit Sub 
With Me.ListBox1 
    'If you only want a single value in the listbox un-comment the .clear line 
    'Otherwise, values will continue to be added 
    '.Clear 
    .AddItem strValue 
    .Value = strValue 
    .SetFocus 
End With 
End Sub 
+0

@DirkReichel谢谢!我编辑了这个问题,我在 –

回答

0

也许是这样的:

Private Sub combobox1_Change() 

    Dim lastRow As Integer 

    lastRow = Cells(Rows.Count, 1).End(xlUp).Row 

    With Me 

     For r = 2 To lastRow 

      If Sheets("Sheet1").Cells(r, 1) = .ComboBox1.Value And Sheets("Sheet1").Cells(r, 2) = .Label1.Caption Then 
       .TextBox1.Text = Sheets("Sheet1").Cells(r, 3) 
       Exit For 
      End If 

     Next 

    End With 

End Sub 
+0

以上尝试过,谢谢你的好意!我试过了,但是出现了一个错误438。我把这个代码放在Private Sub combobox1_Change()下,也许是这样吗?我希望它在combobox1具有选定值时执行。对缺乏知识感到抱歉! –

+0

尝试将“Activesheet”更改为“表格(”Sheet1“)”或任何对象所在的位置。如果事物位于不同的工作表上,则必须使用正确的工作表名称(或用户窗体名称)删除With块并以“.Cells”(任何以点开头的内容)为前缀。我也会检查你的对象名称,“ComboBox1”等。另外,我的代码假设你的数据在列A:C中。 –

+0

我修改了我的代码的第二部分以适合您的要求。 –