+ Reply to Thread
Results 1 to 8 of 8

Problem with loop

Hybrid View

Dedaluss Problem with loop 02-23-2014, 04:18 PM
TMS Re: Problem with loop 02-23-2014, 05:25 PM
watersev Re: Problem with loop 02-23-2014, 06:34 PM
Dedaluss Re: Problem with loop 02-24-2014, 01:31 AM
watersev Re: Problem with loop 02-24-2014, 10:30 AM
Dedaluss Re: Problem with loop 02-24-2014, 12:27 PM
watersev Re: Problem with loop 02-24-2014, 05:48 PM
Dedaluss Re: Problem with loop 02-25-2014, 12:17 AM
  1. #1
    Registered User
    Join Date
    02-23-2014
    Location
    Hjørring, Denmark
    MS-Off Ver
    Excel 2010
    Posts
    16

    Problem with loop

    I am new to VBA in Excel and need help in looping. I have different data from a sheet "PakkeIndtag" that I need to copy to different cells in other sheet "Data". My loop works almost perfectly. One thing I can't find out is how to jump over row 46 which is in the end of mu loop.If you look at the uploaded picture 1, you can see that I need to come over row 46 then it repeats all over again. The next yellow row will come at row 89 and so on, and so on, every 43 rows. Everything in-between yellow rows is the same as shown on the image.

    My code is as follows
    Sub Analyse_data()
    Dim i, MaskinTid As Single
    Dim Aar, Uge, Dag, MaskinStop, StopType, Station As String
    i = 4
    n = 5
    j = 2
    Application.ScreenUpdating = False
    
    Sheets("PakkeIndtag").Select
    Station = Cells(1, 1).Value
    Do Until Cells(i, 1).Value = ""
        Do Until Cells(n, 1).Value = ""
            Dag = Cells(i, 1).Value
            Uge = Cells(i, 2).Value
            Aar = Cells(i, 6).Value ' Skal justeres til
            MaskinStop = Cells(n, 1).Value
            StopType = Cells(n, 3).Value
            MaskinTid = Cells(n, 4).Value
            Sheets("Data").Select
            Cells(j, 1).Select
            Cells(j, 1).Value = Station
            Cells(j, 2).Value = MaskinStop
            Cells(j, 3).Value = StopType
            Cells(j, 4).Value = MaskinTid
            Cells(j, 5).Value = Dag
            Cells(j, 6).Value = Uge
            Cells(j, 7).Value = Aar
            Sheets("PakkeIndtag").Select
            j = j + 1
            n = n + 1
        Loop
        i = i + 6
        n = i + 1
    Loop
    Application.ScreenUpdating = True
    End Sub
    I hope somebody can help me with this problem
    Attached Images Attached Images
    Last edited by Leith Ross; 02-23-2014 at 04:21 PM. Reason: Added Code Tags

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,494

    Re: Problem with loop

    What would you have us do with a picture of a worksheet?

    If it were my call, I'd move the subtotal(s) from their location in-line to the right hand side perhaps on the last row of the Sunday block.


    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Problem with loop

    hi Dedaluss, welcome to Excelforum,

    option basing on the code provided (not tested due no sample file have been presented)

    Option Explicit
    
    Sub Analyse_data()
    Dim i As Long, n As Long, j As Long, lrow As Long
    Dim sh As Worksheet, targetsh As Worksheet
    Dim data, result
    
    Set sh = Sheets("PakkeIndtag")
    Set targetsh = Sheets("Data")
    
    lrow = sh.Cells(Rows.Count, 1).End(xlUp).Row
    
    data = sh.Range("a1:e" & lrow + 5)
    ReDim result(1 To lrow, 1 To 7)
    
    For i = 5 To lrow Step 6
        
        For n = i To i + 4
        
            If data(n, 1) <> "" Then
            
                j = j + 1
            
                result(j, 1) = data(1, 1) 'Station
                result(j, 2) = data(n, 1) 'MaskinTid
                result(j, 3) = data(n, 3) 'StopType
                result(j, 4) = data(n, 4) 'MaskinTid
                
                result(j, 5) = data(i, 1) 'Dag
                result(j, 6) = data(i, 2) 'Uge
                result(j, 7) = data(i, 6) 'Aar
            
            Else
            
                Exit For
                
            End If
        
        Next
        
    Next
    
    targetsh.Range(2, 1).Resize(j, 7) = result
    
    End Sub

  4. #4
    Registered User
    Join Date
    02-23-2014
    Location
    Hjørring, Denmark
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: Problem with loop

    Hi watersev,

    thx for quick reply. I have to disappoint you but code you provided those not work. Maybe it would be easy if I upload portion of the file i'm working on, so you can try it out.

    Best Regards
    Dedaluss
    Attached Files Attached Files

  5. #5
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Problem with loop

    please check attachment, press Run button or run code in Module 2
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    02-23-2014
    Location
    Hjørring, Denmark
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: Problem with loop

    Hi Watersev

    You've done it. It works perfectly. Now do you think that i could take same code, copy it, and use it like this one, just to take data from other sheets and put it on same Data sheet?

    I appreciate all your help. Please tell me if I have added reputation on to your account. This was my first reply with problem solved

    Best regards
    Dedaluss

  7. #7
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Problem with loop

    I can't answer your question. The easiest way to check that is to use this code on a different sheet of the file.

    PS. Re reputation: it was successful

  8. #8
    Registered User
    Join Date
    02-23-2014
    Location
    Hjørring, Denmark
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: Problem with loop

    Thx a lot
    PROBLEM SOLVED

    Best Regards
    Dedaluss

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Vlookup problem in a loop with cell property and variable cell problem (long title sry)
    By ExcelsiorLux in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-13-2013, 10:38 AM
  2. [SOLVED] Loop inside a loop problem!
    By questionguy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-22-2012, 12:54 PM
  3. Loop problem
    By Rams in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-01-2011, 09:50 AM
  4. For Each Next Loop Problem
    By jdbegg in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-20-2006, 10:52 PM
  5. Problem adding charts using Do-Loop Until loop
    By Chris Bromley in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-23-2005, 09:06 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1