+ Reply to Thread
Results 1 to 8 of 8

VBA Code is not cycling through all worksheets

  1. #1
    Forum Contributor
    Join Date
    02-13-2013
    Location
    Columbia, SC
    MS-Off Ver
    Excel 2010
    Posts
    217

    Question VBA Code is not cycling through all worksheets

    Hello, I am trying to get my code to cycle through all worksheets. Can someone please help me? This is what I have:

    Sub DeleteRowWithContents()

    Dim ws As Worksheet
    For Each ws in Worksheets

    Last = Cells(Rows.Count, "C").End(xlUp).Row
    For i = Last To 1 Step -1
    If (Cells(i, "C").Value) <> "01" Then

    Cells(i, "A").EntireRow.Delete
    End If
    Next i
    Next ws
    End Sub

    Maybe it has to do with having two "Next's" in a row?

    Ben

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: VBA Code is not cycling through all worksheets

    Ben

    The code is looping through all the worksheets, but the code within the loop is only referencing the active sheet.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    02-13-2013
    Location
    Columbia, SC
    MS-Off Ver
    Excel 2010
    Posts
    217

    Re: VBA Code is not cycling through all worksheets

    Do you know how I fix this error? The code you posted only did the one worksheet

  4. #4
    Forum Contributor
    Join Date
    02-13-2013
    Location
    Columbia, SC
    MS-Off Ver
    Excel 2010
    Posts
    217

    Re: VBA Code is not cycling through all worksheets

    Works! Thanks!

  5. #5
    Forum Contributor
    Join Date
    02-13-2013
    Location
    Columbia, SC
    MS-Off Ver
    Excel 2010
    Posts
    217

    Re: VBA Code is not cycling through all worksheets

    Actually, one more question: Do you how to start this formula on row 2? I do not want it to mess up my row 1

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: VBA Code is not cycling through all worksheets

    The code I posted should fix the error as it makes sure that the code within the loop is referencing the correct worksheet.

    When I test the code it deletes all rows without "01" in column C on all the worksheets.

  7. #7
    Forum Contributor
    Join Date
    02-13-2013
    Location
    Columbia, SC
    MS-Off Ver
    Excel 2010
    Posts
    217

    Re: VBA Code is not cycling through all worksheets

    Yes, thanks. Works great! I really appreciate it! Do you also know how I might start the code on row 2 rather than row 1? Just realized it is deleting my header on row 1

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: VBA Code is not cycling through all worksheets

    Change the loop to end on 2 instead of 1.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

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