Currently I am trying to display a month given certain conditions in VBA. I have two columns named Quarter and Exact Month in Quarter. Based upon these two conditions, I would like to display a month.
For example, For Quarter 1 Month 1, I would like the new column to display January.
Thank you for your help.
You can just offset each quarter by a value of 3. For instance, Q2 will correspond with 3. When you add your quarter serial #, it will be converted into a calendar serial # (1-12). After that, you just need to set the desired format.
This will just be an excel solution, and if this needs to be in VBA, you can apply similar logic there.
You can do this a few ways. The below image just serves as an example. How to apply this will depend on your data structure. You may need to implement a VLOOKUP as such.
G2 = TEXT(Date(2018, E2 + VLOOKUP(D2,$A$2:$B:$5,2,0),"MMM")
[1] Only one Excel Formula in Column C - doesn't need any helper columns:
This example e.g. for cell C2 as shown in your post converts quarters * 3 months (1 quarter = 3 months) minus 1 plus month in quarter to DATE (argument year can be any year >= 1900) and displays the month's Long form via TEXT argument MMMM; quarter values are extracted from the last string position in column A ( here A2) via RIGHT:
=TEXT(DATE(1900,3*(RIGHT($A2,1)-1)+$B2,1),"MMMM")
[2] Approach via VBA Example Function
Uses the same logic as above:
Sub Test()
MsgBox getMonthInQuarter("Quarter 4", 3)
End Sub
Function getMonthInQuarter(ByVal quarter As Variant, ByVal month As Integer) As String
Const MonthsInQuarter As Integer = 3
Dim q As Integer: q = Val(Right(quarter, 1))
' Return function value
getMonthInQuarter = Format(DateSerial(1900, MonthsInQuarter * (q - 1) + month, 1), "MMMM")
End Function
Related
For example, the inputted date is 2001/01/21 the output must be 21 because 21 days passed since the inputted date's first day of the year.
Transform the date to a value and do the same for the "new" date? Do the same in Dev.mode newCelldate.Value - yearStartdate.Value?
Copy and paste the following to your VB Editor. Then, run the "TestNumberOfDays" procedure.
Function FirstDayOfTheGivenYear(pDate As Date)
'this will return first day of the given year
FirstDayOfTheGivenYear = 1 * (CDate(pDate) - DatePart("y", CDate(pDate) - 1))
End Function
Function YTDDays(pDate As Date) As Long
'this will find the day differences between two dates
YTDDays = DateDiff("d", FirstDayOfTheGivenYear(pDate), pDate) + 1
End Function
Sub TestNumberOfDays()
'change the date according to your need
MsgBox (YTDDays("2001/01/30"))
End Sub
Like this:
InputDate = #2001/01/21#
OrdinalDay = DatePart("y", InputDate)
Having some trouble locating a function to determine the week number of a quarter period in a standard date based (i.e. Day 1 = 2017-01-01) calendar table.
My table has the following information stored :
Quarter Number, Beginning Date, End Date, Number of Days;
As an example, for the first quarter of calendar year, the result for Week 1 would be 1, Week 14 would be 1, going through each quarter until the final week of quarter 4.
Any ideas please help?
I don't believe there is a built-in function for this. If I understand what you're asking, this is a trivial function to write. You don't include any code so I'm just giving you the most basic example. You want to do something similar to this.
Public Function GetWeekInQuarter(ByVal WeekNumber As Integer) As Integer
Const intWeeksInQuarter = 13
Dim intResult As Integer
intResult = WeekNumber Mod intWeeksInQuarter
'if intResult <> intWeeksInQuarter = 0 then this is the 13th week
GetWeekInQuarter = IIf(intResult <> intWeeksInQuarter, intResult, intWeeksInQuarter)
End Function
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)
I want to set a specific date (October 06 , 2014) to be the 1st Week of the year. So when I use the =weeknum() formulaR1C1 it will return the week number.
I'm currently working on a worksheet that gets updated daily so the week number will be updated every week only. In column ("D") indicates the week number. and column ("B") indicates the daily date.
If i use the =weeknum() formula on it returns the value of 41, but it needs to be week number 1.
How about creating your personalized function?
It just consists of counting how many days there are between the date you insert (myDate) and the date which is considered to be the first day of the year (first_day), dividing this number by 7, taking its integer and adding up 1. It will always give you the right one.
Public Function special_weeknum(ByVal myDate As Date) As Integer
Dim first_day As Date: first_day = #10/6/2014#
Dim myWeek As Integer: myWeek = Int((myDate - first_day) / 7) + 1
special_weeknum = myWeek
End Function
Please note that this approach would allow you also to pass the first day as a user input:
Public Function special_weeknum(ByVal myDate As Date, ByVal first_day As Date)
Dim myWeek As Integer: myWeek = Int((myDate - first_day) / 7) + 1
special_weeknum = myWeek
End Function
so that you will always be able to decide which is the day you must consider as first day of the year.
I have two comboboxes that lets the user select a date and year. What I want to do is to translate this into my query in access, which requires a [start date] and an [end date] parameter.
I.E. user picks "May" and "2013" in the combo boxes
My query is setup between [start date] and [end date], so I want to translate this month and year selection from the combo boxes into two strings (startdate and enddate, then pass them as command parameters) that contain MM/DD/YYYY, MM/DD/YYYY. What is the best way to take two strings and get the first valid day and last valid day. I have:
FirstDayInMonth = DateSerial( _
Year(Date), Month(Date), 1)
LastDayInMonth = DateSerial( _
Year(dtmDate), Month(dtmDate) + 1, 0)
But I need to make the switch from a string into a date format to get back the first/last day of the selected month, using only the month ("MAY") and year ("2013")? Am I missing something relatively simple?
Make the "Month" combo box have two columns (Column Count property is 2):
1 | Jan
2 | Feb
...
12 | Dec
Set the Bound Column of the combo box to 1 so its .Value is the month number, but display only the month name (hide the number) by setting the width of the first column to zero:
Column Widths: 0";0.75"
So, do you get the month and year (as integer or long, not a date yet) from the comboboxes?
Then try this:
Dim m As Long
Dim y As Long
Dim d_from As Date
Dim d_until As Date
m = CLng("01") ' get these two from your comboboxes
y = CLng("2013")
d_from = DateSerial(y, m, 1)
d_until = DateAdd("m", 1, d_from)
d_until = DateAdd("d", -1, d_until)
When you have the first date, you calculate the second (last day in month) by adding one month, then going one day back.