+ Reply to Thread
Results 1 to 17 of 17

Apply code to all worksheets in workbook

  1. #1
    Registered User
    Join Date
    05-12-2020
    Location
    Wales
    MS-Off Ver
    365
    Posts
    82

    Apply code to all worksheets in workbook

    I have some code that formats a range of cells in a particular worksheet in a named workbook.

    How could I apply the same code to ALL worksheets in the workbook. The worksheet names are changeable to I won't be able to reference specific worksheet names.

    Is that doable?

  2. #2
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Apply code to all worksheets in workbook

    If the code uses “activesheeT” instead of sheet name, it will work on any sheet

  3. #3
    Registered User
    Join Date
    05-12-2020
    Location
    Wales
    MS-Off Ver
    365
    Posts
    82

    Re: Apply code to all worksheets in workbook

    Yes, but I'd like the code to run on ALL the worksheets, not just one.

  4. #4
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Apply code to all worksheets in workbook

    You can write the code to work on all worksheets in the workbook, something like this

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    05-12-2020
    Location
    Wales
    MS-Off Ver
    365
    Posts
    82

    Re: Apply code to all worksheets in workbook

    It's still only applying to the activesheet???

  6. #6
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Apply code to all worksheets in workbook

    Any chance you can share your code, I might be able to provide you a better insight

  7. #7
    Forum Contributor
    Join Date
    05-08-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    223

    Re: Apply code to all worksheets in workbook

    Sub Macro1()

    Windows("Before.xlsx").Activate

    Range("A1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select

    Selection.Replace What:="XX", Replacement:="", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False

    ' Autofit relevant columns
    Columns.EntireColumn.AutoFit

    end sub

  8. #8
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,078

    Re: Apply code to all worksheets in workbook

    How about
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    05-12-2020
    Location
    Wales
    MS-Off Ver
    365
    Posts
    82

    Re: Apply code to all worksheets in workbook

    Hi Fluff

    That's great. Works a treat.

    I've added a few other elements which work fine however I'm struggling to adapt the code to make the following changes,

    Change the worksheet cell colour to white,
    Add a new row to the top,
    Merge cells ("C1") to the last populated row beneath,
    Populated the merged 'cell' with the relevant worksheet name.

    I've copied the code below, as compiled from recording macros.

    Can you please help...

    Please Login or Register  to view this content.

  10. #10
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,078

    Re: Apply code to all worksheets in workbook

    Sorry but I will have nothing to do with merged cells, they are an abomination & should be avoided like the plague.

  11. #11
    Registered User
    Join Date
    05-12-2020
    Location
    Wales
    MS-Off Ver
    365
    Posts
    82

    Re: Apply code to all worksheets in workbook

    I understand that. It is purely to create a centralised heading.

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Apply code to all worksheets in workbook

    I've got to agree about merged cells so I've used center across selection instead, see if this does what you want.
    Please Login or Register  to view this content.
    P.S. Why are you copying from row 2 to row 1 and then clearing row 1?
    If posting code please use code tags, see here.

  13. #13
    Registered User
    Join Date
    05-12-2020
    Location
    Wales
    MS-Off Ver
    365
    Posts
    82

    Re: Apply code to all worksheets in workbook

    I was copying row 2 to 1 and then clearing row 1's contents to merge the cells before placing the tab name into the merged cell.

    I didn't even consider centering it across the selection. Sounds like that would work. When I ran the above code though it placed the tab name into each cell in row 1.

    I've attached a version of the workbook indicating what I'm trying to achieve on the Apr 2021 (formatted) sheet.

    The code I have so far is shown below,

    Please Login or Register  to view this content.
    Attached Files Attached Files

  14. #14
    Registered User
    Join Date
    05-12-2020
    Location
    Wales
    MS-Off Ver
    365
    Posts
    82

    Re: Apply code to all worksheets in workbook

    Oh, and to color the worksheet white, prior to applying the header color.

  15. #15
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Apply code to all worksheets in workbook

    Quote Originally Posted by AndyEd View Post
    ...it placed the tab name into each cell in row 1...
    Oops.
    I know why that happened, see if this fixes that.

    I'll have a look at the other stuff later and post back.
    Please Login or Register  to view this content.

  16. #16
    Registered User
    Join Date
    05-12-2020
    Location
    Wales
    MS-Off Ver
    365
    Posts
    82

    Re: Apply code to all worksheets in workbook

    I've figured out of it now, albeit the code likely isn't pretty.

    There's only one element that's still causing me problems,

    I can't seem to select columns(3) to the end of the used range, to then change the columnwidth to 4.33 and centre the contents.

    I've been playing around with
    Please Login or Register  to view this content.
    and variations of but keep getting errors.

    Can anyone push me in the right direction...

  17. #17
    Registered User
    Join Date
    05-12-2020
    Location
    Wales
    MS-Off Ver
    365
    Posts
    82

    Re: Apply code to all worksheets in workbook

    I've tried numerous ways but can't get the following to work,

    Columns "C" to the end of the used range, Horizontal alignment = centre & column widths = 4.3.

    The best I can do is get the first sheet to work but t doesn't copy through the remaining sheets.

    ???

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] How do I apply this VBA code to all worksheets in my workbook?
    By jennyblack9 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-20-2015, 09:51 AM
  2. [SOLVED] How do I apply this VBA code to all worksheets in my workbook?
    By jennyblack9 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-19-2015, 01:59 PM
  3. Trying to apply vba coding to two worksheets in same workbook
    By Kaz09 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-24-2014, 07:01 AM
  4. [SOLVED] Apply Macro to all the Worksheets in Workbook
    By Floydlevedale in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-16-2013, 05:54 AM
  5. Apply same formatting to all worksheets in a workbook
    By capnhud in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-22-2010, 12:23 PM
  6. apply macro to all worksheets in the workbook
    By jbyrne in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-15-2010, 09:40 AM
  7. [SOLVED] Apply code to other worksheets
    By Metrazal in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-24-2006, 03:10 AM

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