Results 1 to 6 of 6
  1. #1
    Bronze Lounger
    Join Date
    Feb 2008
    Posts
    1,494
    Thanks
    135
    Thanked 7 Times in 7 Posts

    Using Array in Code to select two sheets

    I have the following code that I have written, but want to streamline this so that instead of first selecting sheets (2) and running the code and then
    selecting sheets(4) , I would like to streamline this in one operation

    Code:
     Sheets(2).Select
    Finalrow = Range("A65536").End(xlUp).Row
    If LCase(Range("a" & Finalrow).Value) = "total" Then
        Range("A" & Finalrow).EntireRow.ClearContents
        End If
    Sheets(4).Select
    Finalrow = Range("A65536").End(xlUp).Row
    If LCase(Range("a" & Finalrow).Value) = "total" Then
        Range("A" & Finalrow).EntireRow.ClearContents
        End If

  2. #2
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,675
    Thanks
    58
    Thanked 1,064 Times in 989 Posts
    You can use a for loop.

    for iSheetNum = 2 to 4 step 2
    Sheets(iSheetNum).Select
    code...
    next iSheetNum

    cheers, Paul

  3. #3
    Bronze Lounger
    Join Date
    Feb 2008
    Posts
    1,494
    Thanks
    135
    Thanked 7 Times in 7 Posts
    Hi Paul

    Thanks for the help

    Howard

  4. #4
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,772
    Thanks
    402
    Thanked 1,550 Times in 1,405 Posts
    Howard,

    I'd suggest a slight change
    From: Finalrow = Range("A65536").End(xlUp).Row
    To: Finalrow = Range("A" & Rows.Count).End(xlUp).Row

    This will future proof you code should you move to Office 2007+ and have more than 65536 rows. Yes you probablly won't but it is better to write unbreakable code if possible as in this case. HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  5. #5
    Gold Lounger Maudibe's Avatar
    Join Date
    Aug 2010
    Location
    Pa, USA
    Posts
    2,789
    Thanks
    138
    Thanked 705 Times in 639 Posts
    Howard,

    Looping through the sheets as Paul posted will work if the sheets always have the same order. If this may change in the future, perhaps a better approach would be something like this:

    Code:
    ~~~your code~~~
    DelRows "Sheet1"
    DelRows "Sheet2"
    ~~~your code~~~
    
    
    Public Sub DelRows(ShtName)
    With Worksheets(ShtName)
    Finalrow = .Range("A" & Rows.Count).End(xlUp).Row
    If LCase(.Range("A" & Finalrow).Value) = "total" Then
        .Range("A" & Finalrow).EntireRow.ClearContents
    End If
    End With
    End Sub
    You can add as many sheets as you wish to call with any name you wish and bury the DelRows subroutine out of sight. You main code will be shorter since the additional coding was removed and the subroutine can be called from anywhere in your project. This can be amended to work with using the sheet indexes instead of sheet names as well.

    HTH,
    Maud
    Last edited by Maudibe; 2013-12-15 at 08:08. Reason: spelling

  6. #6
    Bronze Lounger
    Join Date
    Feb 2008
    Posts
    1,494
    Thanks
    135
    Thanked 7 Times in 7 Posts
    Hi Guys

    Thanks for the advise and input

    Howard

Posting Permissions

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