Consulting

Results 1 to 13 of 13

Thread: Help with loop code please

  1. #1

    Help with loop code please

    Hi

    Following code creates the formula in S10 and loops down column S - issue being it doesn't stop at the last used row.

    With Range("S10", Range("S" & Rows.Count).End(xlDown))
            .FormulaR1C1 = "=RC[-4]*RC[-11]"
            .NumberFormat = "#,##0.00"
        End With
    Can be achieved with a Do Loop & offset but its very slow

    Range("S10").Select
    Do
        ActiveCell.FormulaR1C1 = "=RC[-4]*RC[-11]"
        
        ActiveCell.Offset(1, 0).Select
        
        Loop Until ActiveCell.Offset(0, -3).Value = ""
    Missing something on the first code

    Any help appreciated

    thanks
    Jon

  2. #2
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    Hello Jon,

    Try this...
    With Range("S10", Cells(rows.Count, "S").End(xlUp))
        .FormulaR1C1 = "=RC[-4]*RC[-11]" 
        .NumberFormat = "#,##0.00" 
    End With
    Note if S10 is empty then the first cell could be from S1 to S9.

    To be sure your first cell is S10 use this...
    Dim RngBeg As Range
    Dim RngEnd As Range
    
    Set RngBeg = Range("S10")
    Set RngEnd = Cells(Rows.Count, "S").End(xlUp)
    
    If RngEnd.Row < RngBeg.Row Then Set RngEnd = RngBeg
    
    With Range(RngBEg, RngEnd)
    .FormulaR1C1 = "=RC[-4]*RC[-11]"
    .NumberFormat = "#,##0.00
    
    End With
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

  3. #3
    thanks for trying but doesn't work

  4. #4
    thanks for trying but only populates S10

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    Quote Originally Posted by blackie42 View Post
    thanks for trying but only populates S10
    Attach a workbook with the before and after to make it easier to see what you want to do
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    Test file attached

    thanks
    Attached Files Attached Files

  7. #7
    sorry - correct test file - copies formula up
    Attached Files Attached Files

  8. #8
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Just offset from a known range
    With Range(Cells(10, 8), Cells(10, 8).End(xlDown)).Offset(, 11)
        .FormulaR1C1 = "=RC[-4]*RC[-11]"
        .NumberFormat = "#,##0.00"
    End With
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    Sub test()
    Lastrow = Range("H" & Rows.Count).End(xlUp).Row
    Range("S10:S" & Lastrow).Formula = "=RC[-4]*RC[-11]"
    End Sub

  10. #10
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Sub Blakie42_FillFormulas()
    Dim LR As Long
    With Sheets("Sheet1")
      LR = .Range("H10").End(xlDown).Row
      With Range(.Cells(10, "S"), .Cells(LR, "S"))
          .FormulaR1C1 = "=RC[-4]*RC[-11]"
          .NumberFormat = "#,##0.00"
      End With
    End With
    End Sub
    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

  11. #11
    Thanks for everones time.

    regards
    Jon

  12. #12
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    We couldn't solve your problem, hunh?
    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

  13. #13
    Hi Sam,

    I solved it myself in my last post - which was similar to your reply.

    I was just thanking everyone who had posted replies

    Just being polite

    regards
    Jon

Posting Permissions

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