+ Reply to Thread
Results 1 to 8 of 8

Macro looping

Hybrid View

  1. #1
    Registered User
    Join Date
    12-09-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    8

    Macro looping

    Hi all,

    I'm using the following Macro to delete all rows that have a cell containing USD in a column range from F6 to F5000. The macro works but it only deletes a row at a time when run. Can anyone tell me how get this to loop through all rows until the end?

    Thanks (Excel 2003)


    Sub test()
    Dim Last_Row As Long, n As Long
    
    Application.ScreenUpdating = False
        
    
    Last_Row = Range("F6:F5000").End(xlDown).Row
    
    For n = 5 To Last_Row
        If Cells(n, 6).Value = USD Then
            Cells(n, 6).EntireRow.Hidden = False
        Else: Cells(n, 6).EntireRow.Hidden = True
        End If
    Next n
            
    Application.ScreenUpdating = True
    
    End Sub
    Last edited by davesexcel; 03-16-2011 at 06:38 AM. Reason: code tags required, read the forum rules please....Help is assumed in a Help forum.

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,523

    Re: Macro looping

    Check out this example

    Delete rows on criteria


    Click the Reset button to populate Column A

  3. #3
    Registered User
    Join Date
    12-09-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Macro looping

    Quote Originally Posted by davesexcel View Post
    Check out this example

    Delete rows on criteria


    Click the Reset button to populate Column A
    Thanks for the reply and attached example. Isn't it possible to add something the the end of my macro to do this? I don't really want to re-write it.

    Thanks

  4. #4
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Macro looping

    stjohnsmythe
    when hiding rows it best to loop in reverse order so the row numbers not hidden
    Option Explicit
    Sub test()
        Dim nRow As Long
        Application.ScreenUpdating = False
        For nRow = Cells(Rows.Count, 6).End(xlUp).Row + 1 To 6 Step -1
            If Not Cells(nRow, 6).Value = "USD" Then
                Cells(nRow, 6).EntireRow.Hidden = True
            End If
        Next
        Application.ScreenUpdating = True
    End Sub
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  5. #5
    Registered User
    Join Date
    12-09-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Macro looping

    [QUOTE=pike;2490674]stjohnsmythe
    when hiding rows it best to loop in reverse order so the row numbers not hidden[code]QUOTE]

    Thanks, but this macro has the opposite result. It hides any row that doesn't have USD in any cell in the column, whereas I wanted it to hide any row that does have USD in a cell.

    Cheers!

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Macro looping

    I would use AutoFilter, automate it with VBA if necessary
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  7. #7
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Macro looping

    Roy has suggested the correct approach

    Option Explicit
    Sub test()
        Dim nRow As Long
        Application.ScreenUpdating = False
        For nRow = Cells(Rows.Count, 6).End(xlUp).Row + 1 To 6 Step -1
            If Cells(nRow, 6).Value = "USD" Then
                Cells(nRow, 6).EntireRow.Hidden = True
            End If
        Next
        Application.ScreenUpdating = True
    End Sub

  8. #8
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Macro looping

    I had trouble posting back yesterday.

    I would actually automate AutoFilter to do this. Dave has an example in his workbook, but it needs some changes.

    The button won't work because there is no data to filter on the page containing the button. The sheet to work with needs referring to in the code. Also, ScreenUpdating needs switching back on at the end, I usually add an error handler to make sure.

    Sub DeleteRwsWithAutoFilter()
        With Sheet2
            'by Dave Morrison
            On Error GoTo err_exit
            'timer not really necessary
            '  .Range("K1") = Format(Now, "h:mm:ss.0000")
            Application.ScreenUpdating = False
            .Range("A1").AutoFilter Field:=1, Criteria1:="a"
            'delete visible Rows, this is where an error could occur
            'using Specialcells will error if no Specialcells are found
            .Range("A1").CurrentRegion.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
            .Range("A1").AutoFilter
            .Range("L1") = Format(Now, "h:mm:ss.0000")
            '     .Range("M1") = Format(Range("L1") - Range("K1"), "h:mm:ss.0000")
    err_exit:
            On Error GoTo 0
            Application.ScreenUpdating = False
        End With
    End Sub
    Last edited by royUK; 03-17-2011 at 04:56 AM.

+ 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