How do I delete a Worksheet with VBA that has a date format?
enter code hereA worksheet will automatically be created. The worksheet will have a date that is entered 2 days from the current date. I am having trouble getting it to delete that worksheet when it becomes 3 days old. Please see my code below for what I currently have.
For Each ws In Worksheets
If ws.Name = Format(Date - 2, "MM-DD-YYYY") Then
Application.DisplayAlerts = False
Sheets(Date - 2, "MM-DD-YYYY").Delete
Application.DisplayAlerts = True
excel vba excel-vba
add a comment |
enter code hereA worksheet will automatically be created. The worksheet will have a date that is entered 2 days from the current date. I am having trouble getting it to delete that worksheet when it becomes 3 days old. Please see my code below for what I currently have.
For Each ws In Worksheets
If ws.Name = Format(Date - 2, "MM-DD-YYYY") Then
Application.DisplayAlerts = False
Sheets(Date - 2, "MM-DD-YYYY").Delete
Application.DisplayAlerts = True
excel vba excel-vba
Do you want to delete any worksheet that is three days old or older? Something to cover weekends and holidays?
– user10852207
Jan 1 at 23:23
add a comment |
enter code hereA worksheet will automatically be created. The worksheet will have a date that is entered 2 days from the current date. I am having trouble getting it to delete that worksheet when it becomes 3 days old. Please see my code below for what I currently have.
For Each ws In Worksheets
If ws.Name = Format(Date - 2, "MM-DD-YYYY") Then
Application.DisplayAlerts = False
Sheets(Date - 2, "MM-DD-YYYY").Delete
Application.DisplayAlerts = True
excel vba excel-vba
enter code hereA worksheet will automatically be created. The worksheet will have a date that is entered 2 days from the current date. I am having trouble getting it to delete that worksheet when it becomes 3 days old. Please see my code below for what I currently have.
For Each ws In Worksheets
If ws.Name = Format(Date - 2, "MM-DD-YYYY") Then
Application.DisplayAlerts = False
Sheets(Date - 2, "MM-DD-YYYY").Delete
Application.DisplayAlerts = True
excel vba excel-vba
excel vba excel-vba
asked Jan 1 at 22:52
Kevin SinghKevin Singh
1
1
Do you want to delete any worksheet that is three days old or older? Something to cover weekends and holidays?
– user10852207
Jan 1 at 23:23
add a comment |
Do you want to delete any worksheet that is three days old or older? Something to cover weekends and holidays?
– user10852207
Jan 1 at 23:23
Do you want to delete any worksheet that is three days old or older? Something to cover weekends and holidays?
– user10852207
Jan 1 at 23:23
Do you want to delete any worksheet that is three days old or older? Something to cover weekends and holidays?
– user10852207
Jan 1 at 23:23
add a comment |
3 Answers
3
active
oldest
votes
If i understood your question you can try this code:
Sub test()
For Each ws In Worksheets
If ws.Name = Format(Date - 2, "MM-DD-YYYY") Then
Application.DisplayAlerts = False
'control the date and delete the sheet
If (ws.Name = Format(Date - 2, "MM-DD-YYYY")) Then
ws.Delete
Application.DisplayAlerts = True
End If
End If
Next ws
End Sub
ex. if today is 01-02-2019 the macro delete the sheet with the date 12-31-2018 (MM-DD-YYYY). If you want delete the sheet 3 days old then you have to change value 2 in 3(within macro).
Hope this helps!
add a comment |
You aren't returning a formatted string into the Sheets collection.
For Each ws In Worksheets
If ws.Name = Format(Date - 2, "MM-DD-YYYY") Then
Application.DisplayAlerts = False
Sheets(Format(Date - 2, "MM-DD-YYYY")).Delete '<~~ identified here
'alternate
'ws.delete
Application.DisplayAlerts = True
exit for '<~~ no point in continuing since only one worksheet could have that name
There is actually no point in building the worksheet name twice. If it passes the first name test then ws is the worksheet you want to delete.
I started thinking about weekends and holidays that might interfere with the '3 days old' rule. This will delete any worksheet that has a name as a string date three working days old or older.
For Each ws In Worksheets
If cdate(ws.Name) <= APPLICATION.WORKDAY(Date, -3) Then
Application.DisplayAlerts = False
ws.delete
Application.DisplayAlerts = True
end if
NEXT WS
It's been there all along but commented out as an alternate method so the OP didn't try to delete the same worksheet twice.
– user10852207
Jan 1 at 23:19
add a comment |
Delete Old Sheets
- This code includes the deletion of possible chart sheets.
- To enable deletion without confirmation you have to set
cBlnWithoutConfirmationtoTrue.
The Code
'*******************************************************************************
' Purpose: Deletes all sheets named by a date in format MM-DD-YYYY
' that are at least old as specified.
'*******************************************************************************
Sub DeleteOldSheets()
Const cIntAge As Integer = 3 ' Age of Sheet (Days)
Const cBlnWithoutConfirmation = False ' Enable Delete Without Confirmation
Dim vntDate As Variant ' Date Array
Dim vntSheetDate As Date ' Sheet Date
Dim i As Integer ' Sheets Counter
With ThisWorkbook
For i = 1 To .Sheets.Count
With .Sheets(i)
vntDate = Split(.Name, "-")
On Error Resume Next
vntSheetDate = DateSerial(vntDate(2), vntDate(0), vntDate(1))
If Err Then
On Error GoTo 0
Else
If Date - vntSheetDate + 1 > cIntAge Then
If cBlnWithoutConfirmation Then
Application.DisplayAlerts = False
.Delete
Application.DisplayAlerts = True
Else
.Delete
End If
End If
End If
End With
Next
End With
End Sub
'*******************************************************************************
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%2f53999563%2fhow-do-i-delete-a-worksheet-with-vba-that-has-a-date-format%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
If i understood your question you can try this code:
Sub test()
For Each ws In Worksheets
If ws.Name = Format(Date - 2, "MM-DD-YYYY") Then
Application.DisplayAlerts = False
'control the date and delete the sheet
If (ws.Name = Format(Date - 2, "MM-DD-YYYY")) Then
ws.Delete
Application.DisplayAlerts = True
End If
End If
Next ws
End Sub
ex. if today is 01-02-2019 the macro delete the sheet with the date 12-31-2018 (MM-DD-YYYY). If you want delete the sheet 3 days old then you have to change value 2 in 3(within macro).
Hope this helps!
add a comment |
If i understood your question you can try this code:
Sub test()
For Each ws In Worksheets
If ws.Name = Format(Date - 2, "MM-DD-YYYY") Then
Application.DisplayAlerts = False
'control the date and delete the sheet
If (ws.Name = Format(Date - 2, "MM-DD-YYYY")) Then
ws.Delete
Application.DisplayAlerts = True
End If
End If
Next ws
End Sub
ex. if today is 01-02-2019 the macro delete the sheet with the date 12-31-2018 (MM-DD-YYYY). If you want delete the sheet 3 days old then you have to change value 2 in 3(within macro).
Hope this helps!
add a comment |
If i understood your question you can try this code:
Sub test()
For Each ws In Worksheets
If ws.Name = Format(Date - 2, "MM-DD-YYYY") Then
Application.DisplayAlerts = False
'control the date and delete the sheet
If (ws.Name = Format(Date - 2, "MM-DD-YYYY")) Then
ws.Delete
Application.DisplayAlerts = True
End If
End If
Next ws
End Sub
ex. if today is 01-02-2019 the macro delete the sheet with the date 12-31-2018 (MM-DD-YYYY). If you want delete the sheet 3 days old then you have to change value 2 in 3(within macro).
Hope this helps!
If i understood your question you can try this code:
Sub test()
For Each ws In Worksheets
If ws.Name = Format(Date - 2, "MM-DD-YYYY") Then
Application.DisplayAlerts = False
'control the date and delete the sheet
If (ws.Name = Format(Date - 2, "MM-DD-YYYY")) Then
ws.Delete
Application.DisplayAlerts = True
End If
End If
Next ws
End Sub
ex. if today is 01-02-2019 the macro delete the sheet with the date 12-31-2018 (MM-DD-YYYY). If you want delete the sheet 3 days old then you have to change value 2 in 3(within macro).
Hope this helps!
answered Jan 1 at 23:16
FerdinandoFerdinando
5891417
5891417
add a comment |
add a comment |
You aren't returning a formatted string into the Sheets collection.
For Each ws In Worksheets
If ws.Name = Format(Date - 2, "MM-DD-YYYY") Then
Application.DisplayAlerts = False
Sheets(Format(Date - 2, "MM-DD-YYYY")).Delete '<~~ identified here
'alternate
'ws.delete
Application.DisplayAlerts = True
exit for '<~~ no point in continuing since only one worksheet could have that name
There is actually no point in building the worksheet name twice. If it passes the first name test then ws is the worksheet you want to delete.
I started thinking about weekends and holidays that might interfere with the '3 days old' rule. This will delete any worksheet that has a name as a string date three working days old or older.
For Each ws In Worksheets
If cdate(ws.Name) <= APPLICATION.WORKDAY(Date, -3) Then
Application.DisplayAlerts = False
ws.delete
Application.DisplayAlerts = True
end if
NEXT WS
It's been there all along but commented out as an alternate method so the OP didn't try to delete the same worksheet twice.
– user10852207
Jan 1 at 23:19
add a comment |
You aren't returning a formatted string into the Sheets collection.
For Each ws In Worksheets
If ws.Name = Format(Date - 2, "MM-DD-YYYY") Then
Application.DisplayAlerts = False
Sheets(Format(Date - 2, "MM-DD-YYYY")).Delete '<~~ identified here
'alternate
'ws.delete
Application.DisplayAlerts = True
exit for '<~~ no point in continuing since only one worksheet could have that name
There is actually no point in building the worksheet name twice. If it passes the first name test then ws is the worksheet you want to delete.
I started thinking about weekends and holidays that might interfere with the '3 days old' rule. This will delete any worksheet that has a name as a string date three working days old or older.
For Each ws In Worksheets
If cdate(ws.Name) <= APPLICATION.WORKDAY(Date, -3) Then
Application.DisplayAlerts = False
ws.delete
Application.DisplayAlerts = True
end if
NEXT WS
It's been there all along but commented out as an alternate method so the OP didn't try to delete the same worksheet twice.
– user10852207
Jan 1 at 23:19
add a comment |
You aren't returning a formatted string into the Sheets collection.
For Each ws In Worksheets
If ws.Name = Format(Date - 2, "MM-DD-YYYY") Then
Application.DisplayAlerts = False
Sheets(Format(Date - 2, "MM-DD-YYYY")).Delete '<~~ identified here
'alternate
'ws.delete
Application.DisplayAlerts = True
exit for '<~~ no point in continuing since only one worksheet could have that name
There is actually no point in building the worksheet name twice. If it passes the first name test then ws is the worksheet you want to delete.
I started thinking about weekends and holidays that might interfere with the '3 days old' rule. This will delete any worksheet that has a name as a string date three working days old or older.
For Each ws In Worksheets
If cdate(ws.Name) <= APPLICATION.WORKDAY(Date, -3) Then
Application.DisplayAlerts = False
ws.delete
Application.DisplayAlerts = True
end if
NEXT WS
You aren't returning a formatted string into the Sheets collection.
For Each ws In Worksheets
If ws.Name = Format(Date - 2, "MM-DD-YYYY") Then
Application.DisplayAlerts = False
Sheets(Format(Date - 2, "MM-DD-YYYY")).Delete '<~~ identified here
'alternate
'ws.delete
Application.DisplayAlerts = True
exit for '<~~ no point in continuing since only one worksheet could have that name
There is actually no point in building the worksheet name twice. If it passes the first name test then ws is the worksheet you want to delete.
I started thinking about weekends and holidays that might interfere with the '3 days old' rule. This will delete any worksheet that has a name as a string date three working days old or older.
For Each ws In Worksheets
If cdate(ws.Name) <= APPLICATION.WORKDAY(Date, -3) Then
Application.DisplayAlerts = False
ws.delete
Application.DisplayAlerts = True
end if
NEXT WS
edited Jan 1 at 23:28
answered Jan 1 at 23:08
user10852207user10852207
2263
2263
It's been there all along but commented out as an alternate method so the OP didn't try to delete the same worksheet twice.
– user10852207
Jan 1 at 23:19
add a comment |
It's been there all along but commented out as an alternate method so the OP didn't try to delete the same worksheet twice.
– user10852207
Jan 1 at 23:19
It's been there all along but commented out as an alternate method so the OP didn't try to delete the same worksheet twice.
– user10852207
Jan 1 at 23:19
It's been there all along but commented out as an alternate method so the OP didn't try to delete the same worksheet twice.
– user10852207
Jan 1 at 23:19
add a comment |
Delete Old Sheets
- This code includes the deletion of possible chart sheets.
- To enable deletion without confirmation you have to set
cBlnWithoutConfirmationtoTrue.
The Code
'*******************************************************************************
' Purpose: Deletes all sheets named by a date in format MM-DD-YYYY
' that are at least old as specified.
'*******************************************************************************
Sub DeleteOldSheets()
Const cIntAge As Integer = 3 ' Age of Sheet (Days)
Const cBlnWithoutConfirmation = False ' Enable Delete Without Confirmation
Dim vntDate As Variant ' Date Array
Dim vntSheetDate As Date ' Sheet Date
Dim i As Integer ' Sheets Counter
With ThisWorkbook
For i = 1 To .Sheets.Count
With .Sheets(i)
vntDate = Split(.Name, "-")
On Error Resume Next
vntSheetDate = DateSerial(vntDate(2), vntDate(0), vntDate(1))
If Err Then
On Error GoTo 0
Else
If Date - vntSheetDate + 1 > cIntAge Then
If cBlnWithoutConfirmation Then
Application.DisplayAlerts = False
.Delete
Application.DisplayAlerts = True
Else
.Delete
End If
End If
End If
End With
Next
End With
End Sub
'*******************************************************************************
add a comment |
Delete Old Sheets
- This code includes the deletion of possible chart sheets.
- To enable deletion without confirmation you have to set
cBlnWithoutConfirmationtoTrue.
The Code
'*******************************************************************************
' Purpose: Deletes all sheets named by a date in format MM-DD-YYYY
' that are at least old as specified.
'*******************************************************************************
Sub DeleteOldSheets()
Const cIntAge As Integer = 3 ' Age of Sheet (Days)
Const cBlnWithoutConfirmation = False ' Enable Delete Without Confirmation
Dim vntDate As Variant ' Date Array
Dim vntSheetDate As Date ' Sheet Date
Dim i As Integer ' Sheets Counter
With ThisWorkbook
For i = 1 To .Sheets.Count
With .Sheets(i)
vntDate = Split(.Name, "-")
On Error Resume Next
vntSheetDate = DateSerial(vntDate(2), vntDate(0), vntDate(1))
If Err Then
On Error GoTo 0
Else
If Date - vntSheetDate + 1 > cIntAge Then
If cBlnWithoutConfirmation Then
Application.DisplayAlerts = False
.Delete
Application.DisplayAlerts = True
Else
.Delete
End If
End If
End If
End With
Next
End With
End Sub
'*******************************************************************************
add a comment |
Delete Old Sheets
- This code includes the deletion of possible chart sheets.
- To enable deletion without confirmation you have to set
cBlnWithoutConfirmationtoTrue.
The Code
'*******************************************************************************
' Purpose: Deletes all sheets named by a date in format MM-DD-YYYY
' that are at least old as specified.
'*******************************************************************************
Sub DeleteOldSheets()
Const cIntAge As Integer = 3 ' Age of Sheet (Days)
Const cBlnWithoutConfirmation = False ' Enable Delete Without Confirmation
Dim vntDate As Variant ' Date Array
Dim vntSheetDate As Date ' Sheet Date
Dim i As Integer ' Sheets Counter
With ThisWorkbook
For i = 1 To .Sheets.Count
With .Sheets(i)
vntDate = Split(.Name, "-")
On Error Resume Next
vntSheetDate = DateSerial(vntDate(2), vntDate(0), vntDate(1))
If Err Then
On Error GoTo 0
Else
If Date - vntSheetDate + 1 > cIntAge Then
If cBlnWithoutConfirmation Then
Application.DisplayAlerts = False
.Delete
Application.DisplayAlerts = True
Else
.Delete
End If
End If
End If
End With
Next
End With
End Sub
'*******************************************************************************
Delete Old Sheets
- This code includes the deletion of possible chart sheets.
- To enable deletion without confirmation you have to set
cBlnWithoutConfirmationtoTrue.
The Code
'*******************************************************************************
' Purpose: Deletes all sheets named by a date in format MM-DD-YYYY
' that are at least old as specified.
'*******************************************************************************
Sub DeleteOldSheets()
Const cIntAge As Integer = 3 ' Age of Sheet (Days)
Const cBlnWithoutConfirmation = False ' Enable Delete Without Confirmation
Dim vntDate As Variant ' Date Array
Dim vntSheetDate As Date ' Sheet Date
Dim i As Integer ' Sheets Counter
With ThisWorkbook
For i = 1 To .Sheets.Count
With .Sheets(i)
vntDate = Split(.Name, "-")
On Error Resume Next
vntSheetDate = DateSerial(vntDate(2), vntDate(0), vntDate(1))
If Err Then
On Error GoTo 0
Else
If Date - vntSheetDate + 1 > cIntAge Then
If cBlnWithoutConfirmation Then
Application.DisplayAlerts = False
.Delete
Application.DisplayAlerts = True
Else
.Delete
End If
End If
End If
End With
Next
End With
End Sub
'*******************************************************************************
answered Jan 2 at 0:41
VBasic2008VBasic2008
3,1292416
3,1292416
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.
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%2f53999563%2fhow-do-i-delete-a-worksheet-with-vba-that-has-a-date-format%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
Do you want to delete any worksheet that is three days old or older? Something to cover weekends and holidays?
– user10852207
Jan 1 at 23:23