+ Reply to Thread
Results 1 to 3 of 3

Thread: Macro to run on certain sheets

  1. #1
    Registered User
    Join Date
    08-01-2011
    Location
    Rome, Georgia
    MS-Off Ver
    Excel 2007
    Posts
    7

    Macro to run on certain sheets

    I have a macro that works fine but I must run it on each sheet. I had a friend write the macro for me so I'm still just learning how to do this stuff. I've found several ways to make it run on every sheet but I don't want it to do that. Basically, the workbook has a Billing sheet, individual month sheets (not used in this marco) and then individual client sheets. I want this to run on all my client sheets (currently 4 but will be growing). I appreciate any help. Here is the code I have:
    Sub PetersBilling()
    If ActiveSheet.Name = "Billing" Then

    MsgBox ("Please choose another source sheet.")

    Else
    Dim rSourceName As String
    Dim rSourceInvDate As String
    Dim rSourcePayDate As String
    Dim rSourceDecBill As String
    Dim shCurrentPage As String
    rSourceName = "='" & ActiveSheet.Name & "'!$C$5"
    rSourceInvDate = "='" & ActiveSheet.Name & "'!$Q$5"
    rSourcePayDate = "='" & ActiveSheet.Name & "'!$T$5"
    rSourceDecBill = "='" & ActiveSheet.Name & "'!$P$21"
    Sheets("Billing").Select
    Range("B2").Select

    Do Until Selection.Value = ""
    Selection.Offset(1, 0).Select
    Loop

    Selection.Value = rSourceName
    Selection.Offset(0, 1).Select
    Selection.Value = rSourceInvDate
    Selection.Offset(0, 1).Select
    Selection.Value = rSourcePayDate
    Selection.Offset(0, 1).Select
    Selection.Value = rSourceDecBill

    End If

    End Sub

  2. #2
    Valued Forum Contributor tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    USA
    MS-Off Ver
    Excel 2003 - 2007
    Posts
    2,352

    Re: Macro to run on certain sheets

    pmterp,

    Welcome to the forum. In the future, please wrap your code in code tags. As to your request, you should be able to use the following code. Just change what's in BadSheets to be the names of sheets you don't want included in the macro.
    Sub PetersBilling()
        
        Const BadSheets As String = "Billing,Jan,Feb,etc"
        
        Dim ws As Worksheet
        For Each ws In ActiveWorkbook.Sheets
            If InStr(1, BadSheets, ws.Name, vbTextCompare) = 0 Then Sheets("Billing").Cells(Rows.Count, "B").End(xlUp).Offset(1, 0).Resize(1, 4).Value = Array(ws.[C5].Value, ws.[Q5].Value, ws.[T5].Value, ws.[P21].Value)
        Next ws
    
    End Sub


    Hope that helps,
    ~tigeravatar

  3. #3
    Registered User
    Join Date
    08-01-2011
    Location
    Rome, Georgia
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Macro to run on certain sheets

    Thank you so much. That does exactly what I want. I wish I understood how it works, but I'm content knowing it does.

+ 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.2.0