Excel VBA populating a list box with values from selected sheet using loops
up vote
0
down vote
favorite
I have 2 listboxes in a userform. When the user clicks on an item (a worksheet name) in one of the boxes, the other list box should autopopulate with values in the D column of the specific worksheet they have selected. I also dont want any duplicate values to show up. How can i accomplish this by using loops?
excel vba excel-vba loops listbox
add a comment |
up vote
0
down vote
favorite
I have 2 listboxes in a userform. When the user clicks on an item (a worksheet name) in one of the boxes, the other list box should autopopulate with values in the D column of the specific worksheet they have selected. I also dont want any duplicate values to show up. How can i accomplish this by using loops?
excel vba excel-vba loops listbox
1
Welcome to Stack Overflow! Unfortunately, we won't write code for you. I recommend you read about Loops, Ranges or Cells and Listbox Items in VBA. If you get stuck on a specific problem, please come back with a specific example (including code) and we'll be glad help. Good luck!
– ale10ander
Nov 20 at 0:04
For example, you can get the data from D1 by using Sheets(1).Range("D1").value, and can move down the D column by using the Offset() function. Hopefully this helps get you started.
– ale10ander
Nov 20 at 0:05
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have 2 listboxes in a userform. When the user clicks on an item (a worksheet name) in one of the boxes, the other list box should autopopulate with values in the D column of the specific worksheet they have selected. I also dont want any duplicate values to show up. How can i accomplish this by using loops?
excel vba excel-vba loops listbox
I have 2 listboxes in a userform. When the user clicks on an item (a worksheet name) in one of the boxes, the other list box should autopopulate with values in the D column of the specific worksheet they have selected. I also dont want any duplicate values to show up. How can i accomplish this by using loops?
excel vba excel-vba loops listbox
excel vba excel-vba loops listbox
edited Nov 20 at 7:14
Pᴇʜ
19.8k42650
19.8k42650
asked Nov 19 at 23:21
Edrisa Gharibyar
1
1
1
Welcome to Stack Overflow! Unfortunately, we won't write code for you. I recommend you read about Loops, Ranges or Cells and Listbox Items in VBA. If you get stuck on a specific problem, please come back with a specific example (including code) and we'll be glad help. Good luck!
– ale10ander
Nov 20 at 0:04
For example, you can get the data from D1 by using Sheets(1).Range("D1").value, and can move down the D column by using the Offset() function. Hopefully this helps get you started.
– ale10ander
Nov 20 at 0:05
add a comment |
1
Welcome to Stack Overflow! Unfortunately, we won't write code for you. I recommend you read about Loops, Ranges or Cells and Listbox Items in VBA. If you get stuck on a specific problem, please come back with a specific example (including code) and we'll be glad help. Good luck!
– ale10ander
Nov 20 at 0:04
For example, you can get the data from D1 by using Sheets(1).Range("D1").value, and can move down the D column by using the Offset() function. Hopefully this helps get you started.
– ale10ander
Nov 20 at 0:05
1
1
Welcome to Stack Overflow! Unfortunately, we won't write code for you. I recommend you read about Loops, Ranges or Cells and Listbox Items in VBA. If you get stuck on a specific problem, please come back with a specific example (including code) and we'll be glad help. Good luck!
– ale10ander
Nov 20 at 0:04
Welcome to Stack Overflow! Unfortunately, we won't write code for you. I recommend you read about Loops, Ranges or Cells and Listbox Items in VBA. If you get stuck on a specific problem, please come back with a specific example (including code) and we'll be glad help. Good luck!
– ale10ander
Nov 20 at 0:04
For example, you can get the data from D1 by using Sheets(1).Range("D1").value, and can move down the D column by using the Offset() function. Hopefully this helps get you started.
– ale10ander
Nov 20 at 0:05
For example, you can get the data from D1 by using Sheets(1).Range("D1").value, and can move down the D column by using the Offset() function. Hopefully this helps get you started.
– ale10ander
Nov 20 at 0:05
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
Well, a few things are going to have to happen here.
Since you failed to state the names of your listboxes, we are using the generic forms:
ListBox1
is the list box with your sheet names
ListBox2
is the list box to auto-populate from column D
First, you will need to ensure that listBox1
is initialized with your worksheet values. A sample of how you could do this is:
Private Sub UserForm_Initialize()
'MUST BE PLACED IN USERFORM CODE MODULE!!
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
Me.ListBox1.AddItem ws.Name
Next ws
End Sub
The above is not required for your task, but your userform being pre-populated is required.
Now you are going to want to watch for the ListBox1_Change()
event. Again, if your listbox isn't named ListBox1
, then you will need to change this in the Sub Name.
Private Sub ListBox1_Change()
'MUST BE PLACED IN USERFORM CODE MODULE!!
Dim wsSelected As Worksheet
Set wsSelected = ThisWorkbook.Worksheets(Me.ListBox1.Value)
With wsSelected
Me.ListBox2.List = rngToUniqueArr(.Range(.Cells(1, "D"), .Cells( _
lastRow(wsSelected, "D"), "D")))
End With
End Sub
Function rngToUniqueArr(ByVal rng As Range) As Variant
'Reference to [Microsoft Scripting Runtime] Required
Dim dict As New Scripting.Dictionary, cel As Range
For Each cel In rng.Cells
dict(cel.Value) = 1
Next cel
rngToUniqueArr = dict.Keys
End Function
Function lastRow(ws As Worksheet, Optional col As Variant = 1) As Long
With ws
lastRow = .Cells(.Rows.Count, col).End(xlUp).Row
End With
End Function
The sub rngToUniqueArr
is something I have stored in my PERSONAL.XLSB workbook, it's a useful function to have. This will take an input range and create a dictionary of unique values, and output into an array.
The Above requires a reference to be set to
Microsoft Scripting Runtime
in Tools > References
In your ListBox1_Change()
event, the wsSelected
is the worksheet you selected from ListBox1
, and you are adding the array from the rngToUniqueArr
into ListBox2
.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Well, a few things are going to have to happen here.
Since you failed to state the names of your listboxes, we are using the generic forms:
ListBox1
is the list box with your sheet names
ListBox2
is the list box to auto-populate from column D
First, you will need to ensure that listBox1
is initialized with your worksheet values. A sample of how you could do this is:
Private Sub UserForm_Initialize()
'MUST BE PLACED IN USERFORM CODE MODULE!!
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
Me.ListBox1.AddItem ws.Name
Next ws
End Sub
The above is not required for your task, but your userform being pre-populated is required.
Now you are going to want to watch for the ListBox1_Change()
event. Again, if your listbox isn't named ListBox1
, then you will need to change this in the Sub Name.
Private Sub ListBox1_Change()
'MUST BE PLACED IN USERFORM CODE MODULE!!
Dim wsSelected As Worksheet
Set wsSelected = ThisWorkbook.Worksheets(Me.ListBox1.Value)
With wsSelected
Me.ListBox2.List = rngToUniqueArr(.Range(.Cells(1, "D"), .Cells( _
lastRow(wsSelected, "D"), "D")))
End With
End Sub
Function rngToUniqueArr(ByVal rng As Range) As Variant
'Reference to [Microsoft Scripting Runtime] Required
Dim dict As New Scripting.Dictionary, cel As Range
For Each cel In rng.Cells
dict(cel.Value) = 1
Next cel
rngToUniqueArr = dict.Keys
End Function
Function lastRow(ws As Worksheet, Optional col As Variant = 1) As Long
With ws
lastRow = .Cells(.Rows.Count, col).End(xlUp).Row
End With
End Function
The sub rngToUniqueArr
is something I have stored in my PERSONAL.XLSB workbook, it's a useful function to have. This will take an input range and create a dictionary of unique values, and output into an array.
The Above requires a reference to be set to
Microsoft Scripting Runtime
in Tools > References
In your ListBox1_Change()
event, the wsSelected
is the worksheet you selected from ListBox1
, and you are adding the array from the rngToUniqueArr
into ListBox2
.
add a comment |
up vote
1
down vote
Well, a few things are going to have to happen here.
Since you failed to state the names of your listboxes, we are using the generic forms:
ListBox1
is the list box with your sheet names
ListBox2
is the list box to auto-populate from column D
First, you will need to ensure that listBox1
is initialized with your worksheet values. A sample of how you could do this is:
Private Sub UserForm_Initialize()
'MUST BE PLACED IN USERFORM CODE MODULE!!
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
Me.ListBox1.AddItem ws.Name
Next ws
End Sub
The above is not required for your task, but your userform being pre-populated is required.
Now you are going to want to watch for the ListBox1_Change()
event. Again, if your listbox isn't named ListBox1
, then you will need to change this in the Sub Name.
Private Sub ListBox1_Change()
'MUST BE PLACED IN USERFORM CODE MODULE!!
Dim wsSelected As Worksheet
Set wsSelected = ThisWorkbook.Worksheets(Me.ListBox1.Value)
With wsSelected
Me.ListBox2.List = rngToUniqueArr(.Range(.Cells(1, "D"), .Cells( _
lastRow(wsSelected, "D"), "D")))
End With
End Sub
Function rngToUniqueArr(ByVal rng As Range) As Variant
'Reference to [Microsoft Scripting Runtime] Required
Dim dict As New Scripting.Dictionary, cel As Range
For Each cel In rng.Cells
dict(cel.Value) = 1
Next cel
rngToUniqueArr = dict.Keys
End Function
Function lastRow(ws As Worksheet, Optional col As Variant = 1) As Long
With ws
lastRow = .Cells(.Rows.Count, col).End(xlUp).Row
End With
End Function
The sub rngToUniqueArr
is something I have stored in my PERSONAL.XLSB workbook, it's a useful function to have. This will take an input range and create a dictionary of unique values, and output into an array.
The Above requires a reference to be set to
Microsoft Scripting Runtime
in Tools > References
In your ListBox1_Change()
event, the wsSelected
is the worksheet you selected from ListBox1
, and you are adding the array from the rngToUniqueArr
into ListBox2
.
add a comment |
up vote
1
down vote
up vote
1
down vote
Well, a few things are going to have to happen here.
Since you failed to state the names of your listboxes, we are using the generic forms:
ListBox1
is the list box with your sheet names
ListBox2
is the list box to auto-populate from column D
First, you will need to ensure that listBox1
is initialized with your worksheet values. A sample of how you could do this is:
Private Sub UserForm_Initialize()
'MUST BE PLACED IN USERFORM CODE MODULE!!
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
Me.ListBox1.AddItem ws.Name
Next ws
End Sub
The above is not required for your task, but your userform being pre-populated is required.
Now you are going to want to watch for the ListBox1_Change()
event. Again, if your listbox isn't named ListBox1
, then you will need to change this in the Sub Name.
Private Sub ListBox1_Change()
'MUST BE PLACED IN USERFORM CODE MODULE!!
Dim wsSelected As Worksheet
Set wsSelected = ThisWorkbook.Worksheets(Me.ListBox1.Value)
With wsSelected
Me.ListBox2.List = rngToUniqueArr(.Range(.Cells(1, "D"), .Cells( _
lastRow(wsSelected, "D"), "D")))
End With
End Sub
Function rngToUniqueArr(ByVal rng As Range) As Variant
'Reference to [Microsoft Scripting Runtime] Required
Dim dict As New Scripting.Dictionary, cel As Range
For Each cel In rng.Cells
dict(cel.Value) = 1
Next cel
rngToUniqueArr = dict.Keys
End Function
Function lastRow(ws As Worksheet, Optional col As Variant = 1) As Long
With ws
lastRow = .Cells(.Rows.Count, col).End(xlUp).Row
End With
End Function
The sub rngToUniqueArr
is something I have stored in my PERSONAL.XLSB workbook, it's a useful function to have. This will take an input range and create a dictionary of unique values, and output into an array.
The Above requires a reference to be set to
Microsoft Scripting Runtime
in Tools > References
In your ListBox1_Change()
event, the wsSelected
is the worksheet you selected from ListBox1
, and you are adding the array from the rngToUniqueArr
into ListBox2
.
Well, a few things are going to have to happen here.
Since you failed to state the names of your listboxes, we are using the generic forms:
ListBox1
is the list box with your sheet names
ListBox2
is the list box to auto-populate from column D
First, you will need to ensure that listBox1
is initialized with your worksheet values. A sample of how you could do this is:
Private Sub UserForm_Initialize()
'MUST BE PLACED IN USERFORM CODE MODULE!!
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
Me.ListBox1.AddItem ws.Name
Next ws
End Sub
The above is not required for your task, but your userform being pre-populated is required.
Now you are going to want to watch for the ListBox1_Change()
event. Again, if your listbox isn't named ListBox1
, then you will need to change this in the Sub Name.
Private Sub ListBox1_Change()
'MUST BE PLACED IN USERFORM CODE MODULE!!
Dim wsSelected As Worksheet
Set wsSelected = ThisWorkbook.Worksheets(Me.ListBox1.Value)
With wsSelected
Me.ListBox2.List = rngToUniqueArr(.Range(.Cells(1, "D"), .Cells( _
lastRow(wsSelected, "D"), "D")))
End With
End Sub
Function rngToUniqueArr(ByVal rng As Range) As Variant
'Reference to [Microsoft Scripting Runtime] Required
Dim dict As New Scripting.Dictionary, cel As Range
For Each cel In rng.Cells
dict(cel.Value) = 1
Next cel
rngToUniqueArr = dict.Keys
End Function
Function lastRow(ws As Worksheet, Optional col As Variant = 1) As Long
With ws
lastRow = .Cells(.Rows.Count, col).End(xlUp).Row
End With
End Function
The sub rngToUniqueArr
is something I have stored in my PERSONAL.XLSB workbook, it's a useful function to have. This will take an input range and create a dictionary of unique values, and output into an array.
The Above requires a reference to be set to
Microsoft Scripting Runtime
in Tools > References
In your ListBox1_Change()
event, the wsSelected
is the worksheet you selected from ListBox1
, and you are adding the array from the rngToUniqueArr
into ListBox2
.
answered Nov 20 at 0:02
K.Dᴀᴠɪs
6,295112140
6,295112140
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384064%2fexcel-vba-populating-a-list-box-with-values-from-selected-sheet-using-loops%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Welcome to Stack Overflow! Unfortunately, we won't write code for you. I recommend you read about Loops, Ranges or Cells and Listbox Items in VBA. If you get stuck on a specific problem, please come back with a specific example (including code) and we'll be glad help. Good luck!
– ale10ander
Nov 20 at 0:04
For example, you can get the data from D1 by using Sheets(1).Range("D1").value, and can move down the D column by using the Offset() function. Hopefully this helps get you started.
– ale10ander
Nov 20 at 0:05