+ Reply to Thread
Results 1 to 5 of 5

Clear Contents on Column

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-29-2008
    Location
    usa
    MS-Off Ver
    EXCEL 2010
    Posts
    116

    Clear Contents on Column

    Hi I am looking for some help on this in macro I have over 185000 rows and I ran this code it took over hour to run.
    Do you a faster code that can accomplish the same as this one in less time?
    Any help is greatly appreciated

    Sub ClearDupOnD()
    Dim StartRow As Long
    Dim EndRow As Long
    Dim R As Long
    With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
    .EnableEvents = False
    End With
    StartRow = 1
    EndRow = Cells(StartRow, "B").End(xlDown).Row
    For R = EndRow To StartRow Step -1
    If Application.CountIf(Range(Cells(1, "D"), _
    Cells(R, "D")), Cells(R, "D").Value) > 1 Then
    Range("D" & R).ClearContents
    End If
    Next R
    With Application
    .ScreenUpdating = True
    .Calculation = xlCalculationAutomatic
    .EnableEvents = True
    End With
    End Sub

  2. #2
    Valued Forum Contributor john55's Avatar
    Join Date
    10-23-2010
    Location
    Europe
    MS-Off Ver
    Excel for Microsoft 365
    Posts
    2,060

    Re: Clear Contents on Column

    hi,
    see if it helps you
    Sub Button1_Click()
      Application.ScreenUpdating = 0
      Application.Calculation = xlCalculationManual
      Dim cell As Range
      For Each cell In Application.Intersect(ActiveSheet.Range("d:d"), _
             ActiveSheet.UsedRange)
        If Trim(cell.Value) > 1 Then
            ActiveSheet.Range(cell, cell.Offset(0, 4)).ClearContents
        End If
      Next
      Application.Calculation = xlCalculationAutomatic
      Application.ScreenUpdating = 1
      End Sub

    if you want to know how long it takes to be completed...
    Sub Button1_Click()
    Dim start_time, end_time
    Dim cell As Range
     Application.ScreenUpdating = 0
    Application.Calculation = xlCalculationManual
    
    start_time = Now()
      
      For Each cell In Application.Intersect(ActiveSheet.Range("d:d"), _
             ActiveSheet.UsedRange)
        If Trim(cell.Value) > 1 Then
            ActiveSheet.Range(cell, cell.Offset(0, 4)).ClearContents
        End If
      Next
      Application.Calculation = xlCalculationAutomatic
      Application.ScreenUpdating = 1
    
    end_time = Now()
    MsgBox "completed in" & " " & (DateDiff("s", start_time, end_time)) & " " & "seconds"
    
    End Sub
    Last edited by john55; 01-13-2012 at 03:47 AM.
    Regards, John55
    If you have issues with Code I've provided, I appreciate your feedback.
    In the event Code provided resolves your issue, please mark your Thread as SOLVED.
    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

    ...enjoy -funny parrots-

  3. #3
    Forum Contributor
    Join Date
    09-29-2008
    Location
    usa
    MS-Off Ver
    EXCEL 2010
    Posts
    116

    Re: Clear Contents on Column

    beforehand thank for your help I try using the code that you posted and it run really fast but it is giving me different result than the one posted so I attach a sample spreadsheet so you can see why is erasing everything on d and the other 3 columns to the right
    Attached Files Attached Files

  4. #4
    Valued Forum Contributor john55's Avatar
    Join Date
    10-23-2010
    Location
    Europe
    MS-Off Ver
    Excel for Microsoft 365
    Posts
    2,060

    Re: Clear Contents on Column

    hi martinez,
    try this one
    Sub Button2_Click()
    
    Application.ScreenUpdating = 0
      Application.Calculation = xlCalculationManual
      Dim cell As Range
      For Each cell In Application.Intersect(ActiveSheet.Range("d:d"), _
             ActiveSheet.UsedRange)
        If Trim(cell.Value) > 1 Then
            ActiveSheet.Range(cell, cell.Offset(0, 0)).ClearContents
        End If
      Next
      Application.Calculation = xlCalculationAutomatic
      Application.ScreenUpdating = 1
    End Sub
    Attached Files Attached Files

  5. #5
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Clear Contents on Column

    See if this works for you, it should be a tad faster than looping
    Sub ClearDupOnD()
        Dim StartRow As Long, LastRow As Long, LastCol As Long
        
        LastRow = Cells(Rows.Count, "B").End(xlUp).Row
        LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
        Cells(2, LastCol + 1).Resize(LastRow - 1, 1).Formula = "=IF(COUNTIF($D$2:$D2,$D2)=1,D2,"""")"
        Range(Cells(2, LastCol + 1), Cells(LastRow, LastCol + 1)).Copy
        Range("D2").PasteSpecial xlPasteValues
        Range(Cells(2, LastCol + 1), Cells(LastRow, LastCol + 1)).ClearContents
        
    End Sub
    Attached Files Attached Files
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

+ 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