creating automated income statement, want to add dynamic monthly column to AVERAGE and SUM

pawest

Board Regular
Joined
Jun 27, 2011
Messages
105
Hi VBA masters,
I'm creating an automated daily income statement with currency rates, cash balances, expenses, etc. I would like to add a monthly column that's dynamic and uses VBA's ".Formula" tool to add functions and perform calculations on the daily values. I'm having trouble using the .End(xlToRight) technique to make it dynamic. Here are some pieces of what I have so far:

Code:
Dim homecell as object
Dim CCYrange As Object 

Set homeCell = Range("D5")
Set CCYrange = Range(Range("D5"), Range("D5").End(xlToRight))

homecell.Offset(4, 10).Formula = "=AVERAGE(" & CCYrange & ")"
When I run the above code, I get a "Run-time error '13': Type mismatch" ... when I hardcode the range, such as "=AVERAGE(D5:G5)", it works. Any help on this issue would be great!

Thanks!
 

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841
Try:-
Rich (BB code):
homecell.Offset(4, 10).Formula = "=AVERAGE(" & CCYrange.Address & ")"  

 

pawest

Board Regular
Joined
Jun 27, 2011
Messages
105
Hey MickG,
I appreciate the quick response. No more errors! But it semi-works. It only works if there has been data previously entered in a cell in the same row ahead of the beginning cell ("D5" in this case). If data was not previously in a cell ahead of the beginning cell then it locks and selects to the spreadsheet's last column, for example: when the code is run, the monthly average cell equals this "=AVERAGE($D$5:$XFD$5)".

Unfortunately for my income statement spreadsheet, having previously entered data into the spreadsheet will never be the case. Unless you or anyone else can think of a way to use the .address technique, I might have to go with and offset - 1 type of a deal.

Any other recommendations?

Thanks for your help!
 

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841
Assuming you have data in "D5" ths might help !!
Code:
Set CCYrange = Range(Range("D5"), Cells(5, Columns.count).End(xlToLeft))
 

pawest

Board Regular
Joined
Jun 27, 2011
Messages
105
MickG,
Almost got it! Is there any way to do something like :

Set CCYrange = Range(Range("D5"), (Cells(5, Columns.count).End(xlToLeft)-1)) ?

Thanks!
</pre>
 

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841
I'm not quite sure what you are trying to do !!
Do you only want to run the formula bit of the code if there is some data in at least one of the cells from "D5" on, or something else.???
 

pawest

Board Regular
Joined
Jun 27, 2011
Messages
105
I think the problem that I'm running into is that for the first day of the month, it doesn't select the appropriate range. Once there is data in the first day of the month (the first column of data or column D in my case) and data for additional days worth of data, the macro works. However, when using your suggestion of Columns.count, it includes everything up to the up to the last column... in other word it selects the monthly column which is the last column. As you can see, a formula in a cell doesn't work if the selection it references includes the active cell.

I think I might have to layer this with If-Then statements and I'm not particularly advanced with VBA but I might know enough to say If it's the first day of the month then print data, if it's the X + 1 day of the month then include the dynamic selections... if that makes sense. I'd rather keep it more simple than that but I'll keep working at it.
 

pawest

Board Regular
Joined
Jun 27, 2011
Messages
105
MickG,
Thanks for your help. I wouldn't have been to accomplish my project without your .address suggestion! Here's what I ended up doing (I will try to optimize it a bit further since I'm still in the testing stage):

HTML:
Dim CCYcell as Range

Set CCYcell = Range("D5")

        If CCYcell.Offset(0, 1).Value = 0 Then
            CCYcell.Select
        Else
            Range(CCYcell, CCYcell.End(xlToRight)).Select
        End If
        .Offset(4, 7).Formula = "=AVERAGE(" & Selection.Address & ")"
Thanks!
 

Forum statistics

Threads
1,082,250
Messages
5,364,022
Members
400,774
Latest member
Goldi paul

Some videos you may like

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...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top