+ Reply to Thread
Results 1 to 6 of 6

Looping Macro

Hybrid View

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

    Looping Macro

    Hello all,

    Need a little help writing a macro that will run through a report and change the following:

    If Col D = “Open”, then change to “

    If Col D = “Open”, then Col E changes to “Y”, Col F changes to “Y” and Col G changes to “N.”

    Please see attached workbook DatesReport for before and after versions. Would need this to loop through over 3K rows.

    Thanks for any and all help!
    Last edited by ExcelGuy160; 09-20-2010 at 07:10 PM.

  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,311

    Re: Looping Macro

    One way:

    Sub test()
    
    Application.ScreenUpdating = False
    With Range("D2")
        .AutoFilter
        .AutoFilter Field:=4, Criteria1:="OPEN"
    End With
    
    For Each cell In Intersect(Range("D2:D" & Range("D" & Rows.Count).End(xlUp).Row), _
                               Range("D1").CurrentRegion)
        ' Debug.Print cell.Address
        cell.Value = "SVOD_C_I_W"   ' D
        cell.Offset(0, 1) = "Y"     ' E
        cell.Offset(0, 2) = "Y"     ' F
        cell.Offset(0, 3) = "N"     ' G
    Next 'cell
    Application.ScreenUpdating = True
    
    End Sub

    Regards

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

    Re: Looping Macro

    Thanks TMshucks,

    Unfortunately, this macro deleted all rows except the ones marked "open."

    I was trying to keep all rows where they are, just change "Open"
    Last edited by ExcelGuy160; 09-20-2010 at 07:10 PM.

  4. #4
    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,311

    Re: Looping Macro

    Deleted ... or hidden?

    It shouldn't have deleted anything.

    Click on the drop down selection arrow on column D and select all.

    Regards

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

    Re: Looping Macro

    Ah, thanks - these were hidden.

  6. #6
    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,311

    Re: Looping Macro

    This is a better version of the code. A little longer but a) it will only change the OPEN lines; b) if you run it twice it doesn't mess up the header; and c) it removes the Autofilter

    Sub test()
    
    Application.ScreenUpdating = False
    With Range("D2")
        .AutoFilter
        .AutoFilter Field:=4, Criteria1:="OPEN"
    End With
    
    If Intersect(Range("D2:D" & Range("D" & Rows.Count).End(xlUp).Row), _
                               Range("D1").CurrentRegion, _
                               Range("D1").SpecialCells(xlCellTypeVisible)).Cells.Count = 1 _
    Then
        Range("D2").AutoFilter
        Exit Sub
    End If
    
    For Each cell In Intersect(Range("D2:D" & Range("D" & Rows.Count).End(xlUp).Row), _
                               Range("D1").CurrentRegion, _
                               Range("D1").SpecialCells(xlCellTypeVisible))
        ' Debug.Print cell.Address
        cell.Value = "SVOD_C_I_W"   ' D
        cell.Offset(0, 1) = "Y"     ' E
        cell.Offset(0, 2) = "Y"     ' F
        cell.Offset(0, 3) = "N"     ' G
    Next 'cell
    Range("D2").AutoFilter
    
    Application.ScreenUpdating = True
    
    End Sub

    Regards

+ 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