+ Reply to Thread
Results 1 to 6 of 6

Macros to work in all worksheets

Hybrid View

bigband1 Macros to work in all... 03-16-2016, 07:44 AM
StephenR Re: Macros to work in all... 03-16-2016, 08:09 AM
bigband1 Re: Macros to work in all... 03-16-2016, 08:21 AM
StephenR Re: Macros to work in all... 03-16-2016, 09:34 AM
bigband1 Re: Macros to work in all... 03-16-2016, 09:46 AM
StephenR Re: Macros to work in all... 03-16-2016, 09:58 AM
  1. #1
    Forum Contributor
    Join Date
    04-30-2011
    Location
    wirral,england
    MS-Off Ver
    Excel 2010
    Posts
    148

    Macros to work in all worksheets

    Hello All
    The attached macro will only work on Sheet 1,is it possible to modify it to work on any worksheet.
    Many thanks for help given.

    Sub ColorMinMaxRates()

    'Revised to color min and max in group's columns

    Dim GroupName As String
    Dim GroupRng As Range
    Dim i As Long, j As Long
    Dim MaxRate As Double
    Dim MinRate As Double
    Dim NextName As String
    Dim R As Long
    Dim Rng As Range
    Dim RngEnd As Range
    Dim StartRow As Long
    Dim wks As Worksheet

    Set wks = Worksheets("Sheet1")
    Set Rng = wks.Range("A2:AP2")

    Set RngEnd = wks.Cells(Rows.Count, Rng.Column).End(xlUp)
    If RngEnd.Row < Rng.Row Then Exit Sub

    Set Rng = Rng.Resize(RowSize:=RngEnd.Row - Rng.Row + 1)

    Application.ScreenUpdating = False

    StartRow = 1

    For R = 1 To Rng.Rows.Count

    GroupName = Rng.Item(R, 1) & Rng.Item(R, 4) & Rng.Item(R, 5)
    NextName = Rng.Item(R + 1, 1) & Rng.Item(R + 1, 4) & Rng.Item(R + 1, 5)

    If GroupName <> NextName Then
    Set GroupRng = Rng.Item(StartRow, 6).Resize(R - StartRow + 1, 5)

    For i = 11 To 21

    MinRate = WorksheetFunction.Min(GroupRng.Columns(i))
    MaxRate = WorksheetFunction.Max(GroupRng.Columns(i))
    For j = 1 To GroupRng.Rows.Count
    Select Case GroupRng.Item(j, i)
    Case MinRate
    GroupRng.Item(j, i).Font.Color = vbGreen
    Case MaxRate
    GroupRng.Item(j, i).Font.Color = vbRed
    End Select
    Next j
    Next i

    StartRow = R + 1
    End If

    Next R

    Application.ScreenUpdating = True

    End Sub

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Macros to work in all worksheets

    bigband1 - please add code tags to your post as per the forum rules.

  3. #3
    Forum Contributor
    Join Date
    04-30-2011
    Location
    wirral,england
    MS-Off Ver
    Excel 2010
    Posts
    148

    Re: Macros to work in all worksheets

    Sub ColorMinMaxRates()
    
    'Revised to color min and max in group's columns
    
    Dim GroupName As String
    Dim GroupRng As Range
    Dim i As Long, j As Long
    Dim MaxRate As Double
    Dim MinRate As Double
    Dim NextName As String
    Dim R As Long
    Dim Rng As Range
    Dim RngEnd As Range
    Dim StartRow As Long
    Dim wks As Worksheet
    
    Set wks = Worksheets("Sheet1")
    Set Rng = wks.Range("A2:AP2")
    
    Set RngEnd = wks.Cells(Rows.Count, Rng.Column).End(xlUp)
    If RngEnd.Row < Rng.Row Then Exit Sub
    
    Set Rng = Rng.Resize(RowSize:=RngEnd.Row - Rng.Row + 1)
    
    Application.ScreenUpdating = False
    
    StartRow = 1
    
    For R = 1 To Rng.Rows.Count
    
    GroupName = Rng.Item(R, 1) & Rng.Item(R, 4) & Rng.Item(R, 5)
    NextName = Rng.Item(R + 1, 1) & Rng.Item(R + 1, 4) & Rng.Item(R + 1, 5)
    
    If GroupName <> NextName Then
    Set GroupRng = Rng.Item(StartRow, 6).Resize(R - StartRow + 1, 5)
    
    For i = 11 To 21
    
    MinRate = WorksheetFunction.Min(GroupRng.Columns(i))
    MaxRate = WorksheetFunction.Max(GroupRng.Columns(i))
    For j = 1 To GroupRng.Rows.Count
    Select Case GroupRng.Item(j, i)
    Case MinRate
    GroupRng.Item(j, i).Font.Color = vbGreen
    Case MaxRate
    GroupRng.Item(j, i).Font.Color = vbRed
    End Select
    Next j
    Next i
    
    StartRow = R + 1
    End If
    
    Next R
    
    Application.ScreenUpdating = True
    
    End Sub

  4. #4
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Macros to work in all worksheets

    For info, you could have just editted your post.

    Do you want this code to run on all sheets or just one that you specify?

  5. #5
    Forum Contributor
    Join Date
    04-30-2011
    Location
    wirral,england
    MS-Off Ver
    Excel 2010
    Posts
    148

    Re: Macros to work in all worksheets

    I would like the code to work on any sheet in the workbook.
    Thanks.

  6. #6
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Macros to work in all worksheets

    Ok, so either you could manually change this line
    Set wks = Worksheets("Sheet1")
    or add the sheet name as a parameter
    Sub ColorMinMaxRates(s As String)
    
    'Revised to color min and max in group's columns
    
    Dim GroupName As String
    Dim GroupRng As Range
    Dim i As Long, j As Long
    Dim MaxRate As Double
    Dim MinRate As Double
    Dim NextName As String
    Dim R As Long
    Dim Rng As Range
    Dim RngEnd As Range
    Dim StartRow As Long
    Dim wks As Worksheet
    
    Set wks = Worksheets(s)
    Set Rng = wks.Range("A2:AP2")
    
    Set RngEnd = wks.Cells(Rows.Count, Rng.Column).End(xlUp)
    If RngEnd.Row < Rng.Row Then Exit Sub
    
    Set Rng = Rng.Resize(RowSize:=RngEnd.Row - Rng.Row + 1)
    
    Application.ScreenUpdating = False
    
    StartRow = 1
    
    For R = 1 To Rng.Rows.Count
        GroupName = Rng.Item(R, 1) & Rng.Item(R, 4) & Rng.Item(R, 5)
        NextName = Rng.Item(R + 1, 1) & Rng.Item(R + 1, 4) & Rng.Item(R + 1, 5)
        If GroupName <> NextName Then
            Set GroupRng = Rng.Item(StartRow, 6).Resize(R - StartRow + 1, 5)
            For i = 11 To 21
                MinRate = WorksheetFunction.Min(GroupRng.Columns(i))
                MaxRate = WorksheetFunction.Max(GroupRng.Columns(i))
                For j = 1 To GroupRng.Rows.Count
                    Select Case GroupRng.Item(j, i)
                    Case MinRate
                        GroupRng.Item(j, i).Font.Color = vbGreen
                    Case MaxRate
                        GroupRng.Item(j, i).Font.Color = vbRed
                    End Select
                Next j
            Next i
            StartRow = R + 1
        End If
    Next R
    
    Application.ScreenUpdating = True
    
    End Sub
    so you would need another macro to call it
    Sub x()
    ColorMinMaxRates ("Sheet1")
    End Sub
    or it could be adjusted so that it just ran on whichever sheet was active at the time. So plenty of options...

+ 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. [SOLVED] Macro Doesn't Work Through Button, Does Work Through Developer ->Macros Option
    By freybe06 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-27-2013, 11:55 AM
  2. Macros stop to work when work sheet is protected. Run time error 1004
    By sellim in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-18-2012, 01:14 AM
  3. [SOLVED] How to globally get Macros to work around "Protected" /Locked Worksheets and Workbooks
    By nenadmail in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 05-23-2012, 03:21 AM
  4. [SOLVED] deleting worksheets from a file which has macros in it........does not work.
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 05-17-2012, 12:56 PM
  5. Macros to work on individual worksheets
    By thunder-foot in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-23-2009, 12:43 PM
  6. [SOLVED] Getting embeded Excel worksheets with macros in Word to work
    By dwight.yorke@gmail.com in forum Excel General
    Replies: 3
    Last Post: 06-20-2006, 02:35 PM
  7. [SOLVED] Getting embeded Excel worksheets with macros in Word to work
    By dwight.yorke@gmail.com in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-19-2006, 11:50 AM

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