Determine the column range of merged cells - vba

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

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

Formula to convert a week number to month

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

VBA - Selecting cells that contain dates for the past 5 years

I currently have a spreadsheet that has the dates (excluding weekends and holidays) for the past 10 years in column A. For example, A1 contains "7/19/2007" and A2520 contains "7/19/2017"
Column E contains the closing price for the stock SPY on those corresponding dates.
I am trying to figure out the standard deviation for the past 5 years. In order to do so, my idea was to write a VBA code that would select today's date and the previous five years, and then use that to calculate the standard deviation.
This list is updated everyday, meaning tomorrow, it will contain 7/20/2017 and the closing price for that day. My issue is that I cannot figure out how to make it so it will select today's date and the past five years, so then I can calculate the standard deviation.
Thank you guys for all your help! Sorry if this seems simple, I have just started learning VBA last week!
How's this? I make a few assumptions, like your dates are contiguous, and there's no empty cell in your Date column. I also assume your dates are in order, ascending. (So your day 10 years ago is in say row 10, and today is in row 1000).
Sub get_difference()
Dim dateRng As Range, cel As Range, priceRng As Range
Dim dateCol As Long, stockCol As Long, lastDate As Range
Dim tdyDate As Date, decadeAgo As Date
dateCol = 1 ' column A has your dates
stockCol = 5
tdyDate = WorksheetFunction.Text(Now(), "mm/dd/yyyy")
decadeAgo = get_Previous_Date(tdyDate)
Debug.Print decadeAgo
With Sheets("Stock Prices") ' change name as necessary
With .Columns(dateCol)
Set lastDate = .Find(what:=tdyDate) ' Assuming no break in data from A1
'lastDate.Select
Set cel = .Find(what:=decadeAgo)
'cel.Select
End With
Set rng = .Range(.Cells(cel.Row, dateCol), .Cells(lastDate.Row, dateCol))
'rng.Select
Set priceRng = rng.Offset(0, stockCol - dateCol)
'priceRng.Select
'priceRng.Offset(0, 1).Select
priceRng.Offset(0, 1).FormulaR1C1 = "=IFERROR((RC[-1]/R[-1]C[-1])-1,"""")"
End With
End Sub
Function get_Previous_Date(Dt As Date) As Date
' https://www.mrexcel.com/forum/excel-questions/37667-how-subtract-year-date-2.html
Dim numYearsBefore as Long, numDaysBefore as Long, numMonthsBefore as Long
numYearsBefore = 10 ' Change this to any amount of years
numDaysBefore = 0
numMonthsBefore = 0
get_Previous_Date = DateSerial(Year(Dt) - numYearsBefore, Month(Dt) - numMonthsBefore, Day(Dt) - numDaysBefore)
End Function
Make changes as needed, i.e. sheet name (I called mine "Stock Prices"), and the columns. It's also a little verbose, and could be made more compact, but I figured it'd help you learn to keep it like that. I suggest stepping through with F8 to see what happens, and uncommenting the .select lines so you can visually see what it's doing.

Macro for sorting and Sumif

In a worksheet, I have 5 columns, 1st column contain the file name. I have to split the file name in team,branch,location and week wise in preceding columns. after that have to apply filter and first filter on week from Oldest to newest then on location A to Z. I have 3 weeks data in each report. At the end of the last row, I want to add 3 more row of respective weeks and sum the respective weeks data.i.e eq. for 22-May-17, the next column should add only the data of the row of the same date.
table image

Excel sub to fill out a table averaging data of another sheet

I am having issues to get data from several time stamps into usable data.
I am trying to fill out a table which the first of every column is a month (January --> December) and the first of every row is an hour of day (00 --> 23).
The table is to be filled out by averaging data from another sheet which consists of a timestamp (i.e. 2011-01-01 0:00) and an amount (i.e. XXXX.XX).
My current logic is this:
Get Cell Number (i.e. B2)
Get Hour and Month allocated to cell (i.e. 00 and January)
Loop through data and Average data of said month and hour from other sheet excluding weekend days
Can anyone help me figure out the coding for this?
Need sample data and also what you have tried so far. You can achive the same using Countifs and sumifs.
So a given cells will be sumifs/countifs to get the average time for the given hour and for the given month.
To know more you can use excel help for the given formulas.