1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

MACRO - To run on all worksheets

Discussion in 'Business Applications' started by KAC1711, Feb 18, 2016.

Thread Status:
Not open for further replies.
  1. KAC1711

    KAC1711 Thread Starter

    Joined:
    Feb 18, 2016
    Messages:
    1
    (This is my first posting)

    I'm very far from being macro knowledgeable, however, I did create two small macros that I would like to run on all worksheets, rather than just the active worksheet. And if possible, I'd like them to be the same macro, however, they seem to only work as two separate ones. I've found some code for looping the macro but I can't figure out how to add it to what I already have so that it will work. I'd really appreciate any help. See Below...


    This is my first macro, it finds the line in the worksheet that says Grand Total, and then it replaces it new text that includes adding the text from part of another cell.

    Sub GrandTotal()
    '
    ' GrandTotal Macro
    '

    '
    Cells.Replace What:="Grand Total", Replacement:= _
    "=""Grand""&REPLACE(A6,1,FIND("": "",A6)+0,"""")&"" Total""", LookAt:=xlPart _
    , SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=True
    End Sub


    This is my second macro, that finds the word 'Fee' and replaces it with 'Profit', if a cell contains specific text.


    Sub FeetoProfit()

    'Changes Fee to Profit if contract type in cell A6 says Firm Fixed Price

    If (Range("A6") = "Contract Type: Firm Fixed Price") Then
    Cells.Replace What:="Fee", Replacement:="Profit", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False

    End If

    End Sub


    Again, i want it to work on all the sheets in the workbook and if possible for them to be combined. Thanks so much!
     
  2. Rollin_Again

    Rollin_Again

    Joined:
    Sep 4, 2003
    Messages:
    4,912
    You just need to add a loop to select and process each sheet.

    Code:
    Sub ReplaceText()
    
    For i = 1 To ActiveWorkbook.Sheets.Count
    
    Sheets(i).Activate
    
    Cells.Replace What:="Grand Total", Replacement:= _
    "=""Grand""&REPLACE(A6,1,FIND("": "",A6)+0,"""")&"" Total""", LookAt:=xlPart _
    , SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=True
    End Sub
    
    If (Range("A6") = "Contract Type: Firm Fixed Price") Then
    Cells.Replace What:="Fee", Replacement:="Profit", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    End If
    
    Next i
    
    End Sub
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1166405

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice