![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Board Regular
Join Date: Feb 2002
Posts: 217
|
This should be simple for the experts out there. I have a worksheet formula that looks like this
=CHOOSE(weekday(B8)),"M","T","W","TH","F","S","SU") where cell b8 is the day of the month. How do I write a VBA function to substitute for this formula so all i have to reference is the date cell, example "=dayofweek(ref)"? |
|
|
|
|
|
#2 |
|
MrExcel MVP
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
|
Hi,
Try... Code:
Function DayOfWeek(RefCell) Dim fn As Object, wkday As Byte Set fn = Application.WorksheetFunction wkday = fn.WeekDay(RefCell, 2) DayOfWeek = fn.Choose(wkday, "M", "T", "W", "TH", "F", "S", "SU") End Function Jay EDIT: Variables, fn and wkday declared. RefCell kept as variant for flexibility. Change to "RefCell As Range" to force the function to reference a cell range. [ This Message was edited by: Jay Petrulis on 2002-05-24 12:16 ] |
|
|
|
|
|
#3 |
|
Board Regular
Join Date: Feb 2002
Posts: 217
|
Thanks for the answer. I did get the function to work another way. If you have the time could you please explain the difference in your code and mine and and advantage of one over the other. I'm a firm believer in learning to do things the right way (although frequently what I learn is through trial and error), so if there is some potential error in what I did I'd like to know. Here is my code.
Function DOW(ref) As String DOW = Choose(Weekday(ref), "M", "T", "W", "TH", "F", "S", "SU") End Function Thanks for your time |
|
|
|
|
|
#4 | |
|
MrExcel MVP
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
|
Quote:
One thing, though. Do you want this to return the day after the reference cell date? Code:
Function DOW(ref) As String DOW = Choose(WeekDay(ref, vbMonday), "M", "T", "W", "TH", "F", "S", "SU") End Function [ This Message was edited by: Jay Petrulis on 2002-05-24 12:56 ] |
|
|
|
|
|
|
#5 |
|
Board Regular
Join Date: Feb 2002
Posts: 217
|
Thanks for the time and the tip.
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|