Excel VBA populating a list box with values from selected sheet using loops
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 |
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 |
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ᴇʜ
20.1k42650
20.1k42650
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
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 |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 |
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 |
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,807112139
6,807112139
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