+ Reply to Thread
Results 1 to 9 of 9

Loop thru ALL sheets, resizing one column in each based on certain criteria

  1. #1
    Registered User
    Join Date
    08-10-2012
    Location
    NY
    MS-Off Ver
    Excel 2010
    Posts
    78

    Loop thru ALL sheets, resizing one column in each based on certain criteria

    Hi -- I have the following code and it works great except for the fact that it only works on the active sheet and I'm not sure what needs to be tweaked. Any suggestions please? Thank you!

    Please Login or Register  to view this content.

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Loop thru ALL sheets, resizing one column in each based on certain criteria

    The code looks ok, Looping wise so you probably have an error that causes VBA to crash before it can loop.

    I am going to step through your code to see what happens, but you may need to do that on your spreadsheet.#

    Ok the macro works as intended with one issue.

    Nowhere do you ask the macro to switch is focus to a sheet other than the active sheet.

    Please Login or Register  to view this content.
    or

    Please Login or Register  to view this content.
    Last edited by mehmetcik; 08-20-2014 at 01:01 PM.

  3. #3
    Registered User
    Join Date
    08-10-2012
    Location
    NY
    MS-Off Ver
    Excel 2010
    Posts
    78

    Re: Loop thru ALL sheets, resizing one column in each based on certain criteria

    Thanks, that would be great. I just realized I forgot to include Dim statements for ws and wb in my sample because I declare them globally in the module.

  4. #4
    Registered User
    Join Date
    08-10-2012
    Location
    NY
    MS-Off Ver
    Excel 2010
    Posts
    78

    Re: Loop thru ALL sheets, resizing one column in each based on certain criteria

    Ok, I added the "With" statement, but the "strLongestCellRef" variable still seems to be defined by the first worksheet rather than redefine itself on each sheet.

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

    Re: Loop thru ALL sheets, resizing one column in each based on certain criteria

    This will only refer to the active sheet.
    Please Login or Register  to view this content.
    Try adding the worksheet reference ws.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  6. #6
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Loop thru ALL sheets, resizing one column in each based on certain criteria

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    08-10-2012
    Location
    NY
    MS-Off Ver
    Excel 2010
    Posts
    78

    Re: Loop thru ALL sheets, resizing one column in each based on certain criteria

    Thank you AB33! That did it!

    Thank you Norie and mehmetcik for all your help too!

  8. #8
    Registered User
    Join Date
    08-10-2012
    Location
    NY
    MS-Off Ver
    Excel 2010
    Posts
    78

    Re: Loop thru ALL sheets, resizing one column in each based on certain criteria

    Hi Guys-- I just ran this on a couple of workbooks and it seems that I am still running into an issue. While the code successfully loops through each sheet, the strLongestCellRef and intLongestCellLen are still referencing the longest string in column L of the first worksheet rather than resetting itself after each loop. I don't understand why this is happening. lngLastRow is returning an accurate reference to each successive worksheet but not the other 2. I have tried:
    Please Login or Register  to view this content.
    and regardless of which, strLongestCellRef and intLongestCellLen refer to worksheet 1. What am I missing?
    Thanks

  9. #9
    Registered User
    Join Date
    08-10-2012
    Location
    NY
    MS-Off Ver
    Excel 2010
    Posts
    78

    Re: Loop thru ALL sheets, resizing one column in each based on certain criteria

    Actually I just realized - it's not that it keeps referring to sheet 1. It continues reffering to the longest string it encounters. So If there is a 102-character string in sheet 2 and no other cell contains a string as long until sheet 6, sheet 2 will remain the reference.

+ 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. Replies: 8
    Last Post: 07-09-2014, 10:42 PM
  2. [SOLVED] Copy data from column to other sheets, based upon vlookup/criteria on column a
    By jedemeyer1 in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 03-27-2013, 04:01 AM
  3. [SOLVED] Loop to find cells based on criteria, enter formula into next empty column
    By cschoyer in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-10-2012, 03:32 PM
  4. how to loop Criteria based on unique values in a column
    By excelkeechak in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-03-2009, 11:01 AM
  5. Resizing a pivot column based on Field Name
    By cmungall in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-19-2006, 08:45 PM

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