Most efficient and easiest way in Excel VBA to classify date as week - vba

I have one column that should either say Week 1, Week 2, Week 3, or Week 4.
I want to use VBA to enter the week based on what date is being looked at in a textfile...
For Example:
12/19/2013
12/01/2013
12/08/2013
12/26/2013
The output in Excel should be:
Week 3
Week 1
Week 2
Week 4
I am thinking of 4 if statements after using Split on the "/" and then converting the string to a number so it can be compared by an if statement but if I have days like "01" or "03", days starting with 0, wouldn't that cause problems in the if statements comparison?
Is there an easy way to do this in VBA?

As follow up from comments this one works:
Dim myDate as Date
myDate = DateValue("12/01/2013")
MsgBox "Week " & (1 + DateDiff("ww", DateSerial(Year(myDate), Month(myDate), 1), myDate))

There is a built in formula function for this.
http://office.microsoft.com/en-us/excel-help/weeknum-HP005209337.aspx
=weeknum(A1)

Related

VBA convert week and day to date

I have a table with week number and day of week.
Example:
J2 = 18
G2 = Monday
How can I convert this to 2018-04-30?
I can find lots of threads for converting the other way around, meaning date to week. But I can't find anything on week + weekday to date.
Anyone know of a method?
The (optional) second argument of the WEEKDAY function determines what the first day of the week is. The two most common choices are:
1: Sunday (1) to Saturday (7)
or
2: Monday (1) to Sunday (7)
But, you can start on Wednesday, Friday, etc if you want. Pick an option now.
So, start with 1st January (of whichever unspecified year you're working with), and subtract the weekday of 1st January, according to whichever start-day you picked. This gives you a baseline for Week 1. To get to the start of Week n, you just need to add 7*(n-1) days.
Finally, you need to add the weekday back on. I'm going to recommend using MATCH on an array. Your array will be your weekdays, in order, surrounded by curly brackets. So, for Sunday-to-Saturday, you would use
MATCH(G2,{"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"},0)
to get a number from 1 ("Sunday") to 7 ("Saturday"), or #NA if cell G2 does not contain a day-name.
Stick it all together, and you get something like this:
=DATE(YEAR(NOW()),1,1) - WEEKDAY(DATE(YEAR(NOW()),1,1),1) + 7*(J2-1) + MATCH(G2,{"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"},0)
Try this, necessary comments in code:
Option Explicit
Sub GetDate()
Dim year As Date, day As Long
'convert day in G2 to integer
Select Case Range("G2").Value
Case "Monday"
day = 1
Case "Tuesday"
day = 2
'rest of cases
End Select
'declare what year you want
year = "01-01-2018"
'move from first week to the week specified in J2
year = DateAdd("d", (Range("J2").Value - 1) * 7, year)
'move back to first day of the week
Do While DatePart("w", year, vbMonday) <> 1
year = DateAdd("w", -1, year)
Loop
'find date of particular day
Do While DatePart("w", year, vbMonday) <> day
year = DateAdd("w", 1, year)
Loop
End Sub
The determination depends critically on your definition of "week number".
If you are using the ISO-8601 definition, where a Week starts on Monday and week 1 contains the first Thursday of the year, you can use this worksheet formula:
=DATE(YEAR(TODAY()),1,-2)-WEEKDAY(DATE(YEAR(TODAY()),1,7),14)+$J$2*7+MATCH($G$2&"*",{"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday";"Sunday"},0)-1
you may need to change the commas to semicolons depending on your regional settings
To explain:
First, find the last Monday of the previous year:
DATE(YEAR(TODAY()),1,-2)-WEEKDAY(DATE(YEAR(TODAY()),1,7),14)
Then add seven(7) times the number of desired weeks:
+$J$2*7
Then add the number of days from Monday to the desired day for that week:
+MATCH($G$2&"*",{"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday";"Sunday"},0)-1

SSRS Expressions For end of week date

I need to use an SSRS expression to find two dates -
The date that's the end of this week.
The dates that's the start of the week, 2 weeks ago.
For example, if today is 27/05/2016
1 = 29/05/2016
2 = 09/05/2016
This needs to be dynamic e.g. getdate()
I'm using these functions to filter a matrix.
Thanks.
It may be something like below,
1. The date that's the end of this week.
=DateAdd("d", 7 - DatePart("w", CDate(Today)), CDate(Today).AddDays(1)).ToString("dd/MM/yyyy")
2. The dates that's the start of the week, 2 weeks ago.
=DateAdd("d", 2 - WeekDay(Today), DateAdd("d", -14, Today).ToString("dd/MM/yyyy")
SSRS Expression Cheat Sheet
May be something like this,
End of this week
=DateAdd("d", 8 - Weekday(Today), Today).ToString("dd/MM/yyyy")
Start of the week (2 weeks ago)
=DateAdd("d", -(Weekday(Today)+12) , Today).ToString("dd/MM/yyyy")

Input month as an integer, output the final day of that month

I am trying to write an Excel VBA function that outputs the final day in a month in the format yyyy-mm-dd when given only the month number as an integer.
So for January, a 1 would be input and the output would be 2014-01-31.
I know how to reformat the date using Format(date(), "yyyy-mm-dd") and think I can calculate the last day using DateSerial but am not sure how to input an integer month number into the DateSerial function.
What is the best way to go about this?
Try:
Function: =TEXT(DATE(2014, A1 + 1, 1)-1,"yyyy-mm-dd")
VBA: DateSerial(Year, Month + 1, 1) - 1
This function finds the first day of the next month and then subtracts one day.
Paste into any cell except A1 and input your month in A1 to test.
As a formula, with your formatting, assuming the year is 2014 and your month number is in A1:
=EOMONTH(41639,A1)

Select dates within the last week

I want to perform an operation in crystal report.
I have a db table contains a date column.
I want to filter and get the rows having data created in last week(last sunday to last saturday = 7 days).For example if today is 24th August Wednesday, then I need data from 14th August(Sunday) to 20th August(Saturday).
Basically I want to find 2 dates and filter the date column.
Date1 = Date(CurrentDate)-Day(7 + WeekDayinNum(CurrentDate)) ; (Ex:for my example it will be 10)
Date2 = Date(CurrentDate)-Day(WeekDayinNum(CurrentDate))
I do not know the Date APIs properly,can anybody help me in this.
This is a common enough date range that CR provides it for you. In your record selection formula, you can just add {table.date} in LastFullWeek
From CR, "LastFullWeek specifies a range of Date values that includes all dates from Sunday to Saturday of the previous week."
Add this to the record selection formula:
{table.date_field} IN Last7Days
If today is Sunday(1) you want rows that are between 7 and 1 days old,
If today is Monday(2) you want rows that are between 8 and 2 days old,
If today is Tuesday(3) you want rows that are between 9 and 3 days old,
etc.
SELECT *
FROM `tablename`
WHERE `somedatefield` >= DATE_SUB(NOW(),INTERVAL (DAYOFWEEK(NOW()) + 6) DAY)
AND `somedatefield` <= DATE_SUB(NOW(),INTERVAL (DAYOFWEEK(NOW())) DAY)

Calculating Months

I have an application where the user selects the dates of a first statement and a last statement. Example, first statement = 1/1/08, last statement = 12/1/08, should equal 12 statements.
However, when using the following code, the result is 11:
numPayments = DateDiff(DateInterval.Month, CDate(.FeeStartDate), CDate(.FeeEndDate))
Is there another way to calculate this, or do I have to be stuck with adding 1 to the result?
Add 1, as you write. ;)
The difference between 1/1/2008 and 12/1/2008 is 11 months. No changing that. ;)
Yes, you'd always have to add one though you may be able to add one to the end date or subtract one from the start date to also get this effect. Consider the case where the start and end dates are the same. Their difference is 0 but you'd still want 1 statement to show just to note one odd case.
Well, the number of months between Jan 1st and Dec 1st is 11... what you're looking for is the difference of months +1. So just add one :)
Also, the DateDiff function you're using is a VB6 hold-over. Better to express it like this:
numPayments = (Date.Parse(.FeeEndDate) - Date.Parse(.FeeStartDate)).TotalMonths + 1
You could try this one. Hope this is very helpful.
Dim myDate As Date
Dim dateNow As Date
Dim nextMonth As Date
myDate = Now
dateNow = Format(myDate, "MM/dd/yyyy")
nextMonth = DateAdd(DateInterval.Month, 5, dateNow) 'compute the next 5 months from date now. Let say, #12/6/2012# the result will be #5/6/2013#
MessageBox.Show(DateDiff(DateInterval.Month, dateNow, nextMonth) & "months==> " & nextMonth)
'This will count the number of months interval. The result will be 5 months=>> #5/6/2013 because we count december to may.