Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 79
Default Find a value and color that cell

Using Min function, I was able to find the next (soonest) date, but not able
to color the cell. I used MsgBox to see if any address was assigned to
rngFound. Then used rngFound.Cells.Activate to see where the cell was, which
resulted way below the list. What did I do wrong? Here is the code:

Sub FindNext()
Dim myRange As Range
Dim answer As String
Dim rngFound As Range

Set myRange = Worksheets("Sheet5").Range("K2:K80")
answer = Application.WorksheetFunction.Min(myRange)

Set rngFound = myRange(answer)
MsgBox rngFound.Address

answer = Format(answer, "mm/dd/yy")
MsgBox ("The next date is " & answer)
rngFound.Cells.Activate
rngFound.Font.ColorIndex = 3
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Find a value and color that cell

application.min() will result in the smallest number (including date).

For example, say 12/25/2000 was the smallest date.

Range(12/25/2000)
doesn't make much sense.

You could try using application.match() or even .cells.find().

Dim myRng As Range
Dim MinDate As Double
Dim res As Variant

With Worksheets("sheet5")
Set myRng = .Range("K2:K80")
If Application.Count(myRng) = 0 Then
MsgBox "no numbers/dates in that range!"
Exit Sub '???
Else
MinDate = Application.Min(myRng)
res = Application.Match(MinDate, myRng, 0)
If IsError(res) Then
MsgBox "min not found!"
Else
myRng(res).Font.ColorIndex = 3
End If
End If
End With




A.S. wrote:

Using Min function, I was able to find the next (soonest) date, but not able
to color the cell. I used MsgBox to see if any address was assigned to
rngFound. Then used rngFound.Cells.Activate to see where the cell was, which
resulted way below the list. What did I do wrong? Here is the code:

Sub FindNext()
Dim myRange As Range
Dim answer As String
Dim rngFound As Range

Set myRange = Worksheets("Sheet5").Range("K2:K80")
answer = Application.WorksheetFunction.Min(myRange)

Set rngFound = myRange(answer)
MsgBox rngFound.Address

answer = Format(answer, "mm/dd/yy")
MsgBox ("The next date is " & answer)
rngFound.Cells.Activate
rngFound.Font.ColorIndex = 3
End Sub


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default Find a value and color that cell

Try the following. Note the comments.

Sub FindNext()
Dim myRange As Range
Dim answer As Date
Dim rngFound As Range

Set myRange = Worksheets("Sheet5") _
.Range("K2:K80")

answer = Application _
.WorksheetFunction.Min(myRange)

Set rngFound = myRange _
.Find(What:=answer, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)

MsgBox ("The next date is " & _
Format(answer, "mm/dd/yy"))

'because this code could be run when
'another worksheet is active you must
'ensure the correct worksheet is active
'before activating cells.
'Also Activate is not necessarily the same
'as Select. You can Activate a cell in
'a selected range.(It is the cell that is
'still white color.) Select a cell means
'it is the only selection

Worksheets("Sheet5").Select
rngFound.Cells.Activate
rngFound.Font.ColorIndex = 3
End Sub

--
Regards,

OssieMac


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I change the color the cell becomes when doing a FIND Kenny A. Excel Discussion (Misc queries) 4 April 6th 10 09:05 PM
Find text in cell based on color value of another cell CR[_2_] Excel Programming 2 September 22nd 09 07:59 PM
Find the next Cell with interior color using Do Until...Loop RyanH Excel Programming 4 March 7th 08 02:54 PM
.Find cell within rng when font color=3 Jim at Eagle Excel Programming 3 March 5th 08 07:36 PM
Find purple cell color, change to red goofy11 Excel Programming 1 August 2nd 05 12:03 AM


All times are GMT +1. The time now is 06:39 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"