1

New to VBA, and using it in Excel.

I have a User form, and I am trying to use a lookup to fill TextBox4 from the value that is entered in ComboBox3. I have the following code which compiles, however it is producing the msgbox to say that the string cant be found...

Private Sub ComboBox3_Change()

    Dim strFind As String
    Dim rFound As Range
    ws = "Year-to-Date Summary"

    If ComboBox3.ListIndex > -1 Then
        strFind = ComboBox3
        On Error Resume Next
        With ws.Column(2, 3)
            Set rFound = .Find(What:="strFind", After:=.Cells(39, 49), _
            LookIn:=.Cells(39, 49), LookAt _
            :=.Cells(39, 49), SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
            MatchCase:= False)
        End With

        If rFound Is Nothing Then
            MsgBox strFind & " cannot be found"
            Exit Sub
        Else
            TextBox4 = rFound(1, 2)
        End If

    End If

End Sub

I also tried Vlookup, however this sprung error messages...

Private Sub ComboBox3_Change()

TextBox4.Text = WorksheetFunction.VLookup(Val(ComboBox3.Text), _
Sheets("Year-to-Date Summary").Range("C39:C49" & LastRow), 2, False)

End Sub
4

4 回答 4

1
  1. ws 是一个变体。您不会像这样分配工作表。你必须使用Set
  2. strFind 在引号内,因此将被视为字符串

请参阅此示例(未测试

Private Sub ComboBox3_Change()
    If ComboBox3.ListIndex = 0 Then Exit Sub

    Dim strFind As String
    Dim ws As Worksheet
    Dim rFound As Range

    Set ws = ThisWorkbook.Sheets("Year-to-Date Summary")

    strFind = ComboBox3.Value

    Set rFound = ws.Columns(3).Find(What:=strFind, LookIn:=xlValues, _
    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)

    If Not rFound Is Nothing Then
        TextBox4 = rFound.Offset(,1)
    Else
        MsgBox strFind & " cannot be found"
    End If
End Sub

如果您的范围是固定的,则更ws.Columns(3)改为ws.Range("C39:C49") 上面的代码。

如果您想使用工作表功能,那么您可以这样做(尝试和测试

注意:我没有在下面的代码中使用错误捕获。我相信你可以解决这个问题。

Private Sub ComboBox3_Change()
    If ComboBox3.ListIndex = 0 Then Exit Sub

    TextBox4.Text = Application.WorksheetFunction.VLookup( _
                    Val(ComboBox3.Value), _
                    Sheets("Year-to-Date Summary").Range("C39:D49"), _
                    2, _
                    False _
                    )
End Sub

请注意我们C39:C49 / Columns(3) + Offset在第一个示例中的使用方式以及我们C49:D49在第二个示例中的使用方式

编辑:我忘了评论On Error Resume Next从不使用它,除非需要。这就像告诉代码“闭嘴!” 如果发现错误:)

于 2012-11-22T13:25:37.747 回答
0

您的 ws 显然是一个字符串,它没有 .column 属性。
替换行ws = "Year-to-Date Summary"
set ws = worksheets("Year-to-Date Summary")看看是否有帮助。

您是否确保每个模块都有一个“ Option Explicit”并且您的应用程序编译成功?你会发现我认为的那个错误。

于 2012-11-22T11:17:13.147 回答
0
Private Sub ComboBox3_Change()

TextBox4.Text = WorksheetFunction.VLookup(Me.ComboBox3.Text, Sheets("Year-to-Date Summary").Range("$B$39:$C$49"), 2, False)

TextBox3.Text = WorksheetFunction.VLookup(Me.TextBox4.Text, Sheets("Year-to-Date Summary").Range("$C$39:$D$49"), 2, False)

End Sub

这对我有用,从组合框中做两个文本框

于 2012-11-22T12:49:01.383 回答
-1

私有子 ComboBox3_Change()

Dim strFind As String
Dim rFound As Range
Dim ws As Worksheet
Set ws = ActiveWorkbook.Sheets("Year-to-Date Summary")

If ComboBox3.ListIndex > -1 Then
    strFind = ComboBox3.text
    On Error Resume Next

'编辑删除整个 vlookup 部分...'添加 for 循环以搜索范围内的文本

    Set rSearch = ws.Range("B:C")            
            For Each rFound In rSearch
                If rFound.Value = strFind Then
                 TextBox4.Text = rFound.value
                else
                 MsgBox strFind & " cannot be found"
                 Exit Sub
                End If                
            Next rFound        
End Sub
于 2012-11-22T10:46:30.517 回答