VBA - call function within With..End With

logstarter

New Member
Joined
Apr 14, 2017
Messages
39
Hi,

I have written something like

Code:
Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Dim ChangedRow As Integer
    With Sh
    
        If Not Intersect(Target, .Columns(3)) Is Nothing Then
            On Error GoTo bm_Safe_Exit
            Application.EnableEvents = False
            ChangedRow = Target.Row
            If WorksheetFunction.CountA(Target) > 0 Then
                confirmChange = MsgBox("Are you sure you want to assign the code?", vbYesNo + vbQuestion)
                If confirmChange = vbYes Then
                    Range("G" & ChangedRow) = createNo
                End If
            End If
        End If
        
        If Not Intersect(Target, .Columns(9)) Is Nothing Then
            On Error GoTo bm_Safe_Exit
            Application.EnableEvents = False
            ChangedRow = Target.Row
            If WorksheetFunction.CountA(Target) > 0 Then
                confirmChange = MsgBox("Are you sure you want to assign the code?", vbYesNo + vbQuestion)
                If confirmChange = vbYes Then
                    Range("M" & ChangedRow) = createNo
                End If
            End If
        End If

Since the code is repetitious, I would like to use a function like

Code:
Sub addNo(ByVal Sh As Object, ByVal Target As Range, ByVal col As Integer, ByVal NoCol As String)
  If Not Intersect(Target, .Columns(col)) Is Nothing Then
            On Error GoTo bm_Safe_Exit
            Application.EnableEvents = False
            ChangedRow = Target.Row
            If WorksheetFunction.CountA(Target) > 0 Then
                confirmChange = MsgBox("Are you sure you want to assign the code?", vbYesNo + vbQuestion)
                If confirmChange = vbYes Then
                    Range(NoCol & ChangedRow) = createNo
                End If
            End If
        End If
End Sub

My problem is that since there is
Code:
If Not Intersect(Target, [COLOR=#ff0000].Columns(col)[/COLOR]) Is Nothing Then

I would like to ask how to edit my function so that it could support .Column(col) with "With Sh".

Thank you.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
If you're always offsetting 4 columns, try
Code:
Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
   Dim ChangedRow As Integer
   
   If Not Intersect(Target, Sh.Range("C:C,I:I")) Is Nothing Then
      On Error GoTo bm_Safe_Exit
      Application.EnableEvents = False
      ChangedRow = Target.Row
      If WorksheetFunction.CountA(Target) > 0 Then
         confirmChange = MsgBox("Are you sure you want to assign the code?", vbYesNo + vbQuestion)
         If confirmChange = vbYes Then
            Target.Offset(, 4).Value = createNo
         End If
      End If
   End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,958
Messages
6,175,629
Members
452,661
Latest member
Nonhle

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