Results 1 to 4 of 4

Thread: *SOLVED* subscript out of range error

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Location
    right here
    Posts
    87

    Resolved *SOLVED* subscript out of range error

    I have made a macro for copy and paste into a new file.
    Copied it into vba editor into my code.
    Worked fine, but not anymore.
    It makes a new file but as soon as it wants to go back to
    "Windows("file.xls").Activate"
    "it gives a suscripts out of range" error
    I have checked for the value of "strBestandMap"
    and "strBestand" both are not changed and are fine.

    This is my code:

    VB Code:
    1. Workbooks.Add
    2.     ChDir strBestandMap
    3.     ActiveWorkbook.SaveAs Filename:=strBestand
    4.     Windows("file.xls").Activate
    5.     Sheets("resultaten").Select
    6.     Cells.Select
    7.     Selection.Copy
    8.     Windows("" & strBestandNaam & "").Activate
    9.     Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
    10.         False, Transpose:=False
    11.     Application.CutCopyMode = False
    12.     Range("A1").Activate
    13.     ActiveWorkbook.Save
    14.     ActiveWorkbook.Close

    Thanks in advance,
    Brian
    Last edited by brianbaart; Nov 1st, 2004 at 11:04 AM.
    If Not Now Then When

    If Not Here Then Where

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    When you do a SaveAs it will change the file to the new name
    and the original one is closed. Use the .SaveCopyAs instead.

    VB Code:
    1. ActiveWorkbook.SaveCopyAs Filename:=strBestand
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Location
    right here
    Posts
    87

    Sorry it has taken so long but NO

    Converting SaveAs to SaveCopyAs has not helped at all.

    VB Code:
    1. Workbooks.Add
    2.     ChDir strBestandMap
    3.     ActiveWorkbook.SaveAs Filename:=strBestand
    4.     Workbooks("file.xls").Activate
    5.     Sheets("resultaten").Select
    6.     Cells.Select
    7.     Selection.Copy
    8.     Workbooks("" & strBestandNaam & "").Activate
    9.     Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
    10.         False, Transpose:=False
    11.     Application.CutCopyMode = False
    12.     Range("A1").Activate
    13.     ActiveWorkbook.Save
    14.     ActiveWorkbook.Close

    The programs stops at:
    Workbooks("Royaltys.xls").Activate
    And gives a "Subscript out of range" error

    It has worked before I have made some changes to the code somewhere else in the program (nothing to do with this)
    and now it doesn't work.

    HELP SOMEBODY HELP,

    Thanks in advance,
    Brian
    If Not Now Then When

    If Not Here Then Where

  4. #4

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Location
    right here
    Posts
    87

    Solved It

    Thanks for reading it,
    But I have made a new macro.

    Sheets("resultaten").Select
    Cells.Select
    Selection.Copy
    Workbooks.Add
    Range("A1").Select
    ActiveSheet.Paste
    Range("A1").Select
    Application.CutCopyMode = False
    ActiveWorkbook.SaveAs Filename:=strBestand
    ActiveWindow.Close
    Range("A1").Select
    Sheets("start").Select
    Range("A1").Select

    It no longer gives the error
    Don't know why but it doesn't.
    If Not Now Then When

    If Not Here Then Where

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