Omit vba script from running in specific sheets.

omairhe

Well-known Member
Joined
Mar 26, 2009
Messages
2,040
Office Version
  1. 2019
Platform
  1. Windows
Hey Excel Gurus,

I have a code which runs in every sheet. However I would like to bypass this code from two specific worksheets, namely "HSheet" and "Summary"

The code is as following.

VBA Code:
Private Sub Workbook_Sheetactivate(ByVal Sh As Object)
If Worksheets("HSheet").Range("B2") <> Selection.Column And Worksheets("HSheet").Range("B3") > 21 Then
   Dim lCol As Long
    Application.EnableEvents = 0
    Application.ScreenUpdating = 0
    With Sheets("HSheet")
         lCol = .[B3]
    End With

            ActiveSheet.Cells(ActiveCell.Row, lCol).Select
            ActiveWindow.ScrollColumn = Selection.Column
    Application.EnableEvents = 1
    Application.ScreenUpdating = 1
End If
End Sub

Thanks and will appreciate...
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
hi,
try

VBA Code:
Private Sub Workbook_Sheetactivate(ByVal Sh As Object)

If Not IsError(Application.Match(Sh.Name, Array("HSheet", "Summary"), 0)) Then Exit Sub

'rest of code
End Sub

Dave
 
Upvote 0
It will work on the above code I posted.
But on the following code it is not working. How to achieve the same with the following code.

VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Not IsError(Application.Match(Sh.Name, Array("HSheet", "Summary"), 0)) Then Exit Sub
If Worksheets("HSheet").Range("B2") > 21 Then
ActiveWindow.ScrollRow = Worksheets("HSheet").Range("B1")
ActiveWindow.ScrollColumn = Selection.Column
End If
If Worksheets("HSheet").Range("B3") <> Selection.Column And Worksheets("HSheet").Range("B2") > 21 Then
    Dim ws As Worksheet, lCol As Long
    Application.ScreenUpdating = 0
    Application.EnableEvents = 0
    With Sheets("HSheet")
        .[B3] = .[B2]
         lCol = .[B2]
    End With
        For Each ws In Sheets
            ws.Activate
            ws.Cells(ActiveCell.Row, lCol).Select
              ActiveWindow.ScrollColumn = Selection.Column
        Next
        Sh.Activate
    Application.EnableEvents = 1
    Application.ScreenUpdating = 1
End If
End Sub

???
 
Upvote 0
Thanks but adding the following line fixed it. Seen some code in google and thought I give it a try. Hehe

VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Worksheets("HSheet").Range("B2") > 21 Then
ActiveWindow.ScrollRow = Worksheets("HSheet").Range("B1")
ActiveWindow.ScrollColumn = Selection.Column
End If
If Worksheets("HSheet").Range("B3") <> Selection.Column And Worksheets("HSheet").Range("B2") > 21 Then
    Dim ws As Worksheet, lCol As Long
    Application.ScreenUpdating = 0
    Application.EnableEvents = 0
    With Sheets("HSheet")
        .[B3] = .[B2]
         lCol = .[B2]
    End With
        For Each ws In Sheets
                                                                    If ws.Name <> "HSheet" And ws.Name <> "Summary" Then
                ws.Activate
                ws.Cells(ActiveCell.Row, lCol).Select
                ActiveWindow.ScrollColumn = Selection.Column
            End If
        Next
        Sh.Activate
    Application.EnableEvents = 1
    Application.ScreenUpdating = 1
End If
End Sub
 
Upvote 0
Now both the codes are omitting the Vba script.
Thank you.
 
Upvote 0
Now both the codes are omitting the Vba script.
Thank you.

Tested code & worked OK for me and not sure why you had an issue but if managed to resolve another way all well & good


Dave
 
Upvote 0

Forum statistics

Threads
1,215,461
Messages
6,124,954
Members
449,198
Latest member
MhammadishaqKhan

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