Combobox value to display adjacent cells in textboxes

chroniclesofdave

New Member
Joined
Aug 8, 2016
Messages
48
I have the following code from a previous project i worked on and tried to modify it to find the value of a cell in column A and if the value matches, then display the two adjacent cell values in the two textboxes i have on the user form:
VBA Code:
Dim i As Integer
Dim arrIn As Variant
Dim arrOut()
Dim j As Long
Dim counter As Long
Dim outRow As Long
Dim sUser As String
sUser = LCase(ComboBox2.Value)
With Sheets("Sheet3").Range("A1").CurrentRegion
    arrIn = .Value
    counter = Application.CountIf(.Columns(2), sUser)
    For i = 1 To UBound(arrIn)
        If VBA.LCase$(arrIn(i, 2)) = sUser Then
            For j = 1 To UBound(arrIn, 2) - 1
               outRow = outRow + 1
               Me.Controls("textbox" & outRow) = arrIn(i, j)
            Next j
        End If
    Next i
        End With
The value is being pulled from combobox2 and the textboxes that should be receiving the data are TextBox723 and TextBox724.
While the above code does not throw any errors, the data is not displaying on my userform. Any ideas?
Edit: The values of Combobox2 are coming from Sheet3 column A, and the two adjacent columns, B and C and what I am trying to have it pull and display.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
This line
VBA Code:
If VBA.LCase$(arrIn(i, 2)) = sUser Then
is checking column 2 not column 1.
 
Upvote 0
So i modified the code to look like this:
VBA Code:
Private Sub CommandButton124_Click()
Dim i As Integer
Dim arrIn As Variant
Dim arrOut()
Dim j As Long
Dim counter As Long
Dim outRow As Long
Dim sUser As String
sUser = LCase(ComboBox2.Value)
With Sheets("Sheet3").Range("A1").CurrentRegion
    arrIn = .Value
    counter = Application.CountIf(.Columns(2), sUser)
    For i = 1 To UBound(arrIn)
        If VBA.LCase$(arrIn(i, 1)) = sUser Then
            For j = 1 To UBound(arrIn, 2) - 1
               outRow = outRow + 1
               Me.Controls("textbox" & outRow) = arrIn(i, j)
            Next j
        End If
    Next i
        End With
End Sub
However it is still not displaying the information in the text boxes.
 
Upvote 0
Step through the code using F8 & check to see if if you are entering the For j= loop.
Also do you get any error messages?
 
Upvote 0
It's still not throwing any errors, and I tried to f8 it to see if it is running through the code, but honestly nothing really happens. it didn't even highlight anything in yellow indicating it was processing the code.
 
Upvote 0
So quick update, i modified the code to reflect something close to what i originally was using:
VBA Code:
Private Sub CommandButton124_Click()
Dim i As Integer
Dim arrIn As Variant
Dim arrOut()
Dim j As Long
Dim counter As Long
Dim outRow As Long
Dim sUser As String
sUser = LCase(ComboBox2.Value)
With Sheets("Sheet3").Range("A1").CurrentRegion
    arrIn = .Value
    counter = Application.CountIf(.Columns(2), sUser)
    For i = 1 To UBound(arrIn)
        If VBA.LCase$(arrIn(i, 1)) = sUser Then
            For j = 1 To UBound(arrIn, 2) - 1
               outRow = outRow + 1
               Me.Controls("textbox" & outRow) = arrIn(i, j)
            Next j
        End If
    Next i
        End With
        On Error Resume Next
        Application.Visible = False
UserForm1.Show
End Sub
And now it is displaying the information, but it is displaying in the wrong text box, it is showing up in the first one in order, can i get it to push to TextBox723 and TextBox724
 
Last edited:
Upvote 0
Change
VBA Code:
            For j = 1 To UBound(arrIn, 2) - 1
               outRow = outRow + 1
               Me.Controls("textbox" & outRow) = arrIn(i, j)
            Next j
to
VBA Code:
Me.TextBox723=arrIn(i,1)
Me.TextBox724=arrIn(i,2)
 
Upvote 0
It worked! I had to change it to
VBA Code:
Me.TextBox723=arrIn(i,2)
Me.TextBox724=arrIn(i,3)
But that worked perfectly! Thanks for all the help!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,300
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top