+ Reply to Thread
Results 1 to 15 of 15

Working "With" multiple worksheets at once

  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):

    Please Login or Register  to view this content.
    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.

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,526

    Re: Working "With" multiple worksheets at once

    I suppose a loop through he sheets are what you are looking for
    Please Login or Register  to view this content.
    Last edited by davesexcel; 07-01-2010 at 04:45 PM.

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

    Re: Working "With" multiple worksheets at once

    davesexcel,
    Thanks for taking a look. I realized I didn't make this clear in the original post; I need to do this for just sheets 2-6, but not sheet1. I think your code affects every sheet in the workbook.

  4. #4
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,526

    Re: Working "With" multiple worksheets at once

    The example code I supplied affects all sheets but the sheet Named Total, Change it to the Sheet1 if that is the sheet name you don't want to affect

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

    Re: Working "With" multiple worksheets at once

    Aaaand it is "ws" to denote the current sheet before all .sort or .columns etc., sorry for the hasty question that I was able to answer myself. Two last questions, though -- if I wanted to make more than one exception in addition to "Total," how would I do that? And if I wanted to denote the exception as Sheet1 instead of the sheetname, how would that work? Thanks for being so thorough in answering all my questions, I appreciate the help.
    Last edited by Jbm444; 07-01-2010 at 04:01 PM.

  6. #6
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,526

    Re: Working "With" multiple worksheets at once

    ActiveSheet will only work if you actual select the sheets, if want to use activesheet then before you run your code you will require
    Please Login or Register  to view this content.

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

    Re: Working "With" multiple worksheets at once

    Because I hastily asked a question, and you're incredibly fast at answering my questions, when I edited my post to answer my own question and ask two new questions, you had already posted a reply.
    The upshot being: could you please answer the two new questions I asked in my post before this?
    Again, apologies for editing right after I posted, instead of trying to answer myself before posting or just posting my two new questions in a non-edit.

  8. #8
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,526

    Re: Working "With" multiple worksheets at once

    You can Just add another If Statement, check out the original code with the bold change

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

    Re: Working "With" multiple worksheets at once

    This should be the last thing on this: The bold addition takes care of my first question, but let's say I don't know the name of Sheet1 when I'm running the code. It could be "Sheet1" or it could be "Report" or maybe "7/2/2009 Report." I still want to run the code as is without changing it to account for the different ws.name I'm excepting. How do I tell the ws. code to make an exception not based on the name of the sheet, but rather on the number of the sheet?

  10. #10
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Working "With" multiple worksheets at once

    Perhaps ...
    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

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

    Re: Working "With" multiple worksheets at once

    shg,
    It runs into an error with the "case" method here:
    Please Login or Register  to view this content.
    .Rows gets highlighted in an error in this instance.

    Also, I was assuming on how the form of case worked: I just had Case 2 To 6 since I only needed sheets 2-6, but if I needed sheets 9 and 14-22 as well I would just use the exact example code you provided. This is correct?

  12. #12
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Working "With" multiple worksheets at once

    Correct, it's just a list of the relevant sheet indices.

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

    Re: Working "With" multiple worksheets at once

    Good to know shg, that should help in the future with using the case method.
    However, the error remains, so I wonder if either there's something I should be looking into to defeat it, or stick with davesexcel's original method with some kind of change so that I'm not using sheetnames?

  14. #14
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Working "With" multiple worksheets at once

    I can't look at one line of code in isolation and know what you're trying to do.

    Maybe this:
    Please Login or Register  to view this content.

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

    Re: Working "With" multiple worksheets at once

    Sorry that wasn't clear, that was just the line of code that errored from the larger code section in the original post.
    Your new code seems to work, with the addition of "dim lrow as long" at the top (if anyone else tries to adapt this code). Unfortunately I don't have the time to check for sure today, but it looks like it'll work. Thanks for the help!

+ 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