+ Reply to Thread
Results 1 to 3 of 3

Need to apply VBA code to multiple Worksheets

  1. #1
    Registered User
    Join Date
    02-26-2006
    Posts
    68

    Need to apply VBA code to multiple Worksheets

    I have this code listed below which is in a macro module. Is there a way that I can apply this code to only the worksheet I am working in. I have worksheets labeled January thru December and need to distinqiush between the sheets.

    Dim rng As Range
    Dim lastrow As Long, r As Long, i As Integer
    With Worksheets("January") <<----This needs to be a variable for each sheet
    lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
    r = 1
    srow = r
    Do
    Do
    r = r + 1
    Loop Until .Cells(r, "a") = "Weekly Subtotal" Or r >= lastrow
    For i = 4 To 6
    Set rng = .Range(.Cells(srow, i), .Cells(r - 1, i))
    Cells(r, i) = Application.Sum(rng)
    Next i
    srow = r + 1
    Loop Until srow > lastrow
    End With

  2. #2
    Bob Phillips
    Guest

    Re: Need to apply VBA code to multiple Worksheets

    Change

    With Worksheets("January")

    to

    With Activesheet


    --

    HTH

    Bob Phillips

    (remove nothere from the email address if mailing direct)

    "parteegolfer" <[email protected]>
    wrote in message
    news:[email protected]...
    >
    > I have this code listed below which is in a macro module. Is there a way
    > that I can apply this code to only the worksheet I am working in. I have
    > worksheets labeled January thru December and need to distinqiush between
    > the sheets.
    >
    > Dim rng As Range
    > Dim lastrow As Long, r As Long, i As Integer
    > With Worksheets("January") <<----This needs to be a variable for each
    > sheet
    > lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
    > r = 1
    > srow = r
    > Do
    > Do
    > r = r + 1
    > Loop Until .Cells(r, "a") = "Weekly Subtotal" Or r >= lastrow
    > For i = 4 To 6
    > Set rng = .Range(.Cells(srow, i), .Cells(r - 1, i))
    > Cells(r, i) = Application.Sum(rng)
    > Next i
    > srow = r + 1
    > Loop Until srow > lastrow
    > End With
    >
    >
    > --
    > parteegolfer
    > ------------------------------------------------------------------------
    > parteegolfer's Profile:

    http://www.excelforum.com/member.php...o&userid=31951
    > View this thread: http://www.excelforum.com/showthread...hreadid=521533
    >




  3. #3
    Registered User
    Join Date
    02-26-2006
    Posts
    68
    Thanks Bob this worked well!

+ 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