# vba macro

#### Bob Clausen

##### New Member
I have a workbook with 31 worksheets.
One worksheet for everyday of the month.
They are named 1,2,3,...29,30,31
I am having trouble writing a macro to do the following.
In Range("k16") is a value on each worksheet.
On the worksheet #2 i want to average the value of k16 of the first and second sheet.
On the worksheet #3 i want to average the value of k16 of the first, second, third worksheet. I hope you follow my logic.
Of course I don't want to average all worksheets until that day has come to the corresponding worksheet.
I want to put the average for day 1 & 2 on the second worksheet in let's say Range("k17").
Getting caught up in opening sheets or correct order.
Thankyou, Bob

#### nightcrawler23

##### Well-known Member
Code:
``````Sub daysAvg()
Dim currDay As Integer
currDay = Day(Date)
For i = 2 To currDay
Sheets("" & i).Select
Sheets("" & i).Range("K17") = (Sheets("" & (i - 1)).Range("K16") * (i - 1) + Sheets("" & i).Range("K16")) / i
Next i
End Sub``````
Try This

#### nightcrawler23

##### Well-known Member
BTW,
Welcome to the Board.

#### Bob Clausen

##### New Member
Thanks so much, I will try it. I didn't try doing sheets("" & i) to call up sheet.
Bob

#### wolverineb

##### Well-known Member
This is also possible using only formulas, although it's ugly.

Basically, you can use the fact that this formula gives you the name of the current worksheet as a number:

=MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1,31)*1

And this one tells you what's in the previous sheet's cell K17:

=INDIRECT(((MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1,31)*1)-1)&"!K17")

Oh, and this gives you the number of the current day:

=DAY(TODAY())

Combine things in the same way nightcrawler did in his code and voila! Easy as pie.

1,082,153
Messages
5,363,459
Members
400,737
Latest member
urhen22

### This Week's Hot Topics

• populate from drop list with multiple tables
Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
• Find list of words from sheet2 in sheet1 before a comma and extract text vba
Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
• Dynamic Formula entry - VBA code sought
Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...