Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm new to VBA and need help writing a macro given the following

A.  Enddate = .Cell(lRow, 7).Value.
B.  AnnualDepr = ".Cell(lRow, 11).Value.
C.  PartialYearDepr = "(.Cell(lRow, 11).Value) /12) * (DateDiff("m", EndDate, FY)).
D.  FY or Input Range = range of fiscal year end dates (i.e. 10/31/13 - 10/31/75), Range(Cells(2, 14), Cells(2, lColumn)).
E.  Output range = Range(Cells(lRow, 14), Cells(lRow, lColumn)).

Here's the set-up:

  1. If the "Year(EndDate) > Year(FY)" then copy AnnualDepr to the appropriate cells in Output Range.

  2. ElseIf "Year(EndDate) = Year(FY)" then use PartialYearDepr in the appropriate cell in Output Range.

  3. Else Place 0 in the remaining cells in Output Range.

It's important that "AnnualDepr" and "PartialYearDepr" appear beneath the correct FY, and I can't seem to get that right (i.e. linking the Input Range (i.e. Range(Cells(2, 14), Cells(2, lColumn))) and Output Range (i.e. Range(Cells(lRow, 14), Cells(lRow, lColumn))).

I've tried looping with 'For Each', but once the first argument is satisfied (i.e. Year(EndDate) > Year(FY)), AnnualDepr is copied across the entire Output Range, INSTEAD of only copying up to the cell where Year(EndDate) = Year(FY) occurs.

PartialYearDepr should only appear when Year(EndDate) = Year(FY) occurs.

After both arguments are satisfied (i.e. Year(End Date) > = Year(FY)), 0 should appear in the Output Range till the last column (i.e. lColumn).

Thanks in advance for any help.


'Post Annual Depreciation thru EDate, thereafter 0 Dim lColumn As Long lColumn = ws.Cells(2, Columns.Count).End(xlToLeft).Column

Dim EDate As Date
EDate = .Cells(lRow, 7).Value

Dim rgLife As Range
Dim rgDeprec As Range

Dim i As Long, j As Long
Set rgDeprec = Range(Cells(lRow, 14), Cells(lRow, lColumn))

Set rgLife = Range(Cells(2, 14), Cells(2, lColumn))
For i = 1 To rgLife.Rows.Count
For j = 1 To rgLife.Columns.Count

    If Year(EDate) > Year(rgLife.Cells(i, j).Value) Then
        Cells(lRow, 11).Copy rgDeprec.Cells(i, j)
    ElseIf Year(EDate) = Year(rgLife.Cells(i, j).Value) Then
        rgDeprec.Cells(i, j) = Cells(lRow, 10)_
        - WorksheetFunction.Sum(Range(Cells(lRow, 12), Cells(lRow, lColumn)))
    End If
Next j
Next i
share|improve this question
Can you post the code which is giving you the problem and also if possible a screen shot of your data? – Siddharth Rout Apr 16 at 18:45
My apologies for the "Hail Mary" post, but I've been working on this project for several months now and need to draw it to a conclusion very soon. I know I'm close, I just need a little code to finish it up. How do I attach a workbook? Again, any help is much much appreciated. – user2287254 Apr 16 at 20:53
It's almost my bedtime :P But you can upload it to www.wikisend.com and then share the link here. Alternatively you can also upload it to skydrive and post the link here. I will have a look at it when I get up or in the meantime someone else will jump in :) – Siddharth Rout Apr 16 at 20:55
Thanks for your consideration and suggestion. Find the Wikisend.com link here wikisend.com/download/397636/Depreciation 4-17-2013 ZIP.zip – user2287254 Apr 17 at 20:14
Have you had a chance to review the attachment. I'm very close to a deadline. Thanks again. – user2287254 Apr 22 at 13:10
show 3 more comments

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.