Apply VBA Code to All Worksheets

nryan

Board Regular
Joined
Apr 3, 2015
Messages
61
Hello,

The help I've found on this subject has been great, but now I think my problem is more specific. I have the following code applied to one worksheet (right click sheet tab and click "view code") and it works very well:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Cells.Count > 1 Then Exit Sub

    If Not Intersect(Target, Range("C10:C100000")) Is Nothing Then

        With Target(1, -1)

            .Value = Date
            .EntireColumn.AutoFit

        End With
            
        With Target(1, 0)
                
            .Value = Time
            .EntireColumn.AutoFit
                
        End With

    End If


Columns("B").Hidden = True


    Dim MyRange As Range, c As Range
    
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    
    Set MyRange = Range("A2:A4436")
    MyRange.EntireRow.Hidden = False
    
    For Each c In MyRange
        If IsDate(c.Value) And c.Value < Date Then
            c.EntireRow.Hidden = True
        End If

    Next
    
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic


End Sub

Now I tried to apply this code to ALL worksheets by putting it in ThisWorkbook in the VB editor (right click "ThisWorkbook" in the VB editor and click "view code"). All I changed was the first line of the code. Here is that code:

Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

If Target.Cells.Count > 1 Then Exit Sub

    If Not Intersect(Target, Range("C10:C100000")) Is Nothing Then

        With Target(1, -1)

            .Value = Date
            .EntireColumn.AutoFit

        End With
            
        With Target(1, 0)
                
            .Value = Time
            .EntireColumn.AutoFit
                
        End With

    End If


Columns("B").Hidden = True


    Dim MyRange As Range, c As Range
    
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    
    Set MyRange = Range("A2:A4436")
    MyRange.EntireRow.Hidden = False
    
    For Each c In MyRange
        If IsDate(c.Value) And c.Value < Date Then
            c.EntireRow.Hidden = True
        End If

    Next
    
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic


End Sub

The problem is the If statement only works on the first worksheet. This statement inputs the date and time into columns "A" and "B" when column "C" is changed. The dates and times are not appearing in their columns for all sheets (except Sheet1). The problem must lie with either the If statement or the two With statements. Anyone have a solution?

Thanks very much.
-Nick
 
Last edited:

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
The only advantage to putting the code into the ThisWorkbook code module is that you don't have to have the same code in each worksheet module. But you do still have to make individual changes to each separate sheet for the code to affect that sheet. The code will not apply to all sheets by making a change to only sheet 1. You will need to use a loop if you want to do all sheets in one transaction.
 
Last edited:
Upvote 0
I feel silly. The code works fine. I forgot to change C10 back to C2 for the start of the Range. I was typing stuff into C2, C3, C4, etc. for all worksheets (except Sheet1 for some reason) and not getting anything of course because the code doesn't start inputting dates and times until row 10.

Thanks anyways JLGWhiz.
 
Upvote 0
I feel silly. The code works fine. I forgot to change C10 back to C2 for the start of the Range. I was typing stuff into C2, C3, C4, etc. for all worksheets (except Sheet1 for some reason) and not getting anything of course because the code doesn't start inputting dates and times until row 10.

Thanks anyways JLGWhiz.
Been there, done that!
Regards, JLG
 
Upvote 0

Forum statistics

Threads
1,214,952
Messages
6,122,458
Members
449,085
Latest member
ExcelError

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