Results 1 to 6 of 6

Thread: Help with subscript out of range

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    2

    Help with subscript out of range

    Good afternoon,
    I am having a subscript out of range error with this macro in excel.
    I had some help with getting this going so i hope the error is something simple.
    Basically i have two workbooks "template.xlsm" and "plans.xlsx"
    I play around with formulas and formatting in template.xlsm and over time i replace all of the worksheets in plans.xlsx with the worksheet from template.xlsm
    I copy a set of ranges from the old workbooks in "plans.xlsx" and copy them into the updated template that copied into the workbook from "template.xlsm" for each sheet i am replacing. Then i rename each new worksheet the same name as the old sheet and then delete the old sheet. This has become such a cumbersome process because i have so many sheets now that i need replaced each time. See macro below and please let me know what i am missing. This code was written by someone else since i am not up to speed on macros.
    I had a couple of lines that were causing the error that i will highlight in red.

    Thanks for any help. It will be greatly appreciated!


    Sub Macro1
    'Assign Workbook Variables
    WBSource = "template.xlsm"
    WBTarget = "plans.xlsx"

    'Assign Source and Target Range Variables
    SourceRng1 = "A4:A86"
    SourceRng2 = "C4:C86"
    SourceRng3 = "B11"
    TargetRng1 = "A4"
    TargetRng2 = "C4"
    TargetRng3 = "B1"

    'Assign Source and Target WorkSheet Name Variables
    SourceSheet1 = "2012 DRH Bid Template"



    For Each ws In Worksheets
    Select Case ws.Name
    Case "Table of Contents", "Taxes and Labor"
    ' do nothing in above worksheet names
    Case Else
    Var1 = ws.Name
    Workbooks(WBSource).Sheets(SourceSheet1).Copy Before:=Workbooks(WBTarget).Sheets(Var1)
    Var2 = ActiveSheet.Name
    'Copy Ranges to Targets
    Sheets(Var1).Range(SourceRng1).Copy Sheets(Var2).Range(TargetRng1)
    Sheets(Var1).Range(SourceRng2).Copy Sheets(Var2).Range(TargetRng2)
    Sheets(Var1).Range(SourceRng3).Copy Sheets(Var2).Range(TargetRng3)
    'Remove old sheet and rename new
    Application.DisplayAlerts = False
    Sheets(Var1).Delete
    Application.DisplayAlerts = True
    ActiveSheet.Name = Var1
    End Select
    Next ws
    End Sub

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Help with subscript out of range

    Ok, so this is VBA(Excel) not VBScript - which is generally used in scripting or ASP Classic... VBA is the VB that's used inside of the Office products and a different animal... I'll ask a mod to move it to the Office forums...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Help with subscript out of range

    Thread moved

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Help with subscript out of range

    Workbooks(WBSource).Sheets(SourceSheet1).Copy Before:=Workbooks(WBTarget).Sheets(Var1)
    you need to break this line down to find out which variable is causing the error

    i am assuming this is in excel 07?
    if the name of your workbooks and sheets are all correct, from the posts in this forum this may have been a common problem with early office 07, fixed with some service pack or other

    there are work arounds to avoid the error, but possibly better to get any service packs

    your code further down works with sheets, but does not specify which workbook
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    2

    Re: Help with subscript out of range

    Quote Originally Posted by westconn1 View Post
    you need to break this line down to find out which variable is causing the error

    i am assuming this is in excel 07?
    if the name of your workbooks and sheets are all correct, from the posts in this forum this may have been a common problem with early office 07, fixed with some service pack or other

    there are work arounds to avoid the error, but possibly better to get any service packs

    your code further down works with sheets, but does not specify which workbook
    I had someone to help write the code who was far more advanced than i am who was using office 07. My laptop has office 2003 so when i got the code i had to convert my files over to xlsm and xlsx. I couldnt get that to work either so i converted them back and changed the code to reflect the new file extensions.
    I am not very familiar with code so am at a loss on how to fix. if i can answer any question that would fix this i would greatly appreciate it. I have a huge spreadsheet with alot of tabs that i want to apply this to and adjust some of the values to make it work with those sheets but once i get the base code i think i can handle that. This is the last piece of code i need to finish this book and its racking my brain!

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Help with subscript out of range

    instead of using variables to hold the names of workbooks and sheets, use objects, try like
    vb Code:
    1. Sub Macro1
    2. 'Assign Workbook to objects
    3. set WBSource = workbooks("template.xlsm")  ' change extension to xls
    4. set WBTarget = workbooks("plans.xlsx")
    5.  
    6. 'Assign Source and Target Range Variables
    7. SourceRng1 = "A4:A86"
    8. SourceRng2 = "C4:C86"
    9. SourceRng3 = "B11"
    10. TargetRng1 = "A4"
    11. TargetRng2 = "C4"
    12. TargetRng3 = "B1"
    13.  
    14. 'Assign Source and Target WorkSheet Name Variables
    15. SourceSheet1 = "2012 DRH Bid Template"
    16.  
    17.  
    18.  
    19. For Each ws In wbtarget.Worksheets  ' here should be specified which workbook
    20. 'when using a for loop and deleting sheets from within the loop, some inconsistancies may occur
    21. Select Case ws.Name
    22. Case "Table of Contents", "Taxes and Labor"
    23. ' do nothing in above worksheet names
    24. Case Else
    25. Var1 = ws.Name
    26. WBSource.Sheets(SourceSheet1).Copy Before:=WBTarget.Sheets(Var1)
    27. Var2 = ActiveSheet.Name  ' do not use activesheet, specify the workbook.sheet to use, better to use a sheet object and possibly better to add a new sheet
    28. set newsht = wbtarget.sheets.add   'sheet object
    29. ' you could also set a sheet object for sheets(var1)
    30. 'Copy Ranges to Targets
    31. wbtarget.Sheets(Var1).Range(SourceRng1).Copy newsht.Range(TargetRng1)
    32. wbtarget.Sheets(Var1).Range(SourceRng2).Copy newsht.Range(TargetRng2)
    33. wbtarget.Sheets(Var1).Range(SourceRng3).Copy newsht.Range(TargetRng3)
    34. 'Remove old sheet and rename new
    35. Application.DisplayAlerts = False
    36. wbtarget.Sheets(Var1).Delete
    37. Application.DisplayAlerts = True
    38. newsht.Name = Var1
    39. End Select
    40. Next ws
    41. End Sub
    the code you are using assumes that both workbooks are already open in the same instance of excel, i may be confused as to which workbook holds the specified sheets, change to suit if required
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width