+ Reply to Thread
Results 1 to 6 of 6

Loop through all worksheets.

Hybrid View

  1. #1
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Loop through all worksheets.

    I keep getting a error on the below line. I have been trying to figure this for a while and no longer posses the ability to think out side the box.

    The macro looks for cells with "0" in them and deletes the zero.

    I get errors on these two lines,
    For Each ws In wb
                For Each cel In .UsedRange 'Range("A1:N" & LR)
    Sub clear()
       Dim cel     As Range
       Dim LR      As Long
       Dim ws      As Worksheet
       Dim wb      As Workbook
       
            Set ws = ActiveSheet
            Set wb = ActiveWorkbook
            
            LR = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row
            
        With ActiveSheet
          'On Error Resume Next
               Set cel = ActiveCell
             For Each ws In wb
                For Each cel In .UsedRange 'Range("A1:N" & LR)
                  If cel = "0" Then cel.ClearContents
                Next cel
             Next ws
        End With
    
    
    End Sub
    Last edited by JapanDave; 12-30-2011 at 07:41 PM.
    Be fore warned, I regularly post drunk. So don't take offence (too much) to what I say.
    I am the real 'Napster'
    The Grid. A digital frontier. I tried to picture clusters of information as they moved through the computer. What did they look like? Ships? motorcycles? Were the circuits like freeways? I kept dreaming of a world I thought I'd never see. And then, one day...

    If you receive help please give thanks. Click the * in the bottom left hand corner.

    snb's VBA Help Files

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Loop through all worksheets.

    "0" is text string. The number zero does not have quotes around it.

    This is how I would write that:
    Sub clear()
    Dim zFIND As Range, ws As Worksheet
       
    On Error Resume Next
    
    For Each ws In ActiveWorkbook.Worksheets
        With ws
            Set zFIND = .Cells.Find(0, LookIn:=xlValues, LookAt:=xlWhole)
            If Not zFIND Is Nothing Then
                Do
                    zFIND = ""
                    Set zFIND = Nothing
                    Set zFIND = .Cells.Find(0, LookIn:=xlValues, LookAt:=xlWhole)
                Loop Until zFIND Is Nothing
            End If
        End With
    Next ws
    
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: Loop through all worksheets.

    You meant for each WS in worksheets? And you meant numeric 0 not text value 0 right?

  4. #4
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Loop through all worksheets.

    Thanks Jerry, that did the trick. I am gathering find is quicker than looping through each cell?

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Loop through all worksheets.

    Yes, the FIND method jumps directly to the next cell with the searched value, much more efficient than checking each cell row by row yourself. Eventually there are no more values that match (since you're removing them) and the zFIND range equals nothing. At that point the macro exits.


    Note, to implement this method in a macro where the values are NOT being removed as you jump through them all, you'll need to set a secondary range to remember the first one found so that when you come back around to where you started, you'll know and can exit. Here's the same macro again without the delete value command, so you can see what I mean.

    Option Explicit
    
    Sub clear()
    Dim zFIND As Range, zFIRST As Range, ws As Worksheet
       
    On Error Resume Next
    
    For Each ws In ActiveWorkbook.Worksheets
        With ws
            Set zFIND = .Cells.Find(0, LookIn:=xlValues, LookAt:=xlWhole)
            If Not zFIND Is Nothing Then
                Set zFIRST = zFIND
                Do
                    zFIND.EntireRow.Interior.ColorIndex = 8
                    Set zFIND = .Cells.FindNext(zFIND)
                Loop Until zFIND.Address = zFIRST.Address
            End If
        End With
    Next ws
    
    End Sub

  6. #6
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Loop through all worksheets.

    Thanks Jerry,
    That make things a whole lot clearer.

+ 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