+ Reply to Thread
Results 1 to 30 of 30

Subscript out of range

  1. #1
    Registered User
    Join Date
    01-29-2007
    Posts
    69

    Subscript out of range

    I am at a sticking point with a macro code that has stumped a few experienced users. I have verified the file names and sheet names are correct and both are open when i run the macro from template.xls
    Any suggestions?


    Please Login or Register  to view this content.
    Last edited by JESSIER4025; 02-08-2012 at 01:18 PM. Reason: Marked Solved

  2. #2
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Subscript out of range

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  3. #3
    Registered User
    Join Date
    01-29-2007
    Posts
    69

    Re: Subscript out of range

    i have fixed the infraction. Can anyone offer any assistance? From what i have been told, the person who helped me with this did it in office 07 and i currently have office 03. This is the last part of code that i have to figure out to complete the workbook i have been working on for weeks.

    Thanks

  4. #4
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Subscript out of range

    Which line gives the error?
    Good luck.

  5. #5
    Valued Forum Contributor
    Join Date
    07-21-2008
    Location
    London, UK
    Posts
    326

    Re: Subscript out of range

    Where is Your code failing ie which line code is highlighted when the code bombs?

  6. #6
    Registered User
    Join Date
    01-29-2007
    Posts
    69

    Re: Subscript out of range

    I get an error on this line

    Windows("plans.xls").Activate
    I checked the file names and verified no leading or trailing characters in all sheets and file names.
    When i hit F8 and step down on the highlighted line i hover over and get =empty until i step past each line.

    thanks

  7. #7
    Registered User
    Join Date
    01-29-2007
    Posts
    69

    Re: Subscript out of range

    here are the source files if it helps.
    Attached Files Attached Files

  8. #8
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Subscript out of range

    Use
    Please Login or Register  to view this content.
    instead.

  9. #9
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Subscript out of range

    Or
    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    01-29-2007
    Posts
    69

    Re: Subscript out of range

    same error. Unfortunately this code has stumped a few people for why it wont work.
    Its still errors at same spot.
    Thanks

  11. #11
    Registered User
    Join Date
    01-29-2007
    Posts
    69

    Re: Subscript out of range

    Quote Originally Posted by OnErrorGoto0 View Post
    Or
    Please Login or Register  to view this content.
    i replaced the code with this and now its bombing at this line

    Set WBTarget = Workbooks("plans.xls")

    for some reason it always has something to do with the target file. I have verified names over and over and its there but it wont work.

  12. #12
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Subscript out of range

    If that fails, then you do not have that workbook open in the same instance of Excel.

  13. #13
    Registered User
    Join Date
    01-29-2007
    Posts
    69

    Re: Subscript out of range

    Quote Originally Posted by OnErrorGoto0 View Post
    If that fails, then you do not have that workbook open in the same instance of Excel.
    you are correct. I forgot to reopen that sheet before i replaced the code. now i can get alot further down in the code to this and it stops.

    WBSource.Sheets(SourceSheet1).Copy Before:=WBTarget.Sheets(Var1)
    Var2 = ActiveSheet.Name

  14. #14
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Subscript out of range

    With an error? If so what, and on which line (can't be both of those)?

  15. #15
    Registered User
    Join Date
    01-29-2007
    Posts
    69

    Re: Subscript out of range

    when i highlight the bad line at this part of the line the error is:
    WBTarget.Sheets(var1)= <object variable or with block variable is not set>

  16. #16
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Subscript out of range

    Ah - you haven't assigned a value to var1.

  17. #17
    Registered User
    Join Date
    01-29-2007
    Posts
    69

    Re: Subscript out of range

    Thats the part i wanted the macro to do for me. The goal of this macro is to take the sheet from template.xls and replace each sheet in plans.xls and copy the data ranges in each old sheet and paste it into the new sheet that is replacing it then rename the new sheet the same as the old one and delete the old sheet. how should i handle that variable? I am not very knowledgeable on code.

  18. #18
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Subscript out of range

    My mistake, you have actually assigned it a value. In fact you could just use
    Please Login or Register  to view this content.

  19. #19
    Registered User
    Join Date
    01-29-2007
    Posts
    69

    Re: Subscript out of range

    Quote Originally Posted by OnErrorGoto0 View Post
    My mistake, you have actually assigned it a value. In fact you could just use
    Please Login or Register  to view this content.
    I believe its almost done!! The code doesnt error out anymore but i am not sure its doing anything. It flashes like its doing something but i dont see any change to the sheets in the target book. To test this i went back to template.xls and changed cell a1 since it should stay intact throughout the macro run and that change should appear in the result of replace the old sheets in the target file. At the moment it is not updating the sheets that i can tell.
    Suggestions?

  20. #20
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Subscript out of range

    Is this any better
    Please Login or Register  to view this content.

  21. #21
    Registered User
    Join Date
    01-29-2007
    Posts
    69

    Re: Subscript out of range

    Quote Originally Posted by OnErrorGoto0 View Post
    Is this any better
    Please Login or Register  to view this content.
    that gets the sheet from template.xls copied into plans.xls but then subscript error at line
    WBSource.Sheets(SourceSheet1).Copy Before:=ws
    when i hit f8 to step through the code it gets to "table of contents" then jumps down to end select then back to "table of contents" then to WBsource and errors.

  22. #22
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Subscript out of range

    There is a space on the end of the sheet name in your template file - i.e. it is "2012 DRH Bid Template " rather than "2012 DRH Bid Template"

  23. #23
    Registered User
    Join Date
    01-29-2007
    Posts
    69

    Re: Subscript out of range

    Quote Originally Posted by OnErrorGoto0 View Post
    There is a space on the end of the sheet name in your template file - i.e. it is "2012 DRH Bid Template " rather than "2012 DRH Bid Template"
    Wow its doing something strange. I opened the files back and template.xls sheet name 2012 DRH Bid Template had been renamed one of the books from plans.xls
    and the data ranges had been copied into it. Then the 2012 DRH Template sheet was copied into Plans.xls
    Very strange

  24. #24
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Subscript out of range

    You might try not opening the template file at all - you will need to modify the path to the template file
    Please Login or Register  to view this content.

  25. #25
    Registered User
    Join Date
    01-29-2007
    Posts
    69

    Re: Subscript out of range

    which lines do i modify? i modified strTemplatePath= WBTarget.Path & "\" & "template.xls"
    to

    strTemplatePath = WBTarget.Path & "\" & "c:\documents and settings\rstevens1\desktop\template.xls"

    anywhere else?

  26. #26
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Subscript out of range

    That should just be
    Please Login or Register  to view this content.
    no other change required

  27. #27
    Registered User
    Join Date
    01-29-2007
    Posts
    69

    Re: Subscript out of range

    Quote Originally Posted by OnErrorGoto0 View Post
    That should just be
    Please Login or Register  to view this content.
    no other change required
    I believe that finallly didnt it!! One last thing.. In my books i have links that i receive an alert to update when i open them. I went to edit links and turned off the alert and told it to automatically update but i still get the message as the macro is working.
    If i can fix that all will be done!!

  28. #28
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Subscript out of range

    Please test
    Please Login or Register  to view this content.

  29. #29
    Registered User
    Join Date
    01-29-2007
    Posts
    69

    Re: Subscript out of range

    Quote Originally Posted by OnErrorGoto0 View Post
    Please test
    Please Login or Register  to view this content.
    Thank you so much! That was the final piece to my huge puzzle i have been putting together for quite a while. I really appreciate your help!!!
    Awesome!

  30. #30
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Subscript out of range

    My pleasure.

    Please do not forget to mark the thread Solved. (also, it is not necessary to quote my entire post back at me!)

+ 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