+ Reply to Thread
Results 1 to 9 of 9

How to avoid loops

Hybrid View

niko79542 How to avoid loops 07-20-2012, 03:46 PM
fredlo2010 Re: How to avoid loops 07-20-2012, 04:05 PM
niko79542 Re: How to avoid loops 07-20-2012, 04:14 PM
Richard Buttrey Re: How to avoid loops 07-20-2012, 04:05 PM
niko79542 Re: How to avoid loops 07-20-2012, 04:18 PM
niko79542 Re: How to avoid loops 07-20-2012, 04:26 PM
Richard Buttrey Re: How to avoid loops 07-20-2012, 07:34 PM
fredlo2010 Re: How to avoid loops 07-20-2012, 04:08 PM
niko79542 Re: How to avoid loops 07-23-2012, 09:14 AM
  1. #1
    Registered User
    Join Date
    05-18-2012
    Location
    Norwalk, CT
    MS-Off Ver
    Excel 2010
    Posts
    80

    How to avoid loops

    Hi. I just learned how to use For Each...Next loops to replace If...Then loops

    Since it is best to avoid loops, I am looking for a way to solve this problem using VBA, without a loop.

    Book2.xlsm

    There is a simple code inside.

    1) Column C is a direct copy of Column B.
    2) For every cell in Column B which contains '0' I need to copy the corresponding cell from Column A and paste it in Column C
    3) For example: if cell B61 = 0, copy A61, paste in C61

    ----------------------------------------------------

    Here is the code from the xlsm file. YES, there is a reason I am not just doing =IF(B1=0, C1=A1). There are about a million other things I have to do if B = 0 and I am 99% sure I cant do them using built in Excel functions.

    Option Explicit
    
    Sub macro1()
    
    Dim i As Integer
    i = 0
    Dim target, cell As Range
    Set target = Range("b4:b3234")
    
    For Each cell In target
        If cell.Value = i Then
            Cells(cell.Row, 3) = Cells(cell.Row, 1)
        End If
    Next cell
    
    End Sub

    Once again, looking for a faster alternative to loops. Thx, Niko

  2. #2
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: How to avoid loops

    But this is a very fast macro isn't it?

  3. #3
    Registered User
    Join Date
    05-18-2012
    Location
    Norwalk, CT
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: How to avoid loops

    @fredlo2010

    This is confirmed. however my post is only an example. Theres gonna be about 10 other things I have to do when B=0, and then repeat the procedure enough times that I would benefit from a faster method.

    niko
    Last edited by Cutter; 07-23-2012 at 10:16 AM. Reason: Removed whole post quote

  4. #4
    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: How to avoid loops

    Hi,

    One way

    Sub CopyCells()
        Dim l As Long
        l = Range("A" & Rows.Count).End(xlUp).Row
        Range("D1") = 1
        Range("D1").DataSeries Rowcol:=xlColumns, Type:=xlLinear, Step:=1, Stop:=l
        Range("A1").CurrentRegion.Sort key1:=Range("B1")
        l = WorksheetFunction.Match(1, Range("B:B"))
        Range("A" & 1 & ":A" & l).Copy Destination:=Range("C1")
        Range("A1").CurrentRegion.Sort key1:=Range("D1")
        Range("D:D").EntireColumn.Delete
    End Sub
    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.

  5. #5
    Registered User
    Join Date
    05-18-2012
    Location
    Norwalk, CT
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: How to avoid loops

    @Richard Buttrey

    Hi, Could you explain a bit? I am so lost after you started using column D.

    Niko
    Last edited by Cutter; 07-23-2012 at 10:16 AM. Reason: Removed whole post quote

  6. #6
    Registered User
    Join Date
    05-18-2012
    Location
    Norwalk, CT
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: How to avoid loops

    Haha, nvm I got it.

    I'm a dummy. I used step into and figured it out.

    thx Richard.

  7. #7
    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: How to avoid loops

    Hi,

    On second thoughts rather than delete the existing column D which may have data in, it may be better to first insert a new one as the first line of code

    Range("D:D").Entirecolumn.Insert

    then the final line of code is only deleting something the macro has added.

  8. #8
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: How to avoid loops

    Hi Richard.

    You are using a filter right?

  9. #9
    Registered User
    Join Date
    05-18-2012
    Location
    Norwalk, CT
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: How to avoid loops

    Good point, Thx

+ 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