Repeat VBA Code on Multiple Worksheets

Whekpop

New Member
Joined
Feb 27, 2018
Messages
8
I have the following VBA code and want to repeat it across multiple worksheets on a workbook.
---------------------------------------------------------------------
Option Explicit
Private Enum Nwc ' Worksheet columns
' 17 January 2018 ' assign any integer value to any enumeration.
' Enumerations without assigned value
' are incremented by 1 from the one above
NwcDate = 1 ' used to find the last used row
' DR and CR must be adjacent!
NwcDR = 5 ' 3 = column C
NwcCR = 6 ' [auto] = 7 =
NwcBal = 8 ' [auto] = 8 =
NwcFirstRow = 3 ' rows above NwcFirstRow are ignored
' change as required by your worksheet design
End Enum
Private Sub Worksheet_Change(ByVal Target As Range)
' 17 January 2018
Dim Rl As Long
Dim Rng As Range
Dim Bal As Double
Rl = Cells(Rows.Count, NwcDate).End(xlUp).Row
Set Rng = Range(Cells(NwcFirstRow, NwcDR), Cells(Rl, NwcCR))
With Application
.EnableEvents = False
If Not .Intersect(Target, Rng) Is Nothing Then
Rl = Cells(Rows.Count, NwcBal).End(xlUp).Row + 1
If Target.Row <> Rl Then
' the only allowed row is the one immediately under
' the last shown balance:
MsgBox "Past Entries Cannot be Modified. No Vex..." & vbCr & _
"" & vbCr & _
"", _
vbCritical, "KADFAMA"
.Undo
.Cells(Rl, Target.Column).Select
Else
Bal = .Sum(Rng.Columns(2)) - .Sum(Rng.Columns(1))
If Bal < 0 Then
MsgBox "This Debit Would Create a Negative " & vbCr & _
"Balance of " & Bal & " Which is Not Permitted." & vbCr & _
"...and Bash Insists the Entry MUST Be Removed.", _
vbExclamation, "KADFAMA"
.Undo
Else
Cells(Rl, NwcBal).Value = Bal
End If
End If
End If
.EnableEvents = True
End With
End Sub
---------------------------------------
How can I repeat this across multiple worksheets?
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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