Add simple condition to code that prevents code running

Ironman

Well-known Member
Joined
Jan 31, 2004
Messages
1,069
Office Version
  1. 365
Platform
  1. Windows
Hi

The code below runs when any cell in column H row 12 downwards is double-clicked
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

    ' check Target is in column H
    If Target.Column = 8 And Target.Row >= 12 Then
        Cancel = True
        TopCell = Cells(12, 3).Address 'Row 12, Column 3 i.e. mileage
        BottomCell = Cells(Target.Row, 3).Address

        TotalCalc = Application.WorksheetFunction.Sum(Range(TopCell, BottomCell))

        MsgBox "Total miles run to " & Format(Cells(ActiveCell.Row, 1), "dd mmmm yyyy: ") & _
                     Format((CLng(10720.31 + TotalCalc)), "#,##0") & "   ", vbOKOnly, "Lifetime Mileage"
    End If

I'm looking for an additional row of code so the above code does not run if Column B in the same row contains the word 'OTHER' (without the commas).

Many thanks!
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Try

Rich (BB code):
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

    ' check Target is in column H
    
If Cells(Target.Row, "B").Value = "OTHER" Then Exit Sub
If Target.Column = 8 And Target.Row >= 12 Then
        Cancel = True
        TopCell = Cells(12, 3).Address 'Row 12, Column 3 i.e. mileage
        BottomCell = Cells(Target.Row, 3).Address

        TotalCalc = Application.WorksheetFunction.Sum(Range(TopCell, BottomCell))

        MsgBox "Total miles run to " & Format(Cells(ActiveCell.Row, 1), "dd mmmm yyyy: ") & _
                     Format((CLng(10720.31 + TotalCalc)), "#,##0") & "   ", vbOKOnly, "Lifetime Mileage"
    End If
End Sub
 
Upvote 0
Solution
Many thanks Michael, that works perfectly!

Could I just ask you how the code would need to look if I wanted to also exclude the word 'REST' please?

Thanks again!
 
Upvote 0
get you explain a bit more please on that request
 
Upvote 0
Sure! So the above code does not run if Column B in the same row contains either the word 'OTHER' or 'REST'.

Thanks again.
 
Upvote 0
VBA Code:
If Cells(Target.Row, "B").Value = "OTHER" OR Cells(Target.Row, "B").Value = "REST" Then Exit Sub
 
Upvote 0

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

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