Consulting

Results 1 to 9 of 9

Thread: Loop Help

  1. #1
    VBAX Regular
    Joined
    Oct 2011
    Posts
    15
    Location

    Loop Help

    Hey guys,
    I am sure this is a very easy question to answer for most of you, but i just can t find the right code.
    [vba]Private Sub CommandButton3_Click()
    Dim StartTime As Date
    Dim EndTime As Date

    For i = 2 To 146 'end of the sheet1 team/name
    For j = 3 To 15 'end of sheet1 dates
    TempData = Worksheets("Sheet1").Cells(1, j) & Worksheets("Sheet1").Cells(i, 2)

    k = 2

    Do While Cells(k, 1) & Cells(k, 4) = TempData
    StartTime = Cells(k, 5)
    EndTime = Cells(k, 9)
    Worksheets("Sheet1").Cells(i, j) = StartTime & "-" & EndTime
    k = k + 1
    Loop UNTIL k= 300

    Next j
    Next i

    End Sub[/vba]
    I need this thing to loops until k = 300,, but obviuosly is not the correct way to express it in VBA, if you could give me a tipp I would appreciate it.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Maybe somethig like ...


    [VBA]
    k = 2

    Do While Cells(k, 1) & Cells(k, 4) = TempData And K <= 300
    StartTime = Cells(k, 5)
    EndTime = Cells(k, 9)
    Worksheets("Sheet1").Cells(i, j) = StartTime & "-" & EndTime
    k = k + 1
    Loop
    [/VBA]

    Paul

  3. #3
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    I believe you mean:
    [vba] Do While Cells(k, 1) & Cells(k, 4) = TempData And k < 300
    StartTime = Cells(k, 5)
    EndTime = Cells(k, 9)
    Worksheets("Sheet1").Cells(i, j) = StartTime & "-" & EndTime
    k = k + 1
    Loop [/vba]
    Be as you wish to seem

  4. #4
    VBAX Regular
    Joined
    Oct 2011
    Posts
    15
    Location
    Well actually is not working the way I wanted to :S

  5. #5
    VBAX Regular
    Joined
    Oct 2011
    Posts
    15
    Location
    I think, the way I wrote this code is not the most efficient way cause it takes like 10 min or even more for my pc to get the whole information which isn't that many. Could I send you my file, so you can look at it and have a better overview of the issue ?

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    I believe you mean:
    Aflatoon -- I believe you're right

    JJanower - A loop within a loop within a loop will take a long time

    Post a sample workbook with some of your data and the code, and I'm sure people will be willing and able to suggest alternatives.

    Paul

  7. #7
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Can you post a book with sample data.
    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'

  8. #8
    VBAX Regular
    Joined
    Oct 2011
    Posts
    15
    Location
    So here is the thing. I would like to transform everything that is on Worksheets("Weekly") onto Worksheets("Sheet1"), so that is looks at the end, the way it looks in Worksheets("MyProgrammingGoal").
    But this is what I ve gotten so far, please feel free to take a look in the attached File, I would appreciate every Help, advices, and comments.

    I ve made comments to every Step that I ve written, Step 5) needs to be changed into a more efficient Code.

    Thank very Much
    Attached Files Attached Files

  9. #9
    VBAX Regular
    Joined
    Oct 2011
    Posts
    15
    Location
    Updated file*
    Attached Files Attached Files

Posting Permissions

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