Excel Listbox with headers and dynamic rowsource assignment

Cbrine

Well-known Member
Joined
Dec 2, 2003
Messages
3,196
It's been a while since I last posted here, hope all the old chums are doing well. Ran into a small issue with a solution that I'm putting together here at work, and I'm hoping someone has encountered this issue and found a resolution so that I don't have to.

I'm trying to dynamically load data into an excel listbox control, and then using vba I'm changing the rowsource to the data added. The issue is that, even though I have the Headers turned on, when I dynamically change the row source, the headers are not appearing the heading until I close and then reopen the form. Once I reopen the form, eventhing looks perfect. I've tried to repaint the control and the form, and that doesn't seem to work. Hopefully someone can help me out a bit. Here's the code I'm currently using.

Code:
Private Sub CommandButton5_Click()
Dim oConn As New ADODB.Connection, rs As New ADODB.Recordset, rs2 As New ADODB.Recordset
Dim wb As Workbook, Twf As Worksheet

oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
           "Data Source=" & ActiveWorkbook.Path & "\" & ActiveWorkbook.Name & ";" & _
           "Extended Properties=""Excel 8.0;HDR=No"""
           
Set wb = ActiveWorkbook
Set Twf = wb.Sheets("Found Data")
           
'Search General Survey Title Values.
rs.Open "select F1, F2, F3, Len(F1) as lenF1 from [Job Information$A:C] where ucase(F2) like '%" & UCase(TextBox1.Text) & "%' and len(F1)>3;", oConn, adOpenStatic, adLockOptimistic

'Clear Current Found Data
If Twf.Range("A2") <> "" Then
    Twf.Range("A2", Twf.Range("A" & Twf.Rows.Count).End(xlUp).Offset(1, 20)).ClearContents
End If

Do Until rs.EOF
    'Open recordset to detailed data for found Job code and populate found sheet for inclusion into listbox control
    rs2.Open "select * from [Raw Data$A:EZ] where F3='" & rs(0) & "'", oConn, adOpenStatic, adLockOptimistic
        Do Until rs2.EOF
            Twf.Range("A" & Twf.Rows.Count).End(xlUp).Offset(1, 0) = rs(0)
            Twf.Range("A" & Twf.Rows.Count).End(xlUp).Offset(0, 1) = rs(1)
            Twf.Range("A" & Twf.Rows.Count).End(xlUp).Offset(0, 2) = rs2(4)
            Twf.Range("A" & Twf.Rows.Count).End(xlUp).Offset(0, 3) = rs2(5)
            rs2.MoveNext
        Loop
        Set rs2 = Nothing
    rs.MoveNext
Loop

ListBox1.RowSource = "='" & Twf.Name & "'!A1:" & Twf.Range("A" & Twf.Rows.Count).End(xlUp).Offset(0, 3).Address


Set rs = Nothing
Set oConn = Nothing
End Sub

Thanks in advance!
cbrine
 
Last edited:

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

Forum statistics

Threads
1,214,987
Messages
6,122,614
Members
449,090
Latest member
vivek chauhan

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