Populate Combobox

ryancgarrett

Board Regular
Joined
Jun 18, 2011
Messages
122
I have the following code to populate some combo boxes on a form, however for some reason it always references the active sheet, rather than the "Data Sheet" like I want it to. Anyone see anything wrong?

Code:
Sub populateComboBox()
   Dim lr As Long
    
    With Sheets("Data Sheet")
        lr = BlankRow("Data Sheet", 2, 4) - 1
        
        frmTransactionEntry.cbxAccount1.RowSource = .Range("D2:D" & lr).Address(False, False)
        frmTransactionEntry.cbxAccount2.RowSource = .Range("D2:D" & lr).Address(False, False)
        frmTransactionEntry.cbxAccount3.RowSource = .Range("D2:D" & lr).Address(False, False)
        frmTransactionEntry.cbxAccount4.RowSource = .Range("D2:D" & lr).Address(False, False)
        frmTransactionEntry.cbxAccount5.RowSource = .Range("D2:D" & lr).Address(False, False)
        frmTransactionEntry.cbxAccount6.RowSource = .Range("D2:D" & lr).Address(False, False)
        
    End With
End Sub

'Function to find the end of a list
Function BlankRow(sName As String, sRow As Integer, sCol As Integer) As Integer
    Do Until Sheets(sName).Cells(sRow, sCol).Value = Empty
        sRow = sRow + 1
    Loop
    
    BlankRow = sRow
End Function
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
This is because Address property returns address WITHOUT sheet name!

Code:
[COLOR="Blue"]Sub[/COLOR] populateComboBox()
   
   [COLOR="Blue"]Dim[/COLOR] lr [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Long[/COLOR]
    
    [COLOR="Blue"]With[/COLOR] Sheets("Data Sheet")
        lr = .Range("D2").End(xlDown).Row
        frmTransactionEntry.cbxAccount1.RowSource = "'Data Sheet!'" & .Range("D2:D" & lr).Address([COLOR="Blue"]False[/COLOR], [COLOR="Blue"]False[/COLOR])
        frmTransactionEntry.cbxAccount2.RowSource = "'Data Sheet!'" & .Range("D2:D" & lr).Address([COLOR="Blue"]False[/COLOR], [COLOR="Blue"]False[/COLOR])
        frmTransactionEntry.cbxAccount3.RowSource = "'Data Sheet!'" & .Range("D2:D" & lr).Address([COLOR="Blue"]False[/COLOR], [COLOR="Blue"]False[/COLOR])
        frmTransactionEntry.cbxAccount4.RowSource = "'Data Sheet!'" & .Range("D2:D" & lr).Address([COLOR="Blue"]False[/COLOR], [COLOR="Blue"]False[/COLOR])
        frmTransactionEntry.cbxAccount5.RowSource = "'Data Sheet!'" & .Range("D2:D" & lr).Address([COLOR="Blue"]False[/COLOR], [COLOR="Blue"]False[/COLOR])
        frmTransactionEntry.cbxAccount6.RowSource = "'Data Sheet!'" & .Range("D2:D" & lr).Address([COLOR="Blue"]False[/COLOR], [COLOR="Blue"]False[/COLOR])
    [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]With[/COLOR]
    
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 
Upvote 0
Pardon? I posted fixed code in my previous post!
 
Upvote 0
Sorry, the code didn't load until I refreshed the page. I put in your changes, but I'm getting an error: Could not set the RowSource property. Invalid property value.
 
Upvote 0
Ah, yes... Mixed up positions... Thanks for reply!
 
Upvote 0

Forum statistics

Threads
1,224,552
Messages
6,179,487
Members
452,917
Latest member
MrsMSalt

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