Consulting

Results 1 to 7 of 7

Thread: Need help creating a loop please

  1. #1

    Need help creating a loop please

    Hello,

    I have a macro which steps through a process of Selecting the Dashboard, entering week number 1, going to another worksheet, copying a cell result and pasting it on another worksheet.
    Then back to the Dashboard and enter week 2 and repeat for 53 weeks. Although it works fine it is clumsy and seems tailor made for a loop but it is beyond my skills
    Any help would be much appreciated.
    I've pasted the first week's step below:

    Sub GetHours()
    
    
        Sheets("Dashboard").Select
        Range("R8").Select '(Always cell R8)
        ActiveCell.FormulaR1C1 = "1" '(Loop 1 - 53 (Week Numbers))
        Range("R8").Select '(Back to cell R8)
        Sheets("The Plan (Last Cut)").Select
        Range("AG745").Select '(Always cell AG745)
        Selection.Copy
        Sheets("Headcount").Select
        Range("J6").Select '(Initially paste to cell J6)
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Range("J7").Select '(Subsequently paste to cell J7, J8, J9 etc.)
        
    End Sub
    Thanks

    Vince
    Last edited by mdmackillop; 03-30-2017 at 01:25 PM. Reason: Code tags added

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Vince
    Can you post a workbook showing expeted result after say 3 weeks.
    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'

  3. #3
    VBAX Expert
    Joined
    May 2016
    Posts
    604
    Location
    Try this:

    [VBA]Sub loopsub()
    Dim inarr As Variant

    Worksheets("Headcount").Select
    For i = 1 To 53
    Worksheets("Dashboard").Range("R8").Value = i
    inarr = Worksheets("The Plan (Last Cut)").Range("AG745")
    Worksheets("Headcount").Range(Cells(i + 5, 10), Cells(i + 5, 10)) = inarr
    Next i

    End Sub
    [/VBA]
    Last edited by mdmackillop; 03-31-2017 at 03:20 AM. Reason: Typo in sheet name corrected

  4. #4

    Loop Help

    Quote Originally Posted by mdmackillop View Post
    Hi Vince
    Can you post a workbook showing expeted result after say 3 weeks.
    Hi,

    I've attached the 3 relevant sheets as the model exceeds the file size limit.

    Dashboard! Cell R8 is the week number needing to count from 1 - 53
    The Plan (Last Cut)! Cell AG745 is the result to be copied to
    Headcount! Cell J6 for week 1, Cell J7 for week 2 etc. Rinse and repeat.

    Thanks.

    Vince
    Attached Files Attached Files

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    offthelip's code seems to do what you are after.
    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'

  6. #6

    Loop Help

    Hi,

    I tried it earlier today and it hung, I didn't notice the typo.
    Thank you for the correction it now works fine.

    Top site,

    Many thanks
    Vince

  7. #7
    Many thanks offthelip this works a treat.

    Regards

    Vince

Posting Permissions

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