Need help with code

master_of_none

Board Regular
Joined
Jan 29, 2003
Messages
62
I need help putting another code in the same sub.

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

Set rng = Range("O2:O501")

If Intersect(Target, rng) Is Nothing Then Exit Sub

If Target.Value <> "X" Then
With Target
.Value = "X"
With .Font
.Name = "Arial"
.FontStyle = "Bold"
.Size = 14
.ColorIndex = 3
End With

Plus this one



Set rng = Range("P2:P501")

If Intersect(Target, rng) Is Nothing Then Exit Sub

If Target.Value <> "X" Then
With Target
.Value = "X"
With .Font
.Name = "Arial"
.FontStyle = "Bold"
.Size = 14
.ColorIndex = 5
End With
 
Last edited:

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Try reversing the If Intersect logic...

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Set Rng = Range("O2:O501")
If Not Intersect(Target, Rng) Is Nothing Then
    If Target.Value <> "X" Then
        With Target
            .Value = "X"
            With .Font
            .Name = "Arial"
            .FontStyle = "Bold"
            .Size = 14
            .ColorIndex = 3
        End With
    End If
End If
Set Rng = Range("P2:P501")
If Not Intersect(Target, Rng) Is Nothing Then
    If Target.Value <> "X" Then
        With Target
            .Value = "X"
            With .Font
            .Name = "Arial"
            .FontStyle = "Bold"
            .Size = 14
            .ColorIndex = 5
        End With
    End If
End If
 
Last edited:
Upvote 0
Try reversing the If Intersect logic...

Rich (BB code):
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Set Rng = Range("O2:O501")
If Not Intersect(Target, Rng) Is Nothing Then
    If Target.Value <> "X" Then
        With Target
            .Value = "X"
            With .Font
            .Name = "Arial"
            .FontStyle = "Bold"
            .Size = 14
            .ColorIndex = 3
        End With
    End If
End If
Set Rng = Range("P2:P501")
If Not Intersect(Target, Rng) Is Nothing Then
    If Target.Value <> "X" Then
        With Target
            .Value = "X"
            With .Font
            .Name = "Arial"
            .FontStyle = "Bold"
            .Size = 14
            .ColorIndex = 5
        End With
    End If
End If


I get an error "End if with outblock If"
 
Upvote 0
Ahh, it was actually the End WITH that was missing

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Set Rng = Range("O2:O501")
If Not Intersect(Target, Rng) Is Nothing Then
    If Target.Value <> "X" Then
        With Target
            .Value = "X"
            With .Font
                .Name = "Arial"
                .FontStyle = "Bold"
                .Size = 14
                .ColorIndex = 3
            End With
        End With
    End If
End If
Set Rng = Range("P2:P501")
If Not Intersect(Target, Rng) Is Nothing Then
    If Target.Value <> "X" Then
        With Target
            .Value = "X"
            With .Font
                .Name = "Arial"
                .FontStyle = "Bold"
                .Size = 14
                .ColorIndex = 5
            End With
        End With
    End If
End If
End Sub
 
Upvote 0
yes, but the cells are not locked

I don't think that matters....

Try this, adjust SheetName to whatever is the appropriate sheet you're working on. And put in the correct password, or remove "PasswordHere" if there is no password on the protection...


Sheets("SheetName").Unprotect "PasswordHere"

rest of code here

Sheets("SheetName").Protect "PasswordHere"
 
Upvote 0
I don't think that matters....

Try this, adjust SheetName to whatever is the appropriate sheet you're working on. And put in the correct password, or remove "PasswordHere" if there is no password on the protection...


Sheets("SheetName").Unprotect "PasswordHere"

rest of code here

Sheets("SheetName").Protect "PasswordHere"

Do I post the unprotect before the "worksheet change event sub" line or after
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,047
Members
448,940
Latest member
mdusw

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