Results 1 to 15 of 15

Working "With" multiple worksheets at once

Threaded View

  1. #1
    Registered User
    Join Date
    06-11-2010
    Location
    grinnell, iowa
    MS-Off Ver
    Excel 2007
    Posts
    79

    Working "With" multiple worksheets at once

    Hi,
    I have some lengthy code in certain areas which right now I am just copy/pasting and changing the With Sheet1 to With Sheet2 and so forth. Is there any way to write a "With" statement that affects multiple worksheets at once? Here's my (nonworking) attempt at it (if you replace every instance of "SheetnumeRo" with "Sheet2" or the like, the code functions properly):

    Sub Macro1()
    Dim numeRo As Long
    On Error Resume Next
    Dim mylastrowe As Long
    numeRo = 2
    For numeRo = 2 To 6
    With SheetnumeRo
    mylastrowe = .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    
        SheetnumeRo.Columns("B:B").TextToColumns Destination:=Range("B1"), DataType:=xlDelimited, _
            TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
            Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
            :="/", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, _
            1), Array(6, 1), Array(7, 1)), TrailingMinusNumbers:=True
            'Delimits column B on the condition on a /.
        SheetnumeRo.Columns("D:D").Replace What:="John", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
            SheetnumeRo.Columns("B:B").Replace What:=":", Replacement:="://", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
                SheetnumeRo.Sort.SortFields.Clear
        SheetnumeRo.Sort.SortFields.Add Key:=Range("D1:D" & mylastrowe), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With SheetnumeRo.Sort
            .SetRange Range("A1:P" & mylastrowe)
            .Header = False
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        End With
        Next numeRo
        End Sub
    Ideally, this should just run for sheets 2-6, but obviously that doesn't happen. I also tried "With Sheet1 And Sheet2 And Sheet3...etc" but that doesn't work either. Is there any way to do this besides just copy/pasting this code over and over again for every worksheet?
    Last edited by Jbm444; 07-01-2010 at 06:25 PM.

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