Results 1 to 13 of 13

Thread: Problems copying Excel Worksheet.......

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Problems copying Excel Worksheet.......

    I am using the following code to copy a worksheet from one workbook to another, but keep receiving a "Subscript out of range" error. I have double checked the names of the worksheets and they are correct. Any ideas?

    Code:
    Sub COPY_FROM_MainSched_TO_DoorSched()
    
    Dim SourcePath, DestPath, SourceFile, DestFile As String
    
    'set variables
    SourcePath = "H:\schedule\"
    DestPath = "H:\shared_tools\"
    SourceFile = "Cabot, S.xls"
    DestFile = "door_inventory.xls"
    SourceWbk = SourcePath & SourceFile
    DestWbk = DestPath & DestFile
    
        Range("A1").Select
        Application.DisplayAlerts = False
        Application.ScreenUpdating = False
        Workbooks.Open Filename:=SourceWbk
        Workbooks("SourceWbk").Sheets("Log (2)").Copy After:=Workbooks("DestWbk").Sheets("Menu")
        Windows(DestWbk).Activate
        
       
    
       
    End Sub
    Error occurs on this line: "Workbooks("SourceWbk").Sheets("Log (2)").Copy After:=Workbooks("DestWbk").Sheets("Menu")

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Problems copying Excel Worksheet.......

    Your placing double quotes around your variable amking it literal.
    VB Code:
    1. Workbooks([b]"[/b]SourceWbk[b]"[/b]).Sheets("Log (2)").Copy After:=Workbooks([b]"[/b]DestWbk[b]"[/b]).Sheets("Menu")
    2.  
    3. 'Should be...
    4. Workbooks(SourceWbk).Sheets("Log (2)").Copy After:=Workbooks(DestWbk).Sheets("Menu")
    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
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Re: Problems copying Excel Worksheet.......

    Ahh, thanks a lot. I'm an idiot! You can tell I haven't done any programming in a long while. Thanks again.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Re: Problems copying Excel Worksheet.......

    hmm... still getting the same error, even after removing the double quotes. I looked at the examples in the MSDN Knowledge Base, I don't see why it's not working. It didn't think I'd have this much trouble trying to copy a worksheet from one workbook to another. I've also tried:

    Code:
     sourcewbk.Worksheets("Log (2)").Copy After:= destwbk.Worksheets.("menu")
    Last edited by hipopony66; Aug 31st, 2005 at 11:12 AM.

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Problems copying Excel Worksheet.......

    Could it be the comma in the file name? What OS and Excel version are you running?

    Cabot, S.xls
    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

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Re: Problems copying Excel Worksheet.......

    Windows XP & Windows NT 4.0 With Office 2000 & 2003. I am able to open the file though, so why not copy from it? I set a breakpoint & step through, & the workbook opens just fine.

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Problems copying Excel Worksheet.......

    Maybe its a timing issue when your running in real time? It this running on the Workbook_Open event?
    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

  8. #8
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Problems copying Excel Worksheet.......

    Code:
    Sub COPY_FROM_MainSched_TO_DoorSched()
    
        Dim SourcePath as String, DestPath as String
        dim SourceFile as String, DestFile As String
        dim wrkSrc as workbook, shtSrc as worksheet
        dim wrkDest as workbook, shtDest as worksheet
    
    
    'set variables
        SourcePath = "H:\schedule\"
        DestPath = "H:\shared_tools\"
        SourceFile = "Cabot, S.xls"
        DestFile = "door_inventory.xls"
    
        on error resume next
    
        SourceWbk = SourcePath & SourceFile
        DestWbk = DestPath & DestFile
    
        Application.DisplayAlerts = False
        Application.ScreenUpdating = False
    
    
        set wrkSrc = Workbooks.Open(SourceWbk)
        set wrkDest = Workbooks.Open(DestWbk)
    
    '---- this should in theory do the copy
    '---- put a break point on the next line - use f8 to step through
        wrk.Sheets("Log (2)").Copy After:=wrkDest.Sheets("Menu")
        
    '---- not sure this will work!
        wrkDest.Activate
    
        if err.number=0 then
            msgbox "Complete"
        else  
            msgbox "Error : " & err.number & vbcrlf & err.description,vbokonly,"Error" 
        end if
       
    '---- clean up
        set wrkSrc = nothing
        set wrkDest = nothing
       
    End Sub
    Try that?

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Re: Problems copying Excel Worksheet.......

    Thanks, I'll give that a try.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Still no go..........

    Still no luck with several variations of the code. I think I see part of the problem.
    Code:
    Sub COPY_FROM_MainSched_TO_DoorSched()
    
    Dim SourcePath, DestPath, SourceFile, DestFile As String
    Dim SourceWbk, DestWbk as String
    
    
    
    'set variables
    SourcePath = "H:\schedule\"
    DestPath = "H:\shared_tools\"
    SourceFile = "Cabot, S.xls"
    DestFile = "door_inventory.xls"
    SourceWbk = SourcePath & SourceFile
    DestWbk = DestPath & DestFile
    
    'Disable alerts & screen updating
        Application.DisplayAlerts = False
        Application.ScreenUpdating = False
        
    'Open the source workbook
        Workbooks.Open Filename:=SourceWbk
        
    'Copy the sheet from the source workbook to the Destination workbook
        Workbooks(SourceWbk).Sheets("Log(2)").Copy After:=Workbooks(DestWbk).Sheets("Menu")
        Windows(DestWbk).Activate
        
       
    End Sub
    In order to build the path to the location of the file, I have to use strings. Once I get the path, I somehow need to convert SourceWbk & DestWbk to Workbook instead of string. Any ideas? It's the wrong type of variable in the copy statement that may be causing the problem, I think.

  11. #11
    Lively Member JustinLabenne's Avatar
    Join Date
    Jul 2005
    Location
    Ohio
    Posts
    64

    Re: Problems copying Excel Worksheet.......

    Is the destination workbook open?

    What is the exact Full name of both the Source workbook and the Destination workbook?

    Like:
    H:\Schedule\DestFIle.xls 'Destination
    H:\BlahTools\SourceFile 'Source

    Are either of these workbooks the file containing the code your running?
    Justin Labenne
    www.jlxl.net

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Problems copying Excel Worksheet.......

    Doubt it will help but when you dim your vars that way it actually dims them as a Variant when you dont specify.
    VB Code:
    1. Dim SourcePath, DestPath, SourceFile, DestFile As String
    2. Dim SourceWbk, DestWbk as String
    3. 'Should be...
    4. Dim SourcePath As String, DestPath As String, SourceFile As String, DestFile As String
    5. Dim SourceWbk As String, DestWbk as String
    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

  13. #13
    Lively Member JustinLabenne's Avatar
    Join Date
    Jul 2005
    Location
    Ohio
    Posts
    64

    Re: Problems copying Excel Worksheet.......

    Try this, it assumes that Door_Inventory is the workbook with the code:

    VB Code:
    1. Option Explicit
    2.  
    3. Sub COPY_FROM_MainSched_TO_DoorSched()
    4.  
    5. Dim SourcePath As String
    6. Dim DestPath As String
    7. Dim SourceFile As String
    8. Dim DestFile As String
    9. Dim SourceWbk As String
    10. Dim DestWbk As String
    11.  
    12.  
    13. 'set variables
    14. SourcePath = "H:\schedule\"
    15.  
    16. DestPath = "H:\shared_tools\"
    17.  
    18. SourceFile = "Cabot, s.xls"
    19.  
    20. DestFile = "door_inventory.xls"
    21.  
    22. SourceWbk = SourcePath & SourceFile
    23.  
    24. DestWbk = DestPath & DestFile
    25.  
    26. 'Disable alerts & screen updating
    27.     Application.DisplayAlerts = False
    28.     Application.ScreenUpdating = False
    29.    
    30. 'Open the source workbook
    31.     Workbooks.Open SourceWbk
    32.    
    33. 'Copy the sheet from the source workbook to the Destination workbook
    34.     ActiveWorkbook.Sheets("Log(2)").Copy after:=ThisWorkbook.Sheets("Menu")
    35.     Windows(DestFile).Activate
    36.    
    37.     Application.DisplayAlerts = True
    38.     Application.ScreenUpdating = True
    39. End Sub
    Justin Labenne
    www.jlxl.net

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