Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 195
Default VBA to scan range

My range, C2:AA2 contains weekday dates for any given month.
I want code that will look through that range and find the last Friday.
eg. lastFri = ActiveSheet.Range("C2:AA2").Find(<the last Friday)
For my needs, this must be a VBA solution.

Any help?

--
David
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default VBA to scan range

MsgBox Evaluate("=MAX(IF(WEEKDAY(C2:AA2)=6,C2:AA2))")

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

"David" wrote in message
...
My range, C2:AA2 contains weekday dates for any given month.
I want code that will look through that range and find the last Friday.
eg. lastFri = ActiveSheet.Range("C2:AA2").Find(<the last Friday)
For my needs, this must be a VBA solution.

Any help?

--
David



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 718
Default VBA to scan range

No error checking:

'--------------------------
Sub test()
MsgBox lastFriday(Range("C2:AA2"))
End Sub

Function lastFriday(rng As Range) As Date
Dim i As Long
For i = rng.Count To 1 Step -1
If Weekday(rng(i)) = 6 Then
lastFriday = rng(i)
Exit For
End If
Next i
End Function
'---------------------------------

"David" a écrit dans le message de
...
My range, C2:AA2 contains weekday dates for any given month.
I want code that will look through that range and find the last Friday.
eg. lastFri = ActiveSheet.Range("C2:AA2").Find(<the last Friday)
For my needs, this must be a VBA solution.

Any help?

--
David



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 195
Default VBA to scan range

Bob Phillips wrote

MsgBox Evaluate("=MAX(IF(WEEKDAY(C2:AA2)=6,C2:AA2))")


I was able to succesfully incorporate this into my existing code.
Many thanks.

It took some experimenting, because my code actually loops through several
sheets, performing the same operation on each. So, if I didn't put the
Evaluate line before the loop, sheets other than the first didn't show
desired results. Also had to insure first sheet was selected early.

--
David
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 195
Default VBA to scan range

Ardus Petus wrote

No error checking:

'--------------------------
Sub test()
MsgBox lastFriday(Range("C2:AA2"))
End Sub

Function lastFriday(rng As Range) As Date
Dim i As Long
For i = rng.Count To 1 Step -1
If Weekday(rng(i)) = 6 Then
lastFriday = rng(i)
Exit For
End If
Next i
End Function
'---------------------------------

"David" a écrit dans le message de
...
My range, C2:AA2 contains weekday dates for any given month.
I want code that will look through that range and find the last Friday.
eg. lastFri = ActiveSheet.Range("C2:AA2").Find(<the last Friday)
For my needs, this must be a VBA solution.

Any help?

--
David




Long way around, but works. Thanks for your contribution.

--
David


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 430
Default VBA to scan range

Isn't this code just finding the first Right-most Friday
and stopping? What if 2 or 3 columns more
to the left you had another LATER Friday; Wouldn't
this code not consider it?
Sorry, just trying to understand various questions asked
and solutions offered..


"David" wrote in message
...
Ardus Petus wrote

No error checking:

'--------------------------
Sub test()
MsgBox lastFriday(Range("C2:AA2"))
End Sub

Function lastFriday(rng As Range) As Date
Dim i As Long
For i = rng.Count To 1 Step -1
If Weekday(rng(i)) = 6 Then
lastFriday = rng(i)
Exit For
End If
Next i
End Function
'---------------------------------

"David" a écrit dans le message de
...
My range, C2:AA2 contains weekday dates for any given month.
I want code that will look through that range and find the last Friday.
eg. lastFri = ActiveSheet.Range("C2:AA2").Find(<the last Friday)
For my needs, this must be a VBA solution.

Any help?

--
David




Long way around, but works. Thanks for your contribution.

--
David



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 195
Default VBA to scan range

Bob Phillips wrote

MsgBox Evaluate("=MAX(IF(WEEKDAY(C2:AA2)=6,C2:AA2))")


I don't know what I was thinking. There's really no need to scan all the
dates. Since V2 (4th Friday) will always hold a date, and AA2 (5th Friday)
may or may not, I only need to look at those 2 cells:

LastFri = Application.Max([C2], [AA2])

--
David
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 195
Default VBA to scan range

David wrote

Bob Phillips wrote

MsgBox Evaluate("=MAX(IF(WEEKDAY(C2:AA2)=6,C2:AA2))")


I don't know what I was thinking. There's really no need to scan all
the dates. Since V2 (4th Friday) will always hold a date, and AA2 (5th
Friday) may or may not, I only need to look at those 2 cells:

LastFri = Application.Max([C2], [AA2])


All brackets not needed:
LastFri = Application.Max([V2, AA2])
or standard convention:
LastFri = Application.Max(Range("V2, AA2"))

--
David
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 fax a scan k Excel Discussion (Misc queries) 4 March 26th 09 03:35 PM
Virus scan Lane Setting up and Configuration of Excel 2 February 11th 08 07:44 PM
how do i scan documents T Nickel Setting up and Configuration of Excel 1 September 4th 07 03:58 PM
Scan Forms Sam Z Excel Discussion (Misc queries) 1 January 8th 06 04:35 PM
Scan range of rows and export to 2nd worksheet Adam Excel Programming 1 March 14th 05 06:03 AM


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

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

About Us

"It's about Microsoft Excel"