Formula to convert a week number to month - vba

I am trying to implement VBA code for converting the week number to month Name.
I have a sheet in which column "A" contains week number from 1 to 52 . The week number 1 starts from column A3. I want the corresponding month Name for the week number to be printed.
I tried a formula like this
=MONTH(DATE(YEAR(NOW()),1,1)+(B1*7))
The reason I tried an formula fisrt is to get the idea and then converting the formula to VBA.
I am just getting the formula printed in my cell. Can anyone help me with this?

here you go:
dim i as long
dim n as long
n=sheets("Sheet1").cells(Rows.count,1).end(xlup).row
for i = 1 to n
cells(i,3).formula = "=MONTH(DATE(YEAR(NOW()),1,1)+(RC[-1]*7))"
next i

Related

How to display a month in VBA given conditions

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

Determine the column range of merged cells

My goal is to average a number of columns in one row.
The table is laId out by month, ONE week per column and a number of rows with the numbers in each column.
The table is divided into months across the top, with the month name header in a merged cell of four or five columns depending on how many weeks the month spans (Saturdays and Sundays are omitted at the start and end of the month leading to the four or five weeks per month).
For example, November this year (2015) includes five work weeks. The range of the header is O11:S11 - a merged cell.
Above this table that spans the year, there is a cell to average the number of hours spend on project time versus overhead.
In my example for November, I would average O43:S43. I would like to type or select the month to average in one cell, then find the column range of that month from the merged cells holding November.
For example, if the key cell is D9, holding November, and I want to place the average value in E9, and the row of months for the year spans from K11 - W11, what are the possible approaches to solving this question?
I tried using an INDEX MATCH formula but that gets me nowhere. I need to write a VBA function (I think).
You can find the merged cell then resize the range.Remove the rng.Selectline, it's just there to check if it is calculating correctly.
Sub Button1_Click()
Dim Fm As Range, Av As Range, rng As Range
Dim s As String, x
s = Range("D9").Value
Set Fm = Rows(11).Find(s, LookIn:=xlValues)
If Not Fm Is Nothing Then
Set Av = Fm.MergeArea
Set rng = Av.Offset(32).Resize(, Av.Columns.Count)
rng.Select 'remove later
x = Application.WorksheetFunction.Average(rng)
Range("E9").Value = x
End If
End Sub

Excel Formula or VBA to convert year, month data to actual date

I have 2 columns (year and month). A sample data of year column A is 2015 at cell A2. A sample data of month column B is Jun at cell B2. I would like to convert these data to date format in column C to '2015-06-00` at cell C2. Is there a formula or Excel VBA to do this?
First off, 2015-06-00 is actually 31-May-2015. If you supply zero as the day-of-the-month, you get the last day of the previous month. Typically you would set it for the first day of the month. Use the DATE function for this with 1 as the day_num parameter or the DATEVALUE function if you are stringing together text-that-looks-like-a-date.
=datevalue(B2 & " 1, " & A2)
With a three letter month in B2, you will have to translate that to a numerical month if you opt for the DATE function.
=DATE(A2, LOOKUP(B2, {"Apr","Aug","Dec","Feb","Jan","Jul","Jun","Mar","May","Nov","Oct","Sep"}, {4,8,12,2,1,7,6,3,5,11,10,9}), 1)
in cell C "=FECHA(A2;B2;1)" (spanish excell)

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)

How could I use vba to check if dates are the same?

I'm trying to write a code on cells that have both dates and times in them (Example: 1/29/14 9:15 AM). I'm trying to see if the date from 1 cell matches the date on another cell. How could I do that?
Example:
A1: 2/13/14 B1: 2/13/14 - This would return True
A2: 1/15/14 B2: 4/25/14 - This would return false
Any way I could achieve this?
Thanks
Excel stores dates as fractional numbers where the whole number represents the date and the fraction represents the time of day (e.g., 0.5 = noon). The whole number is the number of days since 1/1/1900. So Excel stores 1/3/1900 6:00 PM internally as 3.75.
As long as they are formatted as dates, you can compare them directly:
C1 formula: =(A1=B1)
C2 formula: =(A2=B2)
To ignore the time portion of the date, take the integer portion only:
C1 formula: =(INT(A1)=INT(B1))
C2 formula: =(INT(A2)=INT(B2))
There's no real need for VBA, but if you want to compare the dates in VBA, the same rules apply:
Sub CompareDates()
With ActiveSheet
.[C1] = (Int(.[A1]) = Int(.[B1]))
.[C2] = (Int(.[A2]) = Int(.[B2]))
End With
End Sub