Excel Minimum search which crashes when there are no values :: MrExcel Message Board


 FAQFAQ
   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   FavoritesFavorites   StatisticsStatistics 
 RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Online StoreOnline Store

MrExcel Message Board Forum Index -> Excel Questions

Minimum search which crashes when there are no values
Post new topic   Reply to topic
Last Thread | Next Thread  >   Printable version
  Author    Thread

simmo
Board Regular


Joined: 29 Oct 2002
Posts: 34
Location: East Yorkshire England

Status: Offline

 Reply with quote  

Here is code which finds minimum values in certain columns, it works great,except when there are no values in some columns, can anyone suggest any modifications that would help.
Regards
Simmo

' Sub FormatMinimum()
Range("C4:C120,E4:E120,G4:G120,I4:I120,K4:K120,L4:L120").Interior.ColorIndex = 0

Dim MinRange1 As Range, MinRange2 As Range, MinRange3 As Range, MinRange4 As Range, MaxRange5 As Range, MaxRange6 As Range

Dim MinVal1 As Long, MinVal2 As Long, MinVal3 As Long, MinVal4 As Long, MaxVal5 As Long, MaxVal6 As Long

Set MinRange1 = Range("N4:N120")
Set MinRange2 = Range("O4:O120")
Set MinRange3 = Range("P4:P120")
Set MinRange4 = Range("Q4:Q120")
Set MaxRange5 = Range("K4:K120")
Set MaxRange6 = Range("L4:L120")

MinVal1 = Application.WorksheetFunction.Min(MinRange1)
MinRange1.Find(What:=MinVal1, After:=Range("N4"), LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False).Offset(0, -11).Interior.ColorIndex = 44

MinVal2 = Application.WorksheetFunction.Min(MinRange2)
MinRange2.Find(What:=MinVal2, After:=Range("O4"), LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False).Offset(0, -10).Interior.ColorIndex = 44

MinVal3 = Application.WorksheetFunction.Min(MinRange3)
MinRange3.Find(What:=MinVal3, After:=Range("P4"), LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False).Offset(0, -9).Interior.ColorIndex = 44

Post Fri Nov 08, 2002 11:33 am 
 View user's profile Send private message Send e-mail

Andrew Poulsom
MrExcel MVP


Joined: 22 Jul 2002
Posts: 8178

Flag: Uk

Status: Offline

 Reply with quote  

Something like this:

code:

Dim x As Range

MinVal1 = Application.WorksheetFunction.Min(MinRange1)
Set x = MinRange1.Find(What:=MinVal1, After:=Range("N4"), LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False)
If Not x Is Nothing Then
   x.Offset(0, -11).Interior.ColorIndex = 44
End If


Post Fri Nov 08, 2002 11:49 am 
 View user's profile Send private message

simmo
Board Regular


Joined: 29 Oct 2002
Posts: 34
Location: East Yorkshire England

Status: Offline

 Reply with quote  

Thanks for that piece of code Andrew, do i replace my "MinVal1" statement with the one you have written ? and then replace / edit MinVal2,MinVal3 and MinVal4. I dont mean to sound dumb, but I have lots to learn.

Post Mon Nov 11, 2002 10:50 am 
 View user's profile Send private message Send e-mail

Andrew Poulsom
MrExcel MVP


Joined: 22 Jul 2002
Posts: 8178

Flag: Uk

Status: Offline

 Reply with quote  

Replace:

code:

MinVal1 = Application.WorksheetFunction.Min(MinRange1)
MinRange1.Find(What:=MinVal1, After:=Range("N4"), LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False).Offset(0, -11).Interior.ColorIndex = 44




with

code:

MinVal1 = Application.WorksheetFunction.Min(MinRange1)
Set x = MinRange1.Find(What:=MinVal1, After:=Range("N4"), LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False)
If Not x Is Nothing Then
   x.Offset(0, -11).Interior.ColorIndex = 44
End If




and repeat for the other two blocks, changing variable and range references as necessary.

Dim x As Range

goes in once at the top.

Post Mon Nov 11, 2002 11:10 am 
 View user's profile Send private message
  Display posts from previous:      

MrExcel Message Board Forum Index -> Excel Questions


Forum Jump:
Jump to:  

Post new topic   Reply to topic
Page 1 of 1



Add To Favorites

 


Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB: 2.0.4 © 2001 phpBB Group

Need help posting your first question? Read how to post

Need extra help ? Couldn't get the answer you needed ? Get a free quote from our Consulting Team

Download Colo's HTML Maker utility for displaying your Excel Worksheet on the board.

Download VB HTML Maker to post your code on the board


Check out our new index to 485 Excel Articles.


Return to MrExcel Consulting

All contents Copyright 1998-2004 by MrExcel.com
If you believe information posted here is from your copyrighted source, notify us per the Terms of Use
Excel is a registered trademark of the Microsoft Corporation.
MrExcel is a registered trademark of Tickling Keys, Inc.