VBA Formula Trouble

boogagang

New Member
Joined
Dec 22, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I have a cell (B18) for users to input a number between 0 and 5
I have an adjacent cell (C18) which is a check box.
The combination of these two cells outputs a number to a different cell. I'm trying to write a code in VBA which automatically checks the check box in C18, every time the value of the B18 is 5.
I have managed to accomplish this with:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("B18").Address Then
Range("C18").Formula = "=IF(B18=5,TRUE,FALSE)"
End If
End Sub

This seemed to work at first. Every time I would change cell B18 to 5, then C18 would be "TRUE". Every time I changed B18 to 4, C18 would read "FASLE". The problem comes when I manually check the box. There are situations where B18 can be less than 5 and the checkbox in C18 still needs to be checked. Once I do this however, the formula no longer works and C18 wont read TRUE is B18 is set to 5.
I hope I explained this clearly. I would appreciate any help.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
So why do you need the checkBox
Why not just let the script do all the work without the CheckBox
What happens when the checkbox is checked?
 
Upvote 0
you could try this which allows you to overwrite C18:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("B18").Address Then
Range("C18") = Target.Value = 5
End If
End Sub
 
Upvote 0
Solution
It is a work application where the checkbox indicates a scenario. The scenario could be true even if the number is 3 or 4, so the user needs the autonomy to check the box themselves, but if the number is 5, the scenario is always true. This makes checking the box after inputting the 5 feel redundant.

The VBA code you posted works! Thanks so much. I have been searching google and youtube for a couple days trying to learn how to do this.
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,243
Members
449,075
Latest member
staticfluids

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