• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member
  • Login

Macros for highlighting max min values|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Macros for highlighting max min values|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosMacros for highlighting max min val…
sp_PrintTopic sp_TopicIcon
Macros for highlighting max min values
Avatar
Paul Chong

Active Member
Members
Level 0
Forum Posts: 4
Member Since:
October 27, 2019
sp_UserOfflineSmall Offline
1
October 27, 2019 - 3:35 pm
sp_Permalink sp_Print

Can someone kindly point me to some macros for highlighting max and min values in an excel sheet with data in columns D8 to f20. Examples from the net returns a lot of errors which I do not comprehend as Iam a newbie. Thanks and much appreciated.

 

Chong

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1549
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
2
October 28, 2019 - 9:30 am
sp_Permalink sp_Print

Hi Chong,

Do you have to use VBA?  have you tried Conditional Formatting?

Regards

Phil

Avatar
Paul Chong

Active Member
Members
Level 0
Forum Posts: 4
Member Since:
October 27, 2019
sp_UserOfflineSmall Offline
3
October 28, 2019 - 1:07 pm
sp_Permalink sp_Print

Thanks for the query Phil. Unfortunately yes as part of a course assignment I have to use macro in order to show that macros are used. I tried using this

Dim zelle As Range

Dim rng As Range

Dim lCol As Long

Dim lLastRow As Long

  With ActiveSheet

    For lCol = e8 To .UsedRange.Columns.Count

      lLastRow = .Cells(.Rows.Count, lCol).End(xlUp).Row

      Set rng = .Range(.Cells(e8, lCol), .Cells(lLastRow, lCol))

      For Each zelle In rng

        If zelle.Value = Application.WorksheetFunction.Max(rng) Then

          zelle.Interior.ColorIndex = 6

        Else

          zelle.Interior.ColorIndex = xlNone

        End If

      Next

    Next lCol

  End With

End Sub

 

but this line needs debugging with a runtime error of 1004

  lLastRow = .Cells(.Rows.Count, lCol).End(xlUp).Row

 

Any advise with regards to the macro above or the line concerned

 

Much appreciated for your help.

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1549
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
4
October 28, 2019 - 2:09 pm
sp_Permalink sp_Print

Hi,

Can you supply the workbook please.

Phil

Avatar
Paul Chong

Active Member
Members
Level 0
Forum Posts: 4
Member Since:
October 27, 2019
sp_UserOfflineSmall Offline
5
October 28, 2019 - 8:20 pm
sp_Permalink sp_Print

Worksheet is as follows

                 
                 
                 
                 
                 
                 
                 
    S/N Name Gender English Maths Physics  
    1 Jane female 98 89 77  
    2 John male 85 86 76  
    3 Jennifer female 72 78 82  
    4 Matthew male 68 79 85  
    5 Marcus male 75 81 78  
    6 Mary female 69 74 59  
    7 Simon male 82 65 74  
    8 Timothy male 59 88 69  
    9 Dennis male 78 76 74  
    10 Alex male 62 71 62  
    11 Aaron male 87 68 72  
    12 Edith female 77 59 70  
                 
                 
                 
                 
                 

S/N is in cell C8.

 

Thanks

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1549
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
6
October 29, 2019 - 10:04 am
sp_Permalink sp_Print

Hi Chong,

See attached workbook.

Regards

Phil

Avatar
Paul Chong

Active Member
Members
Level 0
Forum Posts: 4
Member Since:
October 27, 2019
sp_UserOfflineSmall Offline
7
October 29, 2019 - 7:33 pm
sp_Permalink sp_Print

Dear Phil,

Thanks for the help.

The macros work for both the min and max values as individual macros controlled by different command buttons. I can also see where the errors occur. Now having solve that part I was wondering if I can combine both the max and min values in one macro but using different colors. Can that be done?

Once again thanks for the help.

Chong

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1549
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
8
October 30, 2019 - 10:07 am
sp_Permalink sp_Print

No worries.

After the set rng statement modify your code to this

 

  rng.Interior.ColorIndex = xlNone

  For Each zelle In rng

    If zelle.Value = Application.WorksheetFunction.Max(rng) Then

      zelle.Interior.ColorIndex = 6

    End If

    If zelle.Value = Application.WorksheetFunction.Min(rng) Then

      zelle.Interior.ColorIndex = 7

    End If

  Next

 

Regards

Phil

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Charlotte Holloway, Lorna Henning, Tracy English, Mohamed Touahria
Guest(s) 11
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 880
Purfleet: 414
Frans Visser: 346
David_Ng: 306
lea cohen: 237
Jessica Stewart: 219
A.Maurizio: 213
Aye Mu: 201
jaryszek: 183
Newest Members:
David von Kleek
Ronald White
Ginette Guevremont
Taryn Ambrosi
Mark Davenport
Christy Nichols
Harald Endres
Ashley Hughes
Herbie Key
Trevor Pindling
Forum Stats:
Groups: 3
Forums: 24
Topics: 6528
Posts: 28594

 

Member Stats:
Guest Posters: 49
Members: 32817
Moderators: 2
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel Office Scripts
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook

Sign up to our newsletter and join over 400,000
others who learn Excel and Power BI with us.

 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate
  • Sponsor Our Newsletter

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.