Consulting

Results 1 to 3 of 3

Thread: Solved: Code not updating all worksheets

  1. #1
    VBAX Regular
    Joined
    May 2010
    Posts
    11
    Location

    Solved: Code not updating all worksheets

    Hello,

    I have written the following code and it works but only for the active worksheet. I want the code to cycle through all worksheets. If I make a worksheet active (by clicking the tab) and run the macro it works for that active worksheet. There are hundreds of worksheets. Can someone help me with why it is not looking at all worksheets? What am I missing?

    [vba]Sub ClearExc()

    Application.ScreenUpdating = False

    Dim sh As Worksheet

    For Each sh In Worksheets
    If LCase(sh.Name) <> "admin" Then
    If InStr(Range("D4").Value, "@") Then
    Range("A120:I500").Select
    Selection.ClearContents
    Range("A120").Select
    End If
    End If
    Next

    Application.ScreenUpdating = True

    End Sub
    [/vba]

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,732
    Location
    You need to specify the sheet on Range() 's

    Without the worksheet, it assumes the ActiveSheet

    Also, not necessary to Select something to act on it

    [vba]
    Sub ClearExc()

    Application.ScreenUpdating = False

    Dim sh As Worksheet

    For Each sh In Worksheets
    If LCase(sh.Name) <> "admin" Then
    If InStr(sh.Range("D4").Value, "@") Then
    sh.Range("A120:I500").ClearContents
    sh.Range("A120").Select
    End If
    End If
    Next

    Application.ScreenUpdating = True

    End Sub
    [/vba]

    Paul

    PS. Not tested

  3. #3
    VBAX Regular
    Joined
    May 2010
    Posts
    11
    Location
    Paul, Thank you. That worked perfectly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •