Remove Conditional Formatting via VBA in an existing macro, and protect new workbook created by macro

louloujoy

New Member
Joined
Feb 12, 2017
Messages
3
Hi all,

I have written 3 separate macros in a protected workbook which are running successfully, however I have 2 additional functions that I'm unable to get working. I have explained below what my macro does (in a nutshell) to give context to what I need to add in.

3 existing macros I am currently using:

Macro 1 - NextQuote(): Clears existing data and changes quote number to next sequential number (also unprotects and re-protects main workbook);
Macro 2 - SaveQuoteWithNewName(): Creates new macro enabled workbook saved with a specific name (also unprotects and re-protects main workbook);
Macro 3 - SaveFinalQuoteAsPDF(): Creates PDF copy from the new macro enabled workbook, saved in same name as the workbook it was created from.

2 additional fuctions I need to perform, but can't get working:


  1. Protect the data in the new macro enabled workbook that is created by running macro #2;
  2. Remove Conditional Formatting in the new macro enabled workbook that is created by running macro #2 (but don't remove from the original workbook).

Note - the new macro enabled workbook will have a different name each time it is saved as it incorporates a quote number and and Customer Name.

Here is my VBA coding that is currently performing my 3 macros. Advice on how I could incorporate my second 2 requirements into this coding would be hugely appreciated! :)

Code:
Sub NextQuote()    Worksheets("quoting tool").Unprotect Password:="chcp"
    Range("G4").Value = Range("G4").Value + 1
    Range("servicetype1:D36,B38:E42,C7:C8,K3:K6,C11:C11").ClearContents
    Worksheets("quoting tool").Protect Password:="chcp"
End Sub


Sub SaveQuoteWithNewName()
    Worksheets("quoting tool").Unprotect Password:="chcp"
    Dim NewFN As Variant
    ' Copy Quote to a new workbook
    Sheets(Array("quoting tool", "nurses", "client contribution", "services", "level 1 and 2", "level 3 and 4", "instructions for use", "pricing list")).Copy
    Worksheets("quoting tool").Range("I:Z").EntireColumn.Hidden = True
    NewFN = "M:\Always Used\CHCP Quoting Tool\CHCP Customer Quotes\CHCP Quote " & Range("G4").Value & " - " & Range("C3").Value & ".xlsm"
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs NewFN, FileFormat:=xlOpenXMLWorkbookMacroEnabled
    Application.DisplayAlerts = True
    ActiveWorkbook.Close
    Worksheets("quoting tool").Protect Password:="chcp"
    ThisWorkbook.Save
    ThisWorkbook.Close
End Sub


Sub SaveFinalQuoteAsPDF()
    Dim NewFN As Variant
    NewFN = "M:\Always Used\CHCP Quoting Tool\CHCP Customer Quotes\CHCP Quote " & Range("G4").Value & " - " & Range("C3").Value & ".pdf"
    ' Export the invoice sheet as PDF
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=NewFN, Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, _
    OpenAfterPublish:=True
    ThisWorkbook.Save
    ThisWorkbook.Close
    ActiveWorkbook.Save
    ActiveWorkbook.Close

End Sub

Thanks in advance! :)

Lou
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hi!

To get started, in your 2nd macro, you should very clearly manipulate the workbooks.

Code:
Dim WbMain As Workbook, WbNew As Workbook

Set WbMain = ThisWorkbook

.....

Set WbNew = ActiveWorkbook

Now you can do whatever you want with WbNew before closing it such as
Code:
WbNew.Sheets(1).Range("A:Z").FormatConditions.Delete
WbNew.Sheets(1).Protect Password:="chcp"
 
Upvote 0
Thank you so much!

I was able to get the "protect" function working on my new workbook, although I'm seeing errors with the "delete conditional formatting" function. I suspect that this is because I'm trying to insert the function into the third macro, as it works perfectly when I insert in into the 2nd macro. However, including that line in the 2nd macro is removing the conditional formatting too early; I need the conditional formatting to remain until the document is 100% completed and ready to be saved as a PDF, only then should the conditional formatting be deleted. I hope this makes sense!

Any tips on where/how I should write this in? Updated coding below for my 3rd macro:

Rich (BB code):
Rich (BB code):
Sub SaveFinalQuoteAsPDF()    Dim WbMain As Workbook, WbNew As Workbook
    Set WbMain = ThisWorkbook
    Set WbNew = ActiveWorkbook
    Dim NewFN As Variant
    NewFN = "M:\Always Used\CHCP Quoting Tool\CHCP Customer Quotes\CHCP Quote " & Range("G4").Value & " - " & Range("C3").Value & ".pdf"
    ' Export the invoice sheet as PDF
    WbNew.ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=NewFN, Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, _
    OpenAfterPublish:=True
    WbMain.Save
    WbMain.Close
    WbNew.Save
    WbNew.Close
End Sub


Thanks again! I appreciate the help!
 
Upvote 0
Hi,

I understand that the 1st and 2nd macros are launched from the original file right? What about the 3rd one? If you correctly launch the 3rd macro from the new workbook, and write something like
Code:
ThisWorkbook.Sheets(1).Range("A:Z").FormatConditions.Delete

it should work.

What errors do you see?
 
Upvote 0
Hi again!

Correct, I'm attempting to launch the third macro from the new workbook so that all formatting in the original workbook remains unchanged for future use.

I've tried your suggestion in different positions throughout the coding, and with amendments such as changing the "WbNew" name instead of "ThisWorkbook" but each time get the same error message that just says "400". I've basically tried every variation on this piece of code that I can find/think of for the past few days, always with the same "400" error message.

Here was my last attempt:

Rich (BB code):
Rich (BB code):
Sub SaveFinalQuoteAsPDF()    Dim WbMain As Workbook, WbNew As Workbook
    Set WbMain = ThisWorkbook
    Set WbNew = ActiveWorkbook
    WbNew.Sheets(1).Range("A:Z").FormatConditions.Delete
    Dim NewFN As Variant
    NewFN = "M:\Always Used\CHCP Quoting Tool\CHCP Customer Quotes\CHCP Quote " & Range("G4").Value & " - " & Range("C3").Value & ".pdf"
    ' Export the invoice sheet as PDF
    WbNew.ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=NewFN, Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, _
    OpenAfterPublish:=True
    WbMain.Save
    WbMain.Close
    WbNew.Save
    WbNew.Close

End Sub


Thanks again :biggrin:
 
Upvote 0
Of course, this is because your sheet is protected!!

Try to : unprotect, delete de conditions formats, protect again. And do it after you saved the file as PDF.
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,208
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top