2016-04-21 59 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

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