Using error handling to delete sheets based on listbox value





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















So I have a listbox with a number of names and one value which is an "all names" option. When a name is selected, a worksheet is added with information about this name. When "all names" are selected, multiple worksheets are added, one for each name.



I am trying to put in some error handling that will delete the existing name sheet if someone selects the same one twice. I was able to get the code working for the "all names" function, to where it will delete all sheets except the dashboards/background data and then re-insert the name sheets. However, I am having trouble getting it to work where one single name is selected.



Here is my error handling code. I have it go to this OnError. For the second part, I am just using one of the potential name values, John. The sheet name and listbox value would be the same.



ErrorHandling:

If (Err.Number = 1004 And EmpListBox.Value = "All Names")Then
For Each ws In Worksheets
Select Case ws.Name
Case "Dashboard", "Data", "Employees"
Case Else
ws.Delete
End Select
Next
'this is where it is not working
ElseIf (Err.Number = 1004 And EmpListBox.Value = "John") Then
Sheets("John").Delete
Resume Main
End If









share|improve this question























  • Use a function that returns a boolean if sheet exists and call that stackoverflow.com/q/6688131/6241235 you can then probably re-write the above and remove the current error handling.

    – QHarr
    Nov 25 '18 at 4:44




















1















So I have a listbox with a number of names and one value which is an "all names" option. When a name is selected, a worksheet is added with information about this name. When "all names" are selected, multiple worksheets are added, one for each name.



I am trying to put in some error handling that will delete the existing name sheet if someone selects the same one twice. I was able to get the code working for the "all names" function, to where it will delete all sheets except the dashboards/background data and then re-insert the name sheets. However, I am having trouble getting it to work where one single name is selected.



Here is my error handling code. I have it go to this OnError. For the second part, I am just using one of the potential name values, John. The sheet name and listbox value would be the same.



ErrorHandling:

If (Err.Number = 1004 And EmpListBox.Value = "All Names")Then
For Each ws In Worksheets
Select Case ws.Name
Case "Dashboard", "Data", "Employees"
Case Else
ws.Delete
End Select
Next
'this is where it is not working
ElseIf (Err.Number = 1004 And EmpListBox.Value = "John") Then
Sheets("John").Delete
Resume Main
End If









share|improve this question























  • Use a function that returns a boolean if sheet exists and call that stackoverflow.com/q/6688131/6241235 you can then probably re-write the above and remove the current error handling.

    – QHarr
    Nov 25 '18 at 4:44
















1












1








1








So I have a listbox with a number of names and one value which is an "all names" option. When a name is selected, a worksheet is added with information about this name. When "all names" are selected, multiple worksheets are added, one for each name.



I am trying to put in some error handling that will delete the existing name sheet if someone selects the same one twice. I was able to get the code working for the "all names" function, to where it will delete all sheets except the dashboards/background data and then re-insert the name sheets. However, I am having trouble getting it to work where one single name is selected.



Here is my error handling code. I have it go to this OnError. For the second part, I am just using one of the potential name values, John. The sheet name and listbox value would be the same.



ErrorHandling:

If (Err.Number = 1004 And EmpListBox.Value = "All Names")Then
For Each ws In Worksheets
Select Case ws.Name
Case "Dashboard", "Data", "Employees"
Case Else
ws.Delete
End Select
Next
'this is where it is not working
ElseIf (Err.Number = 1004 And EmpListBox.Value = "John") Then
Sheets("John").Delete
Resume Main
End If









share|improve this question














So I have a listbox with a number of names and one value which is an "all names" option. When a name is selected, a worksheet is added with information about this name. When "all names" are selected, multiple worksheets are added, one for each name.



I am trying to put in some error handling that will delete the existing name sheet if someone selects the same one twice. I was able to get the code working for the "all names" function, to where it will delete all sheets except the dashboards/background data and then re-insert the name sheets. However, I am having trouble getting it to work where one single name is selected.



Here is my error handling code. I have it go to this OnError. For the second part, I am just using one of the potential name values, John. The sheet name and listbox value would be the same.



ErrorHandling:

If (Err.Number = 1004 And EmpListBox.Value = "All Names")Then
For Each ws In Worksheets
Select Case ws.Name
Case "Dashboard", "Data", "Employees"
Case Else
ws.Delete
End Select
Next
'this is where it is not working
ElseIf (Err.Number = 1004 And EmpListBox.Value = "John") Then
Sheets("John").Delete
Resume Main
End If






excel vba excel-vba error-handling






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 3:36









tetrikertetriker

274




274













  • Use a function that returns a boolean if sheet exists and call that stackoverflow.com/q/6688131/6241235 you can then probably re-write the above and remove the current error handling.

    – QHarr
    Nov 25 '18 at 4:44





















  • Use a function that returns a boolean if sheet exists and call that stackoverflow.com/q/6688131/6241235 you can then probably re-write the above and remove the current error handling.

    – QHarr
    Nov 25 '18 at 4:44



















Use a function that returns a boolean if sheet exists and call that stackoverflow.com/q/6688131/6241235 you can then probably re-write the above and remove the current error handling.

– QHarr
Nov 25 '18 at 4:44







Use a function that returns a boolean if sheet exists and call that stackoverflow.com/q/6688131/6241235 you can then probably re-write the above and remove the current error handling.

– QHarr
Nov 25 '18 at 4:44














1 Answer
1






active

oldest

votes


















1














ErrorHandling:



If Err.Number = 1004 Then
If EmpListBox.Value = "All Names" Then
For Each ws In Worksheets
Select Case ws.Name
Case "Dashboard", "Data", "Employees"
Case Else
ws.Delete
End Select
Next
Else
Sheets(EmpListBox.Value).Delete
End If
End If
Resume Main





share|improve this answer
























  • Thanks so much, this worked perfectly! Still threw in a sheet1, etc. but with a left function could easily delete those extra sheets.

    – tetriker
    Nov 25 '18 at 18:52












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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464440%2fusing-error-handling-to-delete-sheets-based-on-listbox-value%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









1














ErrorHandling:



If Err.Number = 1004 Then
If EmpListBox.Value = "All Names" Then
For Each ws In Worksheets
Select Case ws.Name
Case "Dashboard", "Data", "Employees"
Case Else
ws.Delete
End Select
Next
Else
Sheets(EmpListBox.Value).Delete
End If
End If
Resume Main





share|improve this answer
























  • Thanks so much, this worked perfectly! Still threw in a sheet1, etc. but with a left function could easily delete those extra sheets.

    – tetriker
    Nov 25 '18 at 18:52
















1














ErrorHandling:



If Err.Number = 1004 Then
If EmpListBox.Value = "All Names" Then
For Each ws In Worksheets
Select Case ws.Name
Case "Dashboard", "Data", "Employees"
Case Else
ws.Delete
End Select
Next
Else
Sheets(EmpListBox.Value).Delete
End If
End If
Resume Main





share|improve this answer
























  • Thanks so much, this worked perfectly! Still threw in a sheet1, etc. but with a left function could easily delete those extra sheets.

    – tetriker
    Nov 25 '18 at 18:52














1












1








1







ErrorHandling:



If Err.Number = 1004 Then
If EmpListBox.Value = "All Names" Then
For Each ws In Worksheets
Select Case ws.Name
Case "Dashboard", "Data", "Employees"
Case Else
ws.Delete
End Select
Next
Else
Sheets(EmpListBox.Value).Delete
End If
End If
Resume Main





share|improve this answer













ErrorHandling:



If Err.Number = 1004 Then
If EmpListBox.Value = "All Names" Then
For Each ws In Worksheets
Select Case ws.Name
Case "Dashboard", "Data", "Employees"
Case Else
ws.Delete
End Select
Next
Else
Sheets(EmpListBox.Value).Delete
End If
End If
Resume Main






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 25 '18 at 9:21









DisplayNameDisplayName

11.3k2720




11.3k2720













  • Thanks so much, this worked perfectly! Still threw in a sheet1, etc. but with a left function could easily delete those extra sheets.

    – tetriker
    Nov 25 '18 at 18:52



















  • Thanks so much, this worked perfectly! Still threw in a sheet1, etc. but with a left function could easily delete those extra sheets.

    – tetriker
    Nov 25 '18 at 18:52

















Thanks so much, this worked perfectly! Still threw in a sheet1, etc. but with a left function could easily delete those extra sheets.

– tetriker
Nov 25 '18 at 18:52





Thanks so much, this worked perfectly! Still threw in a sheet1, etc. but with a left function could easily delete those extra sheets.

– tetriker
Nov 25 '18 at 18:52




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464440%2fusing-error-handling-to-delete-sheets-based-on-listbox-value%23new-answer', 'question_page');
}
);

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







這個網誌中的熱門文章

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Xamarin.form Move up view when keyboard appear

JBPM : POST request for execute process go wrong