I'm having issues with the following code:
It does create the worksheets, however, it looks like it's trying to create two copies of the sheets. In the Finance workbook, I need it to rename the template worksheet after the template has been copied. This works correctly, but when I check my Finance workbook, I have sheet names:
04-Feb-2018 - 17-Feb-2018 <<== Created since it did not exist
Template (2) <<== Looks like it's trying to create it again
18-Feb-2018 - 03-Mar-2018 <<== Created since it did not exist
Template (3) <<== Again, looks like its trying to create it again
The command I pass to this function is:
The pSheet and cSheet is defined with Dates, I'm not worried about the Cancelled workbook and sheet, as this looks like it's functioning normally, I do not see any Template(#) sheets. Just having issues with the Finance workbook and it creating Template(#) sheets. I'm not sure where the code is doing the duplicate Template copy.
Code:
'================================
' Create Worksheet if not exists
'================================
Function CreateSheetIf(strSheetName As String) As Boolean
Dim blnRetVal As Boolean
blnRetVal = False
Dim wks As Worksheet
Dim wbkTarget As Workbook
If strSheetName = "Cancelled" Then
Set wbkTarget = Application.Workbooks("Cancelled.xlsm")
Else
Set wbkTarget = Application.Workbooks("Finance.xlsm")
End If
For Each wks In wbkTarget.Worksheets
If StrComp(wks.Name, strSheetName) = 0 Then
blnRetVal = True
Exit For
Else
ThisWorkbook.Sheets("Template").Visible = True
ThisWorkbook.Sheets("Template").Copy after:=wbkTarget.Sheets(Sheets.Count)
ActiveSheet.Name = strSheetName
ThisWorkbook.Sheets("Template").Visible = False
End If
Next wks
CreateSheetIf = blnRetVal
End Function
It does create the worksheets, however, it looks like it's trying to create two copies of the sheets. In the Finance workbook, I need it to rename the template worksheet after the template has been copied. This works correctly, but when I check my Finance workbook, I have sheet names:
04-Feb-2018 - 17-Feb-2018 <<== Created since it did not exist
Template (2) <<== Looks like it's trying to create it again
18-Feb-2018 - 03-Mar-2018 <<== Created since it did not exist
Template (3) <<== Again, looks like its trying to create it again
The command I pass to this function is:
Code:
CreateSheetIf (pSheet) ' Create Previous finance period
CreateSheetIf (cSheet) ' Create Current finance period
The pSheet and cSheet is defined with Dates, I'm not worried about the Cancelled workbook and sheet, as this looks like it's functioning normally, I do not see any Template(#) sheets. Just having issues with the Finance workbook and it creating Template(#) sheets. I'm not sure where the code is doing the duplicate Template copy.