【问题标题】:Return on Textbox1 value based upon two criterias in combobox and label根据组合框和标签中的两个条件返回 Textbox1 值
【发布时间】:2016-04-22 05:11:35
【问题描述】:

希望你一切都好。在尝试了很多但没有成功后需要专家的帮助,拜托。

我在 Sheet1 中有一个包含 3 列的价目表:

医疗程序
类型
过程的价值

在用户表单中,我需要在 Textbox1 中返回基于在组合框 1 中选择的标准的程序值(可以在 Sheet1 的医疗程序列中找到值)和 label1 中的标题(实际上已经填充了可以在 Sheet1) 的类型列中遇到的值。

我在用户 B Hart 的 stackoverflow 中尝试了这个(谢谢,B Hart!),但我无法将其更改为在文本框中作为数值返回(这个 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

【问题讨论】:

  • @DirkReichel 谢谢!我用上面的尝试编辑了问题

标签: excel vba userform


【解决方案1】:

可能是这样的:

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

【讨论】:

  • 感谢您的好意!我试过了,但出现错误 438。我将此代码放在 Private Sub combobox1_Change() 下,也许是这样?我希望在 combobox1 具有选定值时执行它。知识欠缺见谅!
  • 尝试将 'Activesheet' 更改为 'Sheets("Sheet1")' 或您的对象所在的任何位置。如果事情在不同的工作表上,您必须使用正确的工作表名称(或用户窗体名称)删除 With 块和前缀“.Cells”(以点开头的任何内容)等。我还会检查您的对象名称、“ComboBox1”等。此外,我的代码假定您的数据位于 A:C 列中。
  • 我修改了我的代码的第二部分以满足您的要求。
  • 谢谢你,@Chris Mack!我遇到了另一个错误,但设法通过在 .ComboBox1 和 .ComboBox1 前面添加用户窗体的名称来解决它。 Label1 和 .Textbox1!再次感谢您的光临
  • 抱歉,是我,我不知何故选择了“Planilhal”作为用户窗体的名称。 :) 我会更正代码。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-30
  • 1970-01-01
  • 2016-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多