+ Reply to Thread
Results 1 to 7 of 7

speeding up a loop

Hybrid View

  1. #1
    Registered User
    Join Date
    01-07-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    16

    speeding up a loop

    Can anyone suggest a more efficient way of ordering two columns so that the same values are on the same row?

    column M has 16000 filled cells and column P has up to 50 filled cells, so my loop's really slow. For each cell in col P I need to move it to the p cell adjacent to its match in col M.

    so I want:

    1 2
    2 7
    3 9
    4
    5
    6
    7
    8
    9

    to be:

    1
    2 2
    3
    4
    5
    6
    7 7
    8
    9 9


    Thanks!

    Sub lloop()
    
    Application.ScreenUpdating = False
    
    
    Dim lcounter As Long
    
    lcounter = 2 'set this to the first row number
    
    Do While Range("M" & lcounter).Value <> "" And Range("P" & lcounter).Value <> ""
        If Range("M" & lcounter).Value <> Range("P" & lcounter) Then
            If Range("M" & lcounter).Value < Range("P" & lcounter) Then
                Range("P" & lcounter).Insert Shift:=xlDown
           
            End If
        End If
    lcounter = lcounter + 1
    Loop
    
    Application.ScreenUpdating = True
    
    End Sub

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

    Re: speeding up a loop

    hi, menos, can you post sample workbook showing original data and result you need to get?

  3. #3
    Registered User
    Join Date
    01-07-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: speeding up a loop

    Here it is; sheet "before" is an example of data I have (col A ref's to col M in the code and col B refs to P), sheet "after" is how I want it.
    Also I have discovered an error that I've corrected:

    Sub lloop()
    
    Application.ScreenUpdating = False
    
    
    Dim lcounter As Long
    
    lcounter = 2 'set this to the first row number
    
    Do While Range("M" & lcounter).Value <> "" And Range("P" & lcounter).Value <> ""
        If Range("M" & lcounter).Value <> Range("P" & lcounter) Then
     '       If Range("M" & lcounter).Value < Range("P" & lcounter) Then
                Range("P" & lcounter).Insert Shift:=xlDown
           
     '       End If
        End If
    lcounter = lcounter + 1
    Loop
    
    Application.ScreenUpdating = True
    
    End Sub
    Attached Files Attached Files
    Last edited by menos; 07-06-2011 at 06:10 AM.

  4. #4
    Valued Forum Contributor
    Join Date
    07-17-2005
    Location
    Abergavenny, Wales, UK
    MS-Off Ver
    XL2003, XL2007, XL2010, XL2013, XL2016
    Posts
    608

    Re: speeding up a loop

    Hi

    Try the following
    
    Option Explicit
    Option Base 1
    
    Sub movedata()
        Dim rng As Range, i As Long, j As Long
        Dim rng2
        Set rng = Range("B2:B" & Cells(Rows.Count, 2).End(xlUp).Row)
        rng2 = rng
        rng.ClearContents
        For i = 1 To UBound(rng2)
            j = WorksheetFunction.Match(rng2(i, 1), Columns(1), 0)
            Cells(j, "B") = Format(rng2(i, 1), "hh:mm:ss")
        Next
    End Sub
    --
    Regards
    Roger Govier
    Microsoft Excel MVP

  5. #5
    Registered User
    Join Date
    01-07-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: speeding up a loop

    Hi Roger, thanks so much, that works a treat! So that I learn, can I check that I have interpreted the code correctly?

    Set rng = Range("B2:B" & Cells(Rows.Count, 2).End(xlUp).Row) 'define the cells that has the shorter col of data in it as a range called rng
       
    rng2 = rng ' now store the values in rng in a new range called rng2
    
    rng.ClearContents 'clear contents of the cells in rng
    
    For i = 1 To UBound(rng2) ' for each value stored in rng2 call it i and ...
         j = WorksheetFunction.Match(rng2(i, 1), Columns(1), 0)  ' look for its exact match in col A and call that value j
    
    Cells(j, "B") = Format(rng2(i, 1), "hh:mm:ss") ' the cell in col b at the row containing j is that value of i formatted as a time

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    a different angle

    I put the values of column B into column C.
    Than I could use the oneliner:

    Sub snb()
      [B1:B8460] = [if(countif($C1:$C$100,A1:A8460)=0,"",A1:A8460)]
    End Sub



  7. #7
    Valued Forum Contributor
    Join Date
    07-17-2005
    Location
    Abergavenny, Wales, UK
    MS-Off Ver
    XL2003, XL2007, XL2010, XL2013, XL2016
    Posts
    608

    Re: speeding up a loop

    Wow!!

    Pretty cool solution!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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