Cyclops755
New Member
- Joined
- Jul 26, 2011
- Messages
- 31
I'm very new to VBA, and have only used it lightly so far, but I'm trying to use it more, and have encountered a problem when trying to have two different "strings" of code on one sheet. My code currently is:
Private Sub Worksheet_Change(ByVal Target As Range)
' Limit to only work on columns 9 or 10
If Target.Column = 9 Or Target.Column = 10 Then
Application.EnableEvents = False
If Target.Column = 9 Then Target.Offset(0, 1).Value = 1 - Target.Value
If Target.Column = 10 Then Target.Offset(0, -1).Value = 1 - Target.Value
Application.EnableEvents = True
End If
End Sub
Code for Worksheet "Cells as Checkboxes"
Option Explicit
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'Limit Target count to 1
If Target.Count > 1 Then Exit Sub
'Isolate Target to a specific range
If Intersect(Target, Range("checkboxes")) Is Nothing Then Exit Sub
'set Target font tp "marlett"
Target.Font.Name = "marlett"
'Check value of target
If Target.Value <> "a" Then
Target.Value = "a" 'Sets target Value = "a"
Cancel = True
Exit Sub
End If
If Target.Value = "a" Then
Target.ClearContents 'Sets Target Value = ""
Cancel = True
Exit Sub
End If
End Sub
The first part ensures that the percentages in a pair of columns always adds up to 100%, the other allows you to check boxes by double-clicking them. I've heard that each might need to be put in a module? but I haven't been able to make that work. Can someone help clear this up?
Private Sub Worksheet_Change(ByVal Target As Range)
' Limit to only work on columns 9 or 10
If Target.Column = 9 Or Target.Column = 10 Then
Application.EnableEvents = False
If Target.Column = 9 Then Target.Offset(0, 1).Value = 1 - Target.Value
If Target.Column = 10 Then Target.Offset(0, -1).Value = 1 - Target.Value
Application.EnableEvents = True
End If
End Sub
Code for Worksheet "Cells as Checkboxes"
Option Explicit
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'Limit Target count to 1
If Target.Count > 1 Then Exit Sub
'Isolate Target to a specific range
If Intersect(Target, Range("checkboxes")) Is Nothing Then Exit Sub
'set Target font tp "marlett"
Target.Font.Name = "marlett"
'Check value of target
If Target.Value <> "a" Then
Target.Value = "a" 'Sets target Value = "a"
Cancel = True
Exit Sub
End If
If Target.Value = "a" Then
Target.ClearContents 'Sets Target Value = ""
Cancel = True
Exit Sub
End If
End Sub
The first part ensures that the percentages in a pair of columns always adds up to 100%, the other allows you to check boxes by double-clicking them. I've heard that each might need to be put in a module? but I haven't been able to make that work. Can someone help clear this up?