+ Reply to Thread
Results 1 to 10 of 10

Loop that takes a long time

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-16-2013
    Location
    london
    MS-Off Ver
    Excel 2007
    Posts
    136

    Loop that takes a long time

    Hi,

    The below look has to go through 30,000 lines and takes ages. Is there a more efficent way to achive the same outcome. All it does is concatenates columns 6 & 7 together.


    x = 2
    Do While Cells(x, 1).Value <> ""
    Cells(x, 8).Value = Cells(x, 6).Value & " " & Cells(x, 7).Value
    x = x + 1
    Loop

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Loop that takes a long time

    It loops 30,000 times. Worse is it may do infinite loop.

  3. #3
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: Loop that takes a long time

    Dim lRw As Long, x As Byte
    
    x = 2
    lRw = Cells(Rows.Count, "A").End(xlUp).Row
    
    With Range("D" & x & ":D" & lRw)
        .Formula = "=F" & x & "&" & "G" & x
        .Value = .Value
    End With


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Loop that takes a long time

    SS,
    The OP does not use column A or D. Columns F-H

  5. #5
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: Loop that takes a long time

    Quote Originally Posted by AB33 View Post
    The OP does not use column A or D. Columns F-H
    Oops... Thanks for the correction

    I just tested it by creating a small data and forgot to change it to actual references

  6. #6
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: Loop that takes a long time

    Here is the corrected version of code

    Dim lRw As Long, x As Byte, sCol As String, sC1 As String, sC2 As String
    
    lRw = Cells(Rows.Count, "A").End(xlUp).Row
    
    x = 2
    sCol = "H"
    sC1 = "F"
    sC2 = "G"
    
    With Range(sCol & x & ":" & sCol & lRw)
        .Formula = sC1 & x & "&" & sC2 & x
        .Value = .Value
    End With

  7. #7
    Forum Contributor
    Join Date
    01-16-2013
    Location
    london
    MS-Off Ver
    Excel 2007
    Posts
    136

    Re: Loop that takes a long time

    Thanks what if I wanted a space between the two values

  8. #8
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: Loop that takes a long time

    Quote Originally Posted by batchjb69 View Post
    Thanks what if I wanted a space between the two values
    Add a space within the & like the below

    .Formula = sC1 & x & "& " & sC2 & x

  9. #9
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Loop that takes a long time

    Why not just use a standard function in Excel rather than VBA
    i.e. in H2 copied down

    Formula: copy to clipboard
    =F2&" "&G2


    Or if you need the macro equivalent

    Sub AddFormula()
       Range("H2:H30000") = "=F2&""""&G2"
    End Sub
    Avoid loops at all costs since they just take too long when compared to using standard functionality.
    Last edited by Richard Buttrey; 06-26-2014 at 08:55 AM.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  10. #10
    Forum Contributor
    Join Date
    01-16-2013
    Location
    london
    MS-Off Ver
    Excel 2007
    Posts
    136

    Re: Loop that takes a long time

    Hi thats what I thought but it did not work

+ 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. Calculation takes long time
    By dorend in forum Excel General
    Replies: 10
    Last Post: 12-13-2011, 06:17 PM
  2. how do i add time to see how long my macro takes to run
    By ernestgoh in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-16-2006, 06:10 AM
  3. Excel Help takes a very long time to load
    By Sam Pack via OfficeKB.com in forum Excel General
    Replies: 4
    Last Post: 03-03-2006, 08:10 PM
  4. Save takes long time
    By Jan in forum Excel General
    Replies: 2
    Last Post: 02-15-2006, 02:10 PM
  5. [SOLVED] Time Calculation - How long a macro takes to run
    By cdb in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-31-2005, 04: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