VBA Click Button enters alternating True False text in cell

drefiek2

Board Regular
Joined
Apr 23, 2023
Messages
59
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hi,
I would like a macro button which when clicked will enter "True" in a cell in a different sheet. If you press it again it will enter "False" in that cell, and will just alternate between "True" and "False" when clicked. I found the below but it does not work properly, any ideas would be greatly appreciated.

VBA Code:
Sub Button24_Click()
If Worksheets("Mechanics").Cell("B16").Value = "True" Then
Replace Worksheets("Mechanics").Cell("B16").Value, "True", "False"
End If
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi try

VBA Code:
Sub Button24_Click()
    Dim rng As Range
    Set rng = ThisWorkbook.Worksheets("Mechanics").Range("B16")
    rng.Value = IIf(rng.Value = False, True, False)
End Sub

providing the range already has a boolean value entered you can just do this

VBA Code:
rng.Value = Not rng.Value

Dave
 
Last edited:
Upvote 0
Solution
most welcome & appreciate your feedback

Dave
No problem. You may be able to help a little more on this actually.
I have another macro button that when pressed, it saves the sheet as a PDF. I would like the above TRUE/FALSE referenced cell to tie in with my PDFsave macro button, if possible.
I would like it so that when the PDFsave section of code runs, it will ONLY run when the referenced cell above says 'TRUE'. This means that the code needs to temporarily change the 'FALSE' to 'TRUE'.
For example if the above referenced cell says FALSE, I want the PDFsave code to change it to TRUE, run the PDFsave code then change it back to FALSE, all in one click of a button.
If the above referenced cell says TRUE already, the PDFsave code should just run as normal without changing anything. I hope this makes sense.
I think this will need some IF's and Else's but I'm not 100% sure on VBA coding, it's a bit of a headache to me.
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

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