code not workig my way

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
I have this code to populate my listbox. Instead on showing me the data as i have it on my sheet, it starts from the next column. I want it to show me the data starting from column B but it started with C.

I have got get it show the data the way it is on my sheet so that i can run the other codes because most of the events rely on the first column in the list box which is now not available.


Code:
Sub Lookup()
        Dim rngFind As Range
        Dim strFirstFind As String
        
        On Error GoTo errHandler:
        lstWin.Clear
        With Sheet1.Range("C3:C303")
        Set rngFind = .Find(txtLookup.Text, LookIn:=xlValues, lookat:=xlPart)
        If Not rngFind Is Nothing Then
        strFirstFind = rngFind.Address
        
        Do
        If rngFind.Row > 1 Then
        lstWin.AddItem rngFind.Value
        lstWin.List(lstWin.ListCount - 1, 1) = rngFind.Offset(0, 1)
        lstWin.List(lstWin.ListCount - 1, 2) = rngFind.Offset(0, 2)
        lstWin.List(lstWin.ListCount - 1, 3) = rngFind.Offset(0, 3)
        lstWin.List(lstWin.ListCount - 1, 4) = rngFind.Offset(0, 4)
        lstWin.List(lstWin.ListCount - 1, 5) = rngFind.Offset(0, 5)
        lstWin.List(lstWin.ListCount - 1, 6) = rngFind.Offset(0, 6)
        lstWin.List(lstWin.ListCount - 1, 7) = rngFind.Offset(0, 7)
        lstWin.List(lstWin.ListCount - 1, 8) = rngFind.Offset(0, 8)
        lstWin.List(lstWin.ListCount - 1, 9) = rngFind.Offset(0, 9)
        End If
        Set rngFind = .FindNext(rngFind)
        Loop While Not rngFind Is Nothing And rngFind.Address <> strFirstFind
        End If
        End With
        Me.Reg1.Enabled = False
        On Error GoTo 0
        Exit Sub
        
errHandler::
            MsgBox "Check your entry for errors"
    End Sub
 
To load a listbox with that many columns you need to use an array.
 
Upvote 0

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
As a possible quick-fix workaround, try inserting the code highlighted in red :

Code:
On Error GoTo errHandler:


[COLOR=#ff0000]lstWin.ColumnCount = 11[/COLOR]
[COLOR=#ff0000]Dim myArray As Variant[/COLOR]
[COLOR=#ff0000]myArray = [C2].Resize(, lstWin.ColumnCount + 1).Value[/COLOR]
[COLOR=#ff0000]lstWin.List = myArray[/COLOR]


lstWin.Clear
 
Upvote 0
Okay. We made it. Now all 11 columns are in.
I am very gratefull. One thing: can i still have the column heads???
 
Upvote 0
You can only have 'real' column headers if you use RowSource to populate the listbox.
 
Upvote 0

Forum statistics

Threads
1,223,642
Messages
6,173,512
Members
452,518
Latest member
SoerenB

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