Vincent88
Active Member
- Joined
- Mar 5, 2021
- Messages
- 382
- Office Version
- 2019
- Platform
- Windows
- Mobile
Hi, I got help from a experienced member here to rectify my code and it resolves my issue. However my another function seems having conflict with the suggested code. Can someone help me to correct it !
My file is attached Excel file
When I run module 1, I debugged to get NEXT C in module 4 keep looping and it could not proceed further.
My file is attached Excel file
When I run module 1, I debugged to get NEXT C in module 4 keep looping and it could not proceed further.
VBA Code:
'Module 1
Sub CheckSheet()
Application.ScreenUpdating = False
Dim szToday As String
szToday = Format(Date, "d mmm yyyy")
If Not Evaluate("isref('" & szToday & "'!A1)") Then
Call BlankSheet
Call Module2.RemoveOldSheets
Else
'MsgBox "Sheet " & szToday & " exists."
Dim AckTime As Integer, InfoBox As Object
Set InfoBox = CreateObject("WScript.Shell")
AckTime = 1
Select Case InfoBox.Popup("Sheet " & szToday & " exists.", AckTime, "Notification", 0)
Case 1, -1
Exit Sub
End Select
End If
Application.ScreenUpdating = True
End Sub
'https://www.mrexcel.com/board/threads/copy-table-headers-from-current-sheet-to-newly-created-sheet-in-excel-vba.1169715/
Sub BlankSheet()
Dim ws As Worksheet
Dim LastColumn As Long
Dim strSheetName As String
If ActiveWorkbook Is ThisWorkbook Then
Set ws = ActiveSheet
LastColumn = ws.Range("A1").CurrentRegion.Columns.Count
On Error Resume Next
'ADD
Application.DisplayAlerts = False
ws.Copy before:=ActiveSheet
strSheetName = Format(Date, "d mmm yyyy")
ActiveSheet.Name = strSheetName
'MODIFIED
'On Error GoTo 0
Application.DisplayAlerts = True
'Clear All Contents
'Application.EnableEvents = False
With ActiveSheet
.Cells.ClearContents
With .OLEObjects
.Visible = True
.Delete
End With
With .Pictures
.Visible = True
.Delete
End With
.Range("A1").Resize(1, LastColumn).Value = ws.Range("A1").Resize(1, LastColumn).Value
.ListObjects.Add(xlSrcRange, .Range("A1").Resize(2, LastColumn), , xlYes).Name = strSheetName
.ListObjects(strSheetName).TableStyle = "TableStyleMedium2"
.ListObjects(strSheetName).ShowAutoFilterDropDown = False
End With
'Application.EnableEvents = True
Set ws = Nothing
g_blnWbkShtSelChange = False
End If
End Sub
'Module 2
Sub RemoveOldSheets()
Dim Sh As Worksheet
For Each Sh In Worksheets
If Len(Sh.Name) >= 10 Then
If Date - CDate(Left(Sh.Name, 11)) >= 60 Then
Application.DisplayAlerts = False
Sh.Delete
'MsgBox "Old Sheets deleted"
Application.DisplayAlerts = True
End If
End If
Next Sh
End Sub
'Module 4
Public Function IsPartOfListObject(ByVal argRange As Range) As Boolean
Dim c As Range
For Each c In argRange
If Not c.ListObject Is Nothing Then
IsPartOfListObject = True
Exit For
End If
Next c
End Function
Public Function IsCurrentSheet(ByVal argSht As Worksheet, ByVal argSheetNames As String) As Boolean
IsCurrentSheet = (CBool(InStr(LCase(argSheetNames), LCase("/" & argSht.Name & "/"))))
End Function