Limit macro to certain cell range

MeaclH

Board Regular
Joined
Apr 2, 2014
Messages
77
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
G'day,
I have a macro that runs on my sheet perfectly fine, however, I have decided I would like to prevent it being run on any cell on the sheet. Users are making errors and running the macro on cells that they shouldn't.

Is there a modification I can make to the code to limit he execution to G5:T9
And if a user does run it on any other cell they are prompted with a custom error message?

VBA Code:
Sub MarkShiftCovered()
     Dim sCmt As String
    Dim rCell As Range
    
    sCmt = InputBox( _
      Prompt:="Have you covered this shift in it's entirety?" & vbCrLf & _
      "Please add details of how the shift has been covered. ie. OFF roster, extensions.", _
      Title:="Comment to Add")
    If sCmt = "" Then
        MsgBox "Not actioned. Has the shift been covered?"
    If StrPtr(sCmt) = 0 Then Exit Sub 'user cancelled inputbox
            Else
        For Each rCell In Selection
            With rCell
                ActiveSheet.Unprotect
                .ClearComments
                .AddComment
                .Comment.Text Text:=sCmt
            End With
        Next
    End If
    Set rCell = Nothing
Selection.Font.Color = RGB(0, 0, 0)
Selection.Interior.ColorIndex = xlNone
Selection.Font.Underline = False
Selection.Font.Bold = False
Selection.Font.Strikethrough = True
ActiveSheet.Protect DrawingObjects:=False
End Sub

Cheers,
Hayden
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
This update should only run your code on the selected cells found within G5:T9.
VBA Code:
Sub MarkShiftCovered()
    Dim sCmt As String
    Dim rng As Range
    Dim rCell As Range
    
    sCmt = InputBox( _
      Prompt:="Have you covered this shift in it's entirety?" & vbCrLf & _
      "Please add details of how the shift has been covered. ie. OFF roster, extensions.", _
      Title:="Comment to Add")
    If sCmt = "" Then
        MsgBox "Not actioned. Has the shift been covered?"
    If StrPtr(sCmt) = 0 Then Exit Sub 'user cancelled inputbox
            Else
            
'       Limit to specific range
        Set rng = Intersect(Selection, Range("G5:T9"))
        
'       Exit if no cells in specified range
        If rng Is Nothing Then
            MsgBox "You range selection does not contain any cells in G5:T9", vbOKOnly, "ERROR!"
            Exit Sub
        End If
            
        For Each rCell In rng
            With rCell
                ActiveSheet.Unprotect
                .ClearComments
                .AddComment
                .Comment.Text Text:=sCmt
            End With
        Next
    End If
    Set rCell = Nothing
    
    rng.Font.Color = RGB(0, 0, 0)
    rng.Interior.ColorIndex = xlNone
    rng.Font.Underline = False
    rng.Font.Bold = False
    rng.Font.Strikethrough = True
    ActiveSheet.Protect DrawingObjects:=False

End Sub
 
Upvote 0
Solution
This update should only run your code on the selected cells found within G5:T9.
VBA Code:
Sub MarkShiftCovered()
    Dim sCmt As String
    Dim rng As Range
    Dim rCell As Range
   
    sCmt = InputBox( _
      Prompt:="Have you covered this shift in it's entirety?" & vbCrLf & _
      "Please add details of how the shift has been covered. ie. OFF roster, extensions.", _
      Title:="Comment to Add")
    If sCmt = "" Then
        MsgBox "Not actioned. Has the shift been covered?"
    If StrPtr(sCmt) = 0 Then Exit Sub 'user cancelled inputbox
            Else
           
'       Limit to specific range
        Set rng = Intersect(Selection, Range("G5:T9"))
       
'       Exit if no cells in specified range
        If rng Is Nothing Then
            MsgBox "You range selection does not contain any cells in G5:T9", vbOKOnly, "ERROR!"
            Exit Sub
        End If
           
        For Each rCell In rng
            With rCell
                ActiveSheet.Unprotect
                .ClearComments
                .AddComment
                .Comment.Text Text:=sCmt
            End With
        Next
    End If
    Set rCell = Nothing
   
    rng.Font.Color = RGB(0, 0, 0)
    rng.Interior.ColorIndex = xlNone
    rng.Font.Underline = False
    rng.Font.Bold = False
    rng.Font.Strikethrough = True
    ActiveSheet.Protect DrawingObjects:=False

End Sub
works perfectly. Thanks legend!!!
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,186
Members
452,615
Latest member
bogeys2birdies

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