I've got a workbook that I use to create other workbooks, it basically saves as a new workbook and saves it to a directory and with a file name based on information entered into the workbook. It also deletes two buttons I put in that have other macros linked to in order to prevent them being accidentally run again these being Rounded Rectangle 1 and Rounded Rectangle 2
This was my code
Sub SaveTrackingSheetWithNewName()
Dim fileFolder As String
Dim NewFN As Variant
Dim sJobNum As String
Dim sClient As String
Dim sTitle As String
Dim sTitleFolder As String
Dim sPath As String
fileFolder = "G:\Commercial work\"
ActiveWorkbook.Save
PostToCommercialTracking
PostToCommercialWorkLog
' Copy Tracking Sheet to a new workbook
sJobNum = Range("D2").Value & " "
sClient = Range("B1").Value & ""
sTitle = Range("D1").Value & ".xlsm"
sTitleFolder = Range("D1").Value & ""
sPath = fileFolder & sClient & Application.PathSeparator & sJobNum & sTitleFolder
If Dir(fileFolder & sClient, vbDirectory) = "" Then
MkDir fileFolder & sClient
Else
MkDir fileFolder & sClient & Application.PathSeparator & sJobNum & sTitleFolder
End If
ActiveSheet.Unprotect "sadie"
ActiveWorkbook.SaveAs Filename:=sPath & Application.PathSeparator & sJobNum & sTitle, FileFormat:=52
ActiveSheet.Shapes("Rounded Rectangle 1").Delete
ActiveSheet.Shapes("Rounded Rectangle 2").Delete
ActiveSheet.Protect "sadie"
ActiveWorkbook.Save
ActiveWorkbook.Close
End Sub
This worked fine but I needed to change two things.
1. I didn't want the workbook that generates the other workbooks to be saved. Instead it should be closed without saving (this is because I have another macro that runs whenever the workbook is saved).
2. If a cell, B1, in the generator workbook was blank, I didn't want it to create the new workbook.
So I changed the code to this:
Sub SaveTrackingSheetWithNewName()
Dim WS1 As Worksheet
Set WS1 = Worksheets("Tracking Sheet")
If IsEmpty(WS1.Range("B1").Value) Then Exit Sub
Application.EnableEvents = False
Dim fileFolder As String
Dim NewFN As Variant
Dim sJobNum As String
Dim sClient As String
Dim sTitle As String
Dim sTitleFolder As String
Dim sPath As String
fileFolder = "G:\Commercial work\"
PostToCommercialTracking
PostToCommercialWorkLog
' Copy Tracking Sheet to a new workbook
sJobNum = Range("D2").Value & " "
sClient = Range("B1").Value & ""
sTitle = Range("D1").Value & ".xlsm"
sTitleFolder = Range("D1").Value & ""
sPath = fileFolder & sClient & Application.PathSeparator & sJobNum & sTitleFolder
If Dir(fileFolder & sClient, vbDirectory) = "" Then
MkDir fileFolder & sClient
Else
MkDir fileFolder & sClient & Application.PathSeparator & sJobNum & sTitleFolder
End If
ActiveSheet.Unprotect "sadie"
ActiveWorkbook.SaveAs Filename:=sPath & Application.PathSeparator & sJobNum & sTitle, FileFormat:=52
ActiveSheet.Shapes("Rounded Rectangle 1").Delete
ActiveSheet.Shapes("Rounded Rectangle 2").Delete
ActiveSheet.Protect "sadie"
ActiveWorkbook.Saved = True
ActiveWorkbook.Close
Application.EnableEvents = True
End Sub
What happens now is the generator workbook isn't saved, which is correct, and if B1 is empty it won't save it as a new workbook which is also correct. However when B1 has data in it, it does create the new workbook but the buttons, rounded rectangle 1 & 2 are not deleted.
I can't work out what I've done that means that everything works as it should but the buttons aren't deleted.
Bookmarks