Consulting

Results 1 to 5 of 5

Thread: VBA entering a nested formula with text

  1. #1

    VBA finding last row of table + 2

    Hi everyone,

    I am trying to find the last row of a table adding 2 rows then pasting a formula into that cell.
    The code i currenlty have to get the desired cell and enter something is:

    Sub Last_row_Add()
    With Range("PerformanceTable")
     LastRow = .Cells(.Rows.count, 1).End(xlUp).Row + 2
     End With
    Worksheets("Performance Update").Range("A" & (LastRow + 2)).Select
    ActiveCell.FormulaR1C1 = _
            "=""This currently represents an overall $""&SUM(PerformanceTable[Profit/Loss])&"" ""&IF(SUM(PerformanceTable[Profit/Loss])<0,""loss"",""increase"")&"" in your portfolio which represents a ""&IF(SUM(PerformanceTable[Profit/Loss])>0,""+"",""-"")&"" ""&ROUND((SUM(PerformanceTable[Profit/Loss]))/SUMPRODUCT((OwnedSH[Share Owner]=Dashboard!R[-10]C[4])*OwnedSH[Total Purcha" & _
            "se Value])*100,2)&""% change from your invested funds. should you wish to make changes to the investmenets please contact your account manager. """
    End Sub
    This works to enter the formula but it does not addapt when the table length changes.
    Please advise how to make the find last row of table + 2 dynamic.
    Last edited by torquil; 04-07-2020 at 03:11 PM. Reason: resolved part of problem

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Assuming no empty rows in table
    LastRow = .Range("A1").End(xlDown).Row + 2
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    Sub Last_row_Add()
    With Range("PerformanceTable")
      'LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row + 2 'I think this is wrong
      LastRow = .Cells(.Rows.Count, 1).Row + 2
    End With
    Worksheets("Performance Update").Range("A" & LastRow).FormulaR1C1 = "=""This currently represents an overall $"" & SUM(PerformanceTable[Profit/Loss]) & "" "" & IF(SUM(PerformanceTable[Profit/Loss])<0,""loss"",""increase"") & "" in your portfolio which represents a "" & IF(SUM(PerformanceTable[Profit/Loss])>0,""+"",""-"") & "" "" & ROUND((SUM(PerformanceTable[Profit/Loss]))/SUMPRODUCT((OwnedSh[Share Owner]=Dashboard!R[" & 19 - LastRow & "]C[4])*Owned" & "Sh[Total Purchase Value])*100,2) & ""% change from your invested funds. should you wish to make changes to the investmenets please contact your account manager. """
    End Sub
    Could be simpler if the reference to Dashboard!E19 was absolute (Dashboard!$E$19) instead of relative.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  4. #4
    Hi SamT,
    doesnt work unfortunatley. BTW the table starts in A25 I tried amending to correct the cell referenece but its not work.

  5. #5
    Dam it, p45cal...... you come to the rescue again!! thank you

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •