VBA change cell value after saving as PDF

drefiek2

Board Regular
Joined
Apr 23, 2023
Messages
59
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hi,
I need a vba button code which when clicked will change cells S4 and S5 to value -1, regardless of the value already in these cells.
The issue I am having is that sometimes S5 is locked (which is determined by cell A1 being TRUE instead of FALSE).
What I need the button to do is first check whether cell A1 says TRUE, if it is then change only cell S4 to -1. If cell A1 says FALSE, then change both cells S4 and S5 to -1.

This new line of code is going right at the end of an existing code which ultimately saves the sheet as a PDF, so I want this new line of code to occur right at the end as a final instruction, after the PDF save.

Thanks in advance.
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
The issue I am having is that sometimes S5 is locked (which is determined by cell A1 being TRUE instead of FALSE).
Is it then protected (locking without protection doesn't really do anything)?
Assuming it is also protected, is it protected with a password?
We don't need to know the password, we just need to know if there is a password on the protection.
 
Upvote 0
The lines of code you need would look something like this:
Rich (BB code):
'   Unprotect sheet
    ActiveSheet.Unprotect Password:="your password here"
  
'   Change S4 to -1
    Range("S4").Value = -1
  
'   Check value of cell A1
    If Range("A1") = False Then
'       Change S5 to -1
        Range("S5").Value = -1
    End If
  
'   Re-protect sheet
    ActiveSheet.Protect Password:="your password here"
You would obviously replace "your password here" with whatever your password is.
If you have sheet protection WITHOUT a password, just remove the "Password:=..." part from each of the Unprotect/Protect lines.

If the values in cell A1 are the TEXT entries of "True" and "False" instead of the boolean values of TRUE and FALSE, just put double quotes around False so that line of code looks like:

Rich (BB code):
    If Range("A1") = "False" Then
 
Upvote 0
Solution
The lines of code you need would look something like this:
Rich (BB code):
'   Unprotect sheet
    ActiveSheet.Unprotect Password:="your password here"
 
'   Change S4 to -1
    Range("S4").Value = -1
 
'   Check value of cell A1
    If Range("A1") = False Then
'       Change S5 to -1
        Range("S5").Value = -1
    End If
 
'   Re-protect sheet
    ActiveSheet.Protect Password:="your password here"
You would obviously replace "your password here" with whatever your password is.
If you have sheet protection WITHOUT a password, just remove the "Password:=..." part from each of the Unprotect/Protect lines.

If the values in cell A1 are the TEXT entries of "True" and "False" instead of the boolean values of TRUE and FALSE, just put double quotes around False so that line of code looks like:

Rich (BB code):
    If Range("A1") = "False" Then

Thank you, it worked, much appreciated!!!
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,223,881
Messages
6,175,161
Members
452,615
Latest member
bogeys2birdies

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