ComboBox Userform list from Sheet2

Lost_User21

New Member
Joined
Feb 28, 2021
Messages
2
Office Version
  1. 2019
Platform
  1. Windows
Hi all, I am new to this and learning VBA. This may be basic but I can't get it -
I have UserForm1 that has ComboBox1 to select an address from a table in Sheet2.
I try to list data from Sheet2 but it just lists Sheet1 (or nothing). I used some lines I found on a video and it just works for data in Sheet1

Private Sub UserForm_Initialize()

Dim cntr As Integer
cntr = Application.WorksheetFunction.CountA(Range("A:A"))

For i = 1 to cntr
Me.ComboBox1.AddItem Cells (i, 1)
Next i
End Sub

I tried Sheet2.Range("A:A")) and worksheets("Sheet2").Range("A:A") but I can't get Sheet2.
I know its a matter of referencing Shee2 but I'm wracking my head.
Image is Sheet2 example
 

Attachments

  • Sheet2 example.PNG
    Sheet2 example.PNG
    6.5 KB · Views: 6

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
I should be Sheets("Sheet2").Range("A:A"). However, Worksheets("Sheet2").Range("A:A") should work too.

It is much easier to define like

Dim ws2 as Worksheet

then after all Dim were done just Declare

Set ws2 = ActiveWorkbook.Sheets("Sheet2")

By doing that, subsequently you can just call Sheet2 by using ws2.Range("A:A"). Less typing :) . In your case

VBA Code:
Private Sub UserForm_Initialize()

Dim cntr As Long
Dim ws2 As Worksheet

Set ws2 = ActiveWorkbook.Sheets("Sheet2")

cntr = Application.WorksheetFunction.CountA(ws2.Range("A:A"))

For i = 1 To cntr
    Me.ComboBox1.AddItem Cells(i, "A")
Next i

End Sub

Note that you can use Cells(i,"A") instead of Cells(i,1). Then no need to calculate for say column K
Forgot to tell you just forget of using Integer. Just use Long. Nowadays, computer will internally convert Integer to Long anyway.
 
Upvote 0
Rich (BB code):
Private Sub UserForm_Initialize()
    
    Dim cntr      As Integer
    cntr = Application.WorksheetFunction.CountA(Sheet2.Range("A:A"))
    
    For i = 1 To cntr
        Me.ComboBox1.AddItem Sheet2.Cells(i, 1)
    Next i
End Sub

Ur just this...
VBA Code:
Private Sub UserForm_Initialize()
    
    With Sheet2
        Me.ComboBox1.List = .Range("A1", .Range("A" & Rows.Count).End(xlUp)).Value
    End With
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,587
Messages
6,120,406
Members
448,958
Latest member
Hat4Life

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