VBA newbie question - Multiple strings on one sheet?

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?
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Assuming that code all relates to a single worksheet, all of that code from the first Private to the last End Sub needs to go in the code module for that worksheet. They are 'event handlers' for a worksheet and they will not work anywhere else. In VBA, go Ctrl-R to view the Project Explorer, then double-click the name of the worksheet you want those bits of code to work with. The code should be in that code module.

Where did you find them? Are they working at the moment? If they are both working okay, leave them where they are - if you move them you will stop them from working.

(The directive Option Explicit goes at the very top of the module.)
 
Upvote 0
Thanks for clarifying Ruddles, yes its all from the same worksheet. I found the code from other threads started to solve similar problems, and pasted them in. Both worked flawlessly alone, but together not so much. I believe I put them in the place you are describing, as I opened the page associated with the specific worksheet. I was confused between placing each one directly in the worksheet, or having to create "modules" at the bottom of the project explorer and somehow call the modules in on the worksheet page. Me and a friend seemed to have found a way to make them both coexist, and they're functioning normally for now. Here's what we have:

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



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


Not sure if thats the best/"correct" way to do it, but it works :biggrin:. If you have any tips or advice on a better way to do this, I'd love to know. Thanks again!
 
Upvote 0
Welcome to the Board!

Worksheet level code, like events, goes in the worksheet specific module, regardless of how many events there are. The problems begin when you try to have two of the same event instead of consolidating them.

So what you did above is correct.

HTH,
 
Upvote 0

Forum statistics

Threads
1,224,594
Messages
6,179,795
Members
452,943
Latest member
Newbie4296

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