+ Reply to Thread
Results 1 to 2 of 2

Move code from Sheet to ThisWorkbook/Module

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-07-2014
    Location
    Nottingham
    MS-Off Ver
    Office 2016, Office 365
    Posts
    400

    Move code from Sheet to ThisWorkbook/Module

    I have the attached workbook which I have had help on with the code.

    I now need to look at moving some of the code from Sheet to either ThisWorkbook or Module. The idea of this workbook will be that I will have all of our staff sick episodes on individual worksheets, so i understand the need to not have too much code on a worksheet and copied every time, so if someone could point me in the right direction please.
    Attached Files Attached Files

  2. #2
    Forum Contributor
    Join Date
    07-07-2014
    Location
    Nottingham
    MS-Off Ver
    Office 2016, Office 365
    Posts
    400

    Re: Move code from Sheet to ThisWorkbook/Module

    Code I have so far is:

    SHEET1

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim rngMonitor  As Range
        Dim rng         As Range
    
        Set rngMonitor = Intersect(Range("A6:A199"), Target)
    
        If Not rngMonitor Is Nothing Then
            Application.EnableEvents = False
            MsgBox "Please do not change the data in column A", vbExclamation
            Application.Undo
            Application.EnableEvents = True
            Exit Sub
        End If
    
    
        Set rngMonitor = Intersect(Range("B6:B199"), Target)
    
        If Not rngMonitor Is Nothing Then
            Call AutoFilter_Example1
        End If
    
    End Sub
    
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        If Len(Me.Range("D200").Value) = 0 Then Exit Sub
    
        If Application.ActiveSheet.Name <> VBA.Left(Me.Range("D200").Value, 31) Then
            Application.ActiveSheet.Name = VBA.Left(Me.Range("D200").Value, 31)
        End If
    
    End Sub
    
    
    Sub AutoFilter_Example1()
        Dim i           As Long
    
        Application.Calculation = xlCalculationManual
        Application.ScreenUpdating = False
    
        With ActiveSheet
            If .AutoFilterMode Then
                .AutoFilterMode = False
            End If
    
            With .Range("A5:I" & Cells(Rows.Count, "A").End(xlUp).Row)
                For i = 1 To .Columns.Count
                    .AutoFilter Field:=i, VisibleDropDown:=False
                Next i
                .AutoFilter Field:=1, Criteria1:="<>Hide"
                .AutoFilter Field:=9, Criteria1:=""
            End With
        End With
    
        Application.Calculation = xlCalculationAutomatic
    
    End Sub

    THISWORKBOOK

    Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
        If Sh.Name = ActiveSheet.Name Then
            If Range("E200") = "Yes" Or Range("G200") > 4 Then
                If Sh.Tab.Color <> RGB(255, 0, 0) Then    ' Red
                    Sh.Tab.Color = RGB(255, 0, 0)
                End If
            Else
                If Sh.Tab.Color <> RGB(146, 208, 80) Then
                    Sh.Tab.Color = RGB(146, 208, 80)    ' Light Green
                End If
            End If
        End If
    End Sub

    MODULE1

    Sub Sort_Tabs_Alphabetically()
    
        For i = 1 To Application.Sheets.Count
            For j = 1 To Application.Sheets.Count - 1
                If UCase$(Application.Sheets(j).Name) > UCase$(Application.Sheets(j + 1).Name) Then
                    Sheets(j).Move after:=Sheets(j + 1)
                End If
            Next
        Next
    
        MsgBox "The tabs have been sorted from A to Z."
    
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Specifying a sheet & range in ThisWorkbook module
    By vwhite in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-01-2018, 09:49 AM
  2. .xla ThisWorkbook module generates code on save
    By ignominousicon17 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-22-2018, 06:07 PM
  3. Difference between sheet, module and thisworkbook in excel
    By elfor in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-01-2017, 09:50 AM
  4. how to access Sheet module, normal module, Worbook module to type code
    By alibaba in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-31-2009, 07:51 AM
  5. Code in 'ThisWorkbook' module in add-in?
    By karan in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 05-04-2009, 03:36 AM
  6. Can I add code to ThisWorkbook module with VBA extensibility?
    By mikeb in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-11-2006, 11:15 PM
  7. Calls from sheet module to ThisWorkbook module
    By quartz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-23-2005, 11:05 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1