Consulting

Results 1 to 14 of 14

Thread: Display cell value into a textbox

  1. #1
    VBAX Regular
    Joined
    Oct 2007
    Posts
    97
    Location

    Display cell value into a textbox

    Hi guys,
    This thing is frustrating.... I am not understanding what is wrong.
    Here is my code.
    [VBA]Private Sub ComboBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Dim RowNumm As Variant
    On Error Resume Next
    With Worksheets("Vol_data")
    RowNumm = Application.Match(ComboBox1.SelText, .Column(3), 0)
    TextBox1.Text = .Cells(RowNumm, "B").Value
    End With
    End Sub[/VBA]

    The code is supposed to take a slected item from a combobox, make a match in sheet "Vol_data" ColumnA, and display in textbox1 the value that exist in the ame row, but column B.

    Can anyone please help me.

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    If this is in a userform, I'd add specificity. I'd also change the property being returned from the combo box.
    Private Sub ComboBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean) 
        Dim RowNumm As Long 
        On Error Resume Next 
        With ThisWorkbook.Worksheets("Vol_data") 
            RowNumm = Application.Match(Me.ComboBox1.Value, .Column(3), 0) 
            Me.TextBox1.Text = .Cells(RowNumm, "B").Value 
        End With
        On Error Goto 0 
    End Sub
    one more possibility, comboBoxes return Strings, if Vol_Data! C:C has numbers, use the Val function on the first argument of the Match.

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Private Sub ComboBox1_Change()
    Dim RowNumm As Variant
    On Error Resume Next
    With Worksheets("Vol_data")
    RowNumm = Application.Match(ComboBox1.Value, .Columns(3), 0)
    TextBox1.Text = .Cells(RowNumm, "B").Value
    End With
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Regular
    Joined
    Oct 2007
    Posts
    97
    Location
    I tried that...
    RowNumm is not the right number, it is supposed to be 2 and I am getting 0.
    So I tried a different Dim, I got empty for RowNumm.
    I don't know what that means, but it might be a clue.

  5. #5
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    You tried which? Did you try

    [VBA]RowNumm = Application.Match(Val(Me.ComboBox1.Value), .Column(3), 0)[/VBA]

    Inside the On Error Resume Next, RowNumm will behave that way if the Match finds nothing and returns an error value.

  6. #6
    VBAX Regular
    Joined
    Oct 2007
    Posts
    97
    Location
    Hi Xld,
    I run the code, while debuging, I have error 2042 on RowNumm.

  7. #7
    VBAX Regular
    Joined
    Oct 2007
    Posts
    97
    Location
    Yep I tried that too. [Mikerickson]

  8. #8
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Eureka!

    Columns(3)

  9. #9
    VBAX Regular
    Joined
    Oct 2007
    Posts
    97
    Location
    Nope!!

  10. #10
    VBAX Regular
    Joined
    Oct 2007
    Posts
    97
    Location
    I have aquestion. what i sthe difference between .columns(3) and .columns(2)?

  11. #11
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    The code in the OP has "column" (no "s") which is not a property of a worksheet.
    "columns" (with the "s") IS a property of a worksheet.

    A simple typo hidden by the On Error Resume Next. And caught by XLD, but not noticed by me.

  12. #12
    VBAX Regular
    Joined
    Oct 2007
    Posts
    97
    Location
    Sorry Guys,
    The reason was That I was using the sentence without even knowing the meaning of it.
    Since I need the search to occur in the first column, I have to use .columns(1)

    Thanks Mikerickson, it worked.

    Xld, I am not sure why it is not working.

  13. #13
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Merry Christmas


  14. #14
    VBAX Regular
    Joined
    Oct 2007
    Posts
    97
    Location
    Thank you.
    Merry christmas to all of you.
    May God always bless you guys.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •