|
-
Aug 31st, 2005, 10:14 AM
#1
Thread Starter
Fanatic Member
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")
-
Aug 31st, 2005, 10:39 AM
#2
Re: Problems copying Excel Worksheet.......
Your placing double quotes around your variable amking it literal.
VB Code:
Workbooks([b]"[/b]SourceWbk[b]"[/b]).Sheets("Log (2)").Copy After:=Workbooks([b]"[/b]DestWbk[b]"[/b]).Sheets("Menu")
'Should be...
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 31st, 2005, 10:46 AM
#3
Thread Starter
Fanatic Member
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.
-
Aug 31st, 2005, 10:48 AM
#4
Thread Starter
Fanatic Member
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.
-
Aug 31st, 2005, 11:11 AM
#5
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 31st, 2005, 11:14 AM
#6
Thread Starter
Fanatic Member
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.
-
Sep 1st, 2005, 12:34 AM
#7
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Sep 1st, 2005, 03:34 AM
#8
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?
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...
-
Sep 1st, 2005, 12:57 PM
#9
Thread Starter
Fanatic Member
Re: Problems copying Excel Worksheet.......
Thanks, I'll give that a try.
-
Sep 2nd, 2005, 01:57 PM
#10
Thread Starter
Fanatic Member
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.
-
Sep 2nd, 2005, 10:48 PM
#11
Lively Member
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?
-
Sep 2nd, 2005, 11:29 PM
#12
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:
Dim SourcePath, DestPath, SourceFile, DestFile As String
Dim SourceWbk, DestWbk as String
'Should be...
Dim SourcePath As String, DestPath As String, SourceFile As String, DestFile As String
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Sep 3rd, 2005, 03:24 AM
#13
Lively Member
Re: Problems copying Excel Worksheet.......
Try this, it assumes that Door_Inventory is the workbook with the code:
VB Code:
Option Explicit
Sub COPY_FROM_MainSched_TO_DoorSched()
Dim SourcePath As String
Dim DestPath As String
Dim SourceFile As String
Dim DestFile As String
Dim SourceWbk As String
Dim 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 SourceWbk
'Copy the sheet from the source workbook to the Destination workbook
ActiveWorkbook.Sheets("Log(2)").Copy after:=ThisWorkbook.Sheets("Menu")
Windows(DestFile).Activate
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|