Adding New Worksheets from a List Box Collection if Specific Value Selected
Some background: there are 2 worksheets, one with a list of employee names/information, and one with delivery records (each delivery was made by one of the employees on the other sheet)
I have made a listbox with the employee names off the first sheet. Based on the selection from the listbox, I would like to insert a new worksheet for the employee selected that lists all the deliveries they made (off the delivery sheet). There is also an "all employees" option in the listbox that should insert a sheet for every employee.
excel vba excel-vba listbox userform
add a comment |
Some background: there are 2 worksheets, one with a list of employee names/information, and one with delivery records (each delivery was made by one of the employees on the other sheet)
I have made a listbox with the employee names off the first sheet. Based on the selection from the listbox, I would like to insert a new worksheet for the employee selected that lists all the deliveries they made (off the delivery sheet). There is also an "all employees" option in the listbox that should insert a sheet for every employee.
excel vba excel-vba listbox userform
add a comment |
Some background: there are 2 worksheets, one with a list of employee names/information, and one with delivery records (each delivery was made by one of the employees on the other sheet)
I have made a listbox with the employee names off the first sheet. Based on the selection from the listbox, I would like to insert a new worksheet for the employee selected that lists all the deliveries they made (off the delivery sheet). There is also an "all employees" option in the listbox that should insert a sheet for every employee.
excel vba excel-vba listbox userform
Some background: there are 2 worksheets, one with a list of employee names/information, and one with delivery records (each delivery was made by one of the employees on the other sheet)
I have made a listbox with the employee names off the first sheet. Based on the selection from the listbox, I would like to insert a new worksheet for the employee selected that lists all the deliveries they made (off the delivery sheet). There is also an "all employees" option in the listbox that should insert a sheet for every employee.
excel vba excel-vba listbox userform
excel vba excel-vba listbox userform
edited Nov 24 '18 at 1:36
K.Dᴀᴠɪs
7,051112239
7,051112239
asked Nov 24 '18 at 1:07
tetrikertetriker
274
274
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
It would probably be better to have your "All Employees" selection as a checkbox rather than a value in your listbox -- but that is just personal preference. If you decided to go this route, you can use the CheckBox_Click
event to disable your listbox completely if it was checked.
Anyways, to your actual question. You can first check if the value of your list box = All Employees
using this line of code:
If Me.ListBox1.Value = "All Employees" Then
Once you have verified the above is true, then you can get the total count of the items in your listbox with .ListCount
and loop through each list item:
For i = 0 To .ListCount - 1
For each item in the loop, ensure that you are not adding a worksheet named All Employees
:
If .List(i) <> "All Employees" Then
and once that final test passes, create a new worksheet and name it to the value of your list box:
Private Sub CommandButton1_Click()
With Me.ListBox1
If .Value = "All Employees" Then
Dim i As Long
For i = 0 To .ListCount - 1
If .List(i) <> "All Employees" Then
ThisWorkbook.Worksheets.Add.Name = .List(i)
End If
Next
End If
End With
End Sub
I assume this should be checked when you press a command button - so I've added it to that particular event.
Miscellaneous Notes
- Since you didn't mention the name of your listbox, I used the generic name
ListBox1
in the code. You will have to change that to the name of your actual listbox if it's different. - You may also have to update the name of your
CommandButton1
in the above code if the button's name you press to populate the sheets is different.
1
Thanks so much, this helped a lot and worked perfectly! Using that same structure I was able to add in an ELSE with the code for other listbox selections too.
– tetriker
Nov 24 '18 at 2:03
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%2f53454343%2fadding-new-worksheets-from-a-list-box-collection-if-specific-value-selected%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
It would probably be better to have your "All Employees" selection as a checkbox rather than a value in your listbox -- but that is just personal preference. If you decided to go this route, you can use the CheckBox_Click
event to disable your listbox completely if it was checked.
Anyways, to your actual question. You can first check if the value of your list box = All Employees
using this line of code:
If Me.ListBox1.Value = "All Employees" Then
Once you have verified the above is true, then you can get the total count of the items in your listbox with .ListCount
and loop through each list item:
For i = 0 To .ListCount - 1
For each item in the loop, ensure that you are not adding a worksheet named All Employees
:
If .List(i) <> "All Employees" Then
and once that final test passes, create a new worksheet and name it to the value of your list box:
Private Sub CommandButton1_Click()
With Me.ListBox1
If .Value = "All Employees" Then
Dim i As Long
For i = 0 To .ListCount - 1
If .List(i) <> "All Employees" Then
ThisWorkbook.Worksheets.Add.Name = .List(i)
End If
Next
End If
End With
End Sub
I assume this should be checked when you press a command button - so I've added it to that particular event.
Miscellaneous Notes
- Since you didn't mention the name of your listbox, I used the generic name
ListBox1
in the code. You will have to change that to the name of your actual listbox if it's different. - You may also have to update the name of your
CommandButton1
in the above code if the button's name you press to populate the sheets is different.
1
Thanks so much, this helped a lot and worked perfectly! Using that same structure I was able to add in an ELSE with the code for other listbox selections too.
– tetriker
Nov 24 '18 at 2:03
add a comment |
It would probably be better to have your "All Employees" selection as a checkbox rather than a value in your listbox -- but that is just personal preference. If you decided to go this route, you can use the CheckBox_Click
event to disable your listbox completely if it was checked.
Anyways, to your actual question. You can first check if the value of your list box = All Employees
using this line of code:
If Me.ListBox1.Value = "All Employees" Then
Once you have verified the above is true, then you can get the total count of the items in your listbox with .ListCount
and loop through each list item:
For i = 0 To .ListCount - 1
For each item in the loop, ensure that you are not adding a worksheet named All Employees
:
If .List(i) <> "All Employees" Then
and once that final test passes, create a new worksheet and name it to the value of your list box:
Private Sub CommandButton1_Click()
With Me.ListBox1
If .Value = "All Employees" Then
Dim i As Long
For i = 0 To .ListCount - 1
If .List(i) <> "All Employees" Then
ThisWorkbook.Worksheets.Add.Name = .List(i)
End If
Next
End If
End With
End Sub
I assume this should be checked when you press a command button - so I've added it to that particular event.
Miscellaneous Notes
- Since you didn't mention the name of your listbox, I used the generic name
ListBox1
in the code. You will have to change that to the name of your actual listbox if it's different. - You may also have to update the name of your
CommandButton1
in the above code if the button's name you press to populate the sheets is different.
1
Thanks so much, this helped a lot and worked perfectly! Using that same structure I was able to add in an ELSE with the code for other listbox selections too.
– tetriker
Nov 24 '18 at 2:03
add a comment |
It would probably be better to have your "All Employees" selection as a checkbox rather than a value in your listbox -- but that is just personal preference. If you decided to go this route, you can use the CheckBox_Click
event to disable your listbox completely if it was checked.
Anyways, to your actual question. You can first check if the value of your list box = All Employees
using this line of code:
If Me.ListBox1.Value = "All Employees" Then
Once you have verified the above is true, then you can get the total count of the items in your listbox with .ListCount
and loop through each list item:
For i = 0 To .ListCount - 1
For each item in the loop, ensure that you are not adding a worksheet named All Employees
:
If .List(i) <> "All Employees" Then
and once that final test passes, create a new worksheet and name it to the value of your list box:
Private Sub CommandButton1_Click()
With Me.ListBox1
If .Value = "All Employees" Then
Dim i As Long
For i = 0 To .ListCount - 1
If .List(i) <> "All Employees" Then
ThisWorkbook.Worksheets.Add.Name = .List(i)
End If
Next
End If
End With
End Sub
I assume this should be checked when you press a command button - so I've added it to that particular event.
Miscellaneous Notes
- Since you didn't mention the name of your listbox, I used the generic name
ListBox1
in the code. You will have to change that to the name of your actual listbox if it's different. - You may also have to update the name of your
CommandButton1
in the above code if the button's name you press to populate the sheets is different.
It would probably be better to have your "All Employees" selection as a checkbox rather than a value in your listbox -- but that is just personal preference. If you decided to go this route, you can use the CheckBox_Click
event to disable your listbox completely if it was checked.
Anyways, to your actual question. You can first check if the value of your list box = All Employees
using this line of code:
If Me.ListBox1.Value = "All Employees" Then
Once you have verified the above is true, then you can get the total count of the items in your listbox with .ListCount
and loop through each list item:
For i = 0 To .ListCount - 1
For each item in the loop, ensure that you are not adding a worksheet named All Employees
:
If .List(i) <> "All Employees" Then
and once that final test passes, create a new worksheet and name it to the value of your list box:
Private Sub CommandButton1_Click()
With Me.ListBox1
If .Value = "All Employees" Then
Dim i As Long
For i = 0 To .ListCount - 1
If .List(i) <> "All Employees" Then
ThisWorkbook.Worksheets.Add.Name = .List(i)
End If
Next
End If
End With
End Sub
I assume this should be checked when you press a command button - so I've added it to that particular event.
Miscellaneous Notes
- Since you didn't mention the name of your listbox, I used the generic name
ListBox1
in the code. You will have to change that to the name of your actual listbox if it's different. - You may also have to update the name of your
CommandButton1
in the above code if the button's name you press to populate the sheets is different.
edited Nov 24 '18 at 1:39
answered Nov 24 '18 at 1:32
K.DᴀᴠɪsK.Dᴀᴠɪs
7,051112239
7,051112239
1
Thanks so much, this helped a lot and worked perfectly! Using that same structure I was able to add in an ELSE with the code for other listbox selections too.
– tetriker
Nov 24 '18 at 2:03
add a comment |
1
Thanks so much, this helped a lot and worked perfectly! Using that same structure I was able to add in an ELSE with the code for other listbox selections too.
– tetriker
Nov 24 '18 at 2:03
1
1
Thanks so much, this helped a lot and worked perfectly! Using that same structure I was able to add in an ELSE with the code for other listbox selections too.
– tetriker
Nov 24 '18 at 2:03
Thanks so much, this helped a lot and worked perfectly! Using that same structure I was able to add in an ELSE with the code for other listbox selections too.
– tetriker
Nov 24 '18 at 2:03
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.
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%2f53454343%2fadding-new-worksheets-from-a-list-box-collection-if-specific-value-selected%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