substitute within formula, not string

lemonbarley

New Member
Joined
Jun 1, 2021
Messages
6
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have several months' of data in a row, e.g.
Col ACol BCol C...Col UCol VCol W
1 Feb 20222 Feb 2022...28 Feb 20221 Mar 20222 Mar 2022...
Row 15Type A99849182...9374957110012...
Row 16Type B2401723981...242312452324917...

I refresh this data every month with current + future months, so next month, Column B will be 1 Mar 2022.

I need to get the average value of each month. I'm currently using =average() and manually moving the range of cells every month for Type A, I would do this 3x for 3 months. Then I copy the formula to Type B and Ctrl+H Replace the row number from 15 to 16.

Col ACol BCol C
Type AAvg ValueFormula
Feb229941=AVERAGE($B$15:$U$15)
Mar229912=AVERAGE($V$15:$AR$15)
Apr229901=AVERAGE($AS$15:$BM$15)
Type BAvg ValueFormula
Feb2224178=AVERAGE($B$16:$U$16)
Mar2224578=AVERAGE($V$16:$AR$16)
Apr2224440=AVERAGE($AS$16:$BM$16)

Specific question: Is there a formula (like Substitute) that can do the Ctrl+H on the formula itself? So Type B values will automatically replace 15 to 16 without me having to Ctrl+H it?

Open question: without using Pivot / Power tools / VBA (i.e. only formula), do you have suggestions on how to get the range automatically after data refresh (which is manual copy and paste) without having to manually move the range? Col A are date format so they can be used as date references.

Thanks!
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
With date running accross row 14, from B14 to, i.e, ZZ14 (wide enough)
A19 downward is first day of month, formatted as "mmmyy"
Cell Formulas
RangeFormula
I14:BO16,C14:H14C14=B14+1
H15:H16H15=G15
B24:B26,B19:B21B19=SUMPRODUCT(($A$15:$A$16=INDEX($A$18:$A19,AGGREGATE(14,6,MATCH($A$15:$A$16,$A$18:$A19,0),1)))*($B$14:$ZZ$14>=$A19)*($B$14:$ZZ$14<=EOMONTH($A19,0))*$B$15:$ZZ$16)/DAY(EOMONTH($A19,0))
 
Upvote 0
average works with the number of filled cells, suppose you have only data in the week, not in the weekends, then you make a mistake with the formula above.
 
Upvote 0
with a named range "My_dates" and now i see that there aren't enough columns for 3 entire months
Cell Formulas
RangeFormula
C14:BO14,I15:BO16C14=B14+1
H15:H16H15=G15
A19,A24A19=+$B$14
B19:B21B19=IFERROR(SUMPRODUCT(--(MONTH(My_dates)=MONTH(A19))*OFFSET(My_dates,1,,,))/SUMPRODUCT(--(MONTH(My_dates)=MONTH(A19))*(OFFSET(My_dates,1,,,)<>"")),"-")
A20:A21,A25:A26A20=EDATE(A19,1)
B24:B26B24=IFERROR(SUMPRODUCT(--(MONTH(My_dates)=MONTH(A24))*OFFSET(My_dates,2,,,))/SUMPRODUCT(--(MONTH(My_dates)=MONTH(A24))*(OFFSET(My_dates,2,,,)<>"")),"-")
Named Ranges
NameRefers ToCells
My_dates=Blad4!$B$14:$BO$14B19:B21, B24:B26, C14, A24, A19
 
Upvote 0
Another option
+Fluff 1.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAWAXAYAZBABBBCBDBEBFBGBHBIBJBKBLBMBNBO
1401/02/202202/02/202203/02/202204/02/202205/02/202206/02/202207/02/202208/02/202209/02/202210/02/202211/02/202212/02/202213/02/202214/02/202215/02/202216/02/202217/02/202218/02/202219/02/202220/02/202221/02/202222/02/202223/02/202224/02/202225/02/202226/02/202227/02/202228/02/202201/03/202202/03/202203/03/202204/03/202205/03/202206/03/202207/03/202208/03/202209/03/202210/03/202211/03/202212/03/202213/03/202214/03/202215/03/202216/03/202217/03/202218/03/202219/03/202220/03/202221/03/202222/03/202223/03/202224/03/202225/03/202226/03/202227/03/202228/03/202229/03/202230/03/202231/03/202201/04/202202/04/202203/04/202204/04/202205/04/202206/04/202207/04/2022
15Type A9984918295559374957110012100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071
16Type B240172398125001242312452324917249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976
17
18Type AAVG
1901/02/20229934.75
2001/03/202210049
2101/04/202210068
Data
Cell Formulas
RangeFormula
C14:BO14,I15:BO16C14=B14+1
H15:H16H15=G15
B19:B21B19=AVERAGEIFS($B$15:$BO$15,$B$14:$BO$14,">="&A19,$B$14:$BO$14,"<"&EDATE(A19,1))
 
Upvote 0
Solution
Data in Sheet1
In Sheet2
In the cell( Say A4) "Feb22" replace By date 1/2/22(1 feb 22) format as MmmYY, if required.

For result
Row2ColACol BCol C
Row3Type AType B
Row4
01-02-2022​
=AVERAGEIFS(OFFSET(Sheet1!$B15:$AZ15,COLUMNS($B4:B4),0),Sheet1!$B$14:$AZ$14,"<="&EOMONTH($A4,0),Sheet1!$B$14:$AZ$14,">="&$A4)
Row5
01-03-2022​

Copy Formula in B4 across and down .
 
Upvote 0
Thank you everyone for your inputs! Sorry I came to this only one month later when I needed it ?

I picked @Fluff 's solution because it is so simple and would have given me what I needed - except that I realised my data is in a table* and the headers are not recognised as dates when I paste them in, so the >= and <edate() did not work. I have an extra row so I put month() in there, which does work (???), then averageifs using the month() row.

=AVERAGEIFS($B$15:$BO$15,$B$14:$BO$14,">="&A19,$B$14:$BO$14,"<"&EDATE(A19,1))
became
=AVERAGEIFS($B$15:$BO$15,$B$13:$BO$13,month(A19))
where Row 13 is =month(row 14)

Then I saw that @BSALV 's solution uses a named range which I can use to integrate the month() into the formula, so I need not use that extra row. Btw, astute observation that there could be weekends without data and indeed I don't have values on non-business days. However, instead of "", those days have "N/A". While "N/A" are automatically excluded in =average which is why I could use it in the first place, it throws up an error when I put it in your formula.

=IFERROR(SUMPRODUCT(--(MONTH(My_dates)=MONTH(A19))*OFFSET(My_dates,1,,,))/SUMPRODUCT(--(MONTH(My_dates)=MONTH(A19))*(OFFSET(My_dates,1,,,)<>"")),"-")
Even when I changed the <>"" to <>"N/A", I get #VALUE! when I take out iferror

*I used a table because I use power tools to get the averages to check my numbers. I still needed the formula because of data presentation and I want to be able to pass this on to new users who may not know how to troubleshoot if the pivots go haywire.
 
Upvote 0
still another "simple" solution
there can be errors in the range of the Types
Here below in the named range, TypeA and TypeB aren't mentioned, but they are offset(mydates,1,,,) and offset(mydates,2,,,)
An empty cell isn't used in that average
Cell Formulas
RangeFormula
C14:BO14C14=+B14+1
H15:H16,AD16:BH16H15=NA()
I15:BO15,I16:AC16,BI16:BO16I15=H28+1
A19,A24A19=+$B$14
B19:B21B19=AVERAGE(IF(ISNUMBER(TypeA),IF(MyDates-DAY(MyDates)+1=A19,TypeA)))
A20:A21,A25:A26A20=EDATE(A19,1)
B24,B26B24=AVERAGE(IF(ISNUMBER(TypeB),IF(MyDates-DAY(MyDates)+1=A24,TypeB)))
B25B25=IFERROR(AVERAGE(IF(ISNUMBER(TypeB),IF(MyDates-DAY(MyDates)+1=A25,TypeB))),"no data")
Named Ranges
NameRefers ToCells
MyDates=Blad2!$B$14:$BO$14B19:B21, B24:B26, C14, A24, A19
 
Last edited:
Upvote 0
an improvement for A19 to be sure that is the 1st day of that month
Map1
AB
18Type AAVG
19feb222136,96296
20mrt221
21apr221
Blad2
Cell Formulas
RangeFormula
A19A19=+$B$14-DAY(B14)+1
B19:B21B19=AVERAGE(IF(ISNUMBER(TypeA),IF(MyDates-DAY(MyDates)+1=A19,TypeA)))
A20:A21A20=EDATE(A19,1)
Named Ranges
NameRefers ToCells
MyDates=Blad2!$B$14:$BO$14B19:B21, A19
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,148
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top