+ Reply to Thread
Results 1 to 16 of 16

Trying to tighten up some code

  1. #1
    Registered User
    Join Date
    03-12-2011
    Location
    Maryland
    MS-Off Ver
    Excel 2007
    Posts
    19

    Question Trying to tighten up some code

    Hi All,

    I am very new to the vba world and learning on the job...I completed a macro which does (almost) everything I want to do...but it is inelegant to say the least...can someone please help me tighten it up? As you can see it is doing the exact same thing...from sheet TS, looks for 2011, copies those rows to sheet2011, keeps going and then looks for 2012 and copies those rows to sheet 2012 and so on. I tried some for next loops and couldn't get them to work.

    I then repeat this process for another 8 sheets, so you can see how the code grew in length. The only consideration for application for the next sheets is they must be processed in a specific order ie. TS, TS1, TS2 TS3 etc., Help here also greatly appreciated, but tightening up the first part will help so much!

    Thank you!

    Steve

    Please Login or Register  to view this content.
    Last edited by pike; 03-12-2011 at 12:43 AM. Reason: remove php and add code tags

  2. #2
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Trying to tighten up some code

    Try this untested code
    [EDIT]
    This only works for one sheet, I missed the second part of the question.

    Please Login or Register  to view this content.

    The main point is not to use Selection, it is rarely required and only slows your code
    You can also omit Cstr in most cases

    Hope this helps
    Last edited by Marcol; 03-12-2011 at 02:33 AM.
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  3. #3
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Trying to tighten up some code

    Hmm? On second thoughts this might be a bit cleaner
    Please Login or Register  to view this content.
    Hope this helps
    Last edited by Marcol; 03-13-2011 at 10:44 AM. Reason: Missed second part of question

  4. #4
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Trying to tighten up some code

    DrSteve
    welcome to the forum or maybe..
    Please Login or Register  to view this content.
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  5. #5
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Trying to tighten up some code

    Marcol likes Select but this will work also
    Please Login or Register  to view this content.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  6. #6
    Registered User
    Join Date
    03-12-2011
    Location
    Maryland
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Trying to tighten up some code

    Thank you for all your help guys...I see the principles of what you have suggested

    I tried removing all the references to different sheets to paste from to simplify things. But I couldn't make any of the suggestions work.

    In Marcol's first suggestion it worked for all sheets apart from 2011!
    In MarvinP's suggestion it seemed to copy data into sheets 2012 onwards, but not 2011!
    It looks like there is a very tiny thing not set right.

    In Pike's suggestion it complained myarray not being defined. So I took out the for/next loop and had it try on just the TS sheet..
    Please Login or Register  to view this content.
    These suggestions are all so close...I feel it! I think that's why I stopped programming back in my old "micro" days!

    Thanks guys.

    Steve
    Last edited by pike; 03-13-2011 at 04:29 PM. Reason: remove php tags

  7. #7
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Trying to tighten up some code

    You need to have a sheet named "2011" and "2012". If you have spaces before or after it may not work. Check the exact spelling of your sheet tabs.

    If not the spelling of the Tabs, then perhaps you have spaces in the data. "2011" is not equal to "2011 " as there is a space after the second string.
    Last edited by MarvinP; 03-13-2011 at 09:59 AM.

  8. #8
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Trying to tighten up some code

    Try this modified code.
    Please Login or Register  to view this content.

    Provided your sheets are named TS, TS1,TS2 to TS whatever and the number suffixes are continuous then this should work.
    Attached Files Attached Files
    Last edited by Marcol; 03-13-2011 at 10:45 AM.

  9. #9
    Registered User
    Join Date
    03-12-2011
    Location
    Maryland
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Trying to tighten up some code

    Marcol...you hit the nail on the head....now instead of having 8 versions of the previous set I now have a mere 15 lines of code for everything. This code has achieved 99%...it has copied all of my sheets exceot TS!

    But...here was me trying to make things simple. The actual sheets I am copy from are: TS, ECFQA, MTCD, PD, OQ, OE, WCFQA.

    I am going to look into using the codenames for the sheets which I did put in order for sheets 1-8. I am thinking a code along the lines of:

    Dim ws
    For x = 1 to 8
    set wscopyfrom = sheet(x)
    .....
    next x

    The copyto eventually can be set as sheet(y) where y = 2011 to 2015. But first things first.

    For now...off to push the kids aorund on their bikes!

    Thank you!

    Steve

  10. #10
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Trying to tighten up some code

    use with marcol example
    Please Login or Register  to view this content.
    steve the whole story would have been nice
    Please Login or Register  to view this content.

  11. #11
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Trying to tighten up some code

    or ?

    Please Login or Register  to view this content.
    Last edited by snb; 03-13-2011 at 06:27 PM.



  12. #12
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Trying to tighten up some code

    The code in post #8 will copy data from all sheets where the sheet name begins with TS
    Did you try the sample workbook? It copies sheet TS

    The extra loop was added to cover
    .....they must be processed in a specific order ie. TS, TS1, TS2 TS3 etc.,
    This loop counts an indefinite number of sheets begining with TS and processes them in order regardless of their position in the workbook

    However what you asked for wasn't what you wanted...

    Go with Pikes' last code.

    [EDIT]
    @ snb Your code doesn't run, what am I missing?
    Last edited by Marcol; 03-13-2011 at 06:41 PM.

  13. #13
    Registered User
    Join Date
    03-12-2011
    Location
    Maryland
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Trying to tighten up some code

    Thank you for your help guys and Marcol for giving me the background into the script itself. Really helps me learn

    I tried SNB's and Pike's codes but they both gave Runtime Error "9" - subscript out of range.

    PHP Code: 
     Sheets(cl.Text).Cells(Rows.Count1).End(xlUp).Offset(1).Resize(, 6) = cl.Offset(, -5).Resize(, 6).Value 
    I don't see where a subscript (or subscript like) comment would even be! I removed all of the spaces from SNB's script and I can confirm all of the sheet names are written correctly on both the spreadsheet and the script.

    Thank you!

  14. #14
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Trying to tighten up some code

    Debugging without the datasheet is not possible.

    'subscript out of range' means that no such sheet exists:
    whether "TS", "ECFQA", "MTCD", "PD", "OQ", "OE", "WCFQA"
    or cl.text (i.e the values in column F form F2 downwards)

  15. #15
    Registered User
    Join Date
    03-12-2011
    Location
    Maryland
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Trying to tighten up some code

    Thank you for all your help guys. I have learned a lot, but cannot seem to make the leap to get the tightened up script to work.

    Steve

  16. #16
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Trying to tighten up some code

    Better attach the workbook

+ 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