Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Copy from one workbook to another failing

I have copied this code pretty much entirely from another post but it
throws up Run time error 9 subscript out of range'. Can anyone help?
Thanks


Public Sub ImportData()
Dim myFilename As Variant
Dim importbook As Workbook
Dim mainbook As Workbook



MsgBox "Please select the Do Not Trace workbook you wish to import."

myFilename = Application.GetOpenFilename("Excel Files (*.xls), *.xls")
'launches prompt to select import file

Set mainbook = ActiveWorkbook

Set importbook = Workbooks.Open(Filename:=myFilename) 'sets variable
to be workbook that opens



importbook.Worksheets("sheet1").Copy _
befo=mainbook.Worksheets(1)


importbook.Close savechanges:=False 'closes the import sheet
end sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,565
Default Copy from one workbook to another failing

Couldn't duplicate the error. It worked Ok for me.


"Shazbot79" wrote in message
...
I have copied this code pretty much entirely from another post but it
throws up Run time error 9 subscript out of range'. Can anyone help?
Thanks


Public Sub ImportData()
Dim myFilename As Variant
Dim importbook As Workbook
Dim mainbook As Workbook



MsgBox "Please select the Do Not Trace workbook you wish to import."

myFilename = Application.GetOpenFilename("Excel Files (*.xls), *.xls")
'launches prompt to select import file

Set mainbook = ActiveWorkbook

Set importbook = Workbooks.Open(Filename:=myFilename) 'sets variable
to be workbook that opens



importbook.Worksheets("sheet1").Copy _
befo=mainbook.Worksheets(1)


importbook.Close savechanges:=False 'closes the import sheet
end sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 921
Default Copy from one workbook to another failing

Public Sub ImportData()
Dim myFilename As String
Dim importbook As Workbook
Dim mainbook As Workbook

MsgBox "Please select the Do Not Trace workbook you wish to import."

myFilename = Application.GetOpenFilename("Excel Files (*.xls), *.xls")
'launches prompt to select import file

Set mainbook = ActiveWorkbook

Set importbook = Workbooks.Open(Filename:=myFilename)
'sets variable to be workbook that opens


'Your looking for a worksheet named "sheet1"
'The sheet your importing likley has a differnt name.
' Try

importbook.Worksheets(1).Copy _
befo=mainbook.Worksheets(1)
'Instead

importbook.Close savechanges:=False 'closes the import sheet
End Sub




"Shazbot79" wrote:

I have copied this code pretty much entirely from another post but it
throws up Run time error 9 subscript out of range'. Can anyone help?
Thanks


Public Sub ImportData()
Dim myFilename As Variant
Dim importbook As Workbook
Dim mainbook As Workbook



MsgBox "Please select the Do Not Trace workbook you wish to import."

myFilename = Application.GetOpenFilename("Excel Files (*.xls), *.xls")
'launches prompt to select import file

Set mainbook = ActiveWorkbook

Set importbook = Workbooks.Open(Filename:=myFilename) 'sets variable
to be workbook that opens



importbook.Worksheets("sheet1").Copy _
befo=mainbook.Worksheets(1)


importbook.Close savechanges:=False 'closes the import sheet
end sub
.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 489
Default Copy from one workbook to another failing

Most likely you don't have a sheet named "Sheet1" in your ImportBook
workbook. Check that and let me know. Hope this helps! If so, let me know,
click "YES' below.

Public Sub ImportData()

Dim MainBook As Workbook
Dim myFileName As Variant
Dim ImportBook As Workbook

MsgBox "Please select the Do Not Trace workbook you wish to import."


' launches prompt to select import file
myFileName = Application.GetOpenFilename("Excel Files (*.xls), *.xls")

Set MainBook = ActiveWorkbook

' sets variable to be workbook that opens
Set ImportBook = Workbooks.Open(Filename:=myFileName)

' copy import sheet to mainbook sheet
ImportBook.Sheets("Sheet1").Copy Befo=MainBook.Sheets(1)

' closes the import sheet
ImportBook.Close SaveChanges:=False

End Sub
--
Cheers,
Ryan


"Shazbot79" wrote:

I have copied this code pretty much entirely from another post but it
throws up Run time error 9 subscript out of range'. Can anyone help?
Thanks


Public Sub ImportData()
Dim myFilename As Variant
Dim importbook As Workbook
Dim mainbook As Workbook



MsgBox "Please select the Do Not Trace workbook you wish to import."

myFilename = Application.GetOpenFilename("Excel Files (*.xls), *.xls")
'launches prompt to select import file

Set mainbook = ActiveWorkbook

Set importbook = Workbooks.Open(Filename:=myFilename) 'sets variable
to be workbook that opens



importbook.Worksheets("sheet1").Copy _
befo=mainbook.Worksheets(1)


importbook.Close savechanges:=False 'closes the import sheet
end sub
.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Copy from one workbook to another failing

Thanks guys that helps me a bit.

I changed the bit of code that was failing to:

importbook.Worksheets(1).Copy _
befo=mainbook.Worksheets(1)

mainbook.Worksheets(1).Cells.Copy
With mainbook.Worksheets(2).Cells
ActiveSheet.Paste
End With

but now it doesn't fail....sadly nor does it copy, if I try to
reference the worksheet directly is fails again.

What I want to do is just copy the cells from importbook.worksheets(1)
to a named worksheet in mainbook.

Can anyone help?
Thanks


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 489
Default Copy from one workbook to another failing

This code worked fine for me. I wouldn't suggest coping entire worksheet
cells. If the worksheet is somewhat large it may run slow. Plus I added an
If...Then Statement to prevent Excel throwing an error in case the user
clicks Cancel when asked to select the import workbook. Hope this helps! If
so, let me know, click "YES" below.

Public Sub ImportData()

Dim MainBook As Workbook
Dim myFileName As Variant
Dim ImportBook As Variant

MsgBox "Please select the Do Not Trace workbook you wish to import."


' launches prompt to select import file
myFileName = Application.GetOpenFilename("Excel Files (*.xls), *.xls")

' user clicks cancel
If myFileName = False Then
MsgBox "You didn't select an import workbook.", vbExclamation
Exit Sub
End If

Set MainBook = ActiveWorkbook

' sets variable to be workbook that opens
Set ImportBook = Workbooks.Open(Filename:=myFileName)

' copy import sheet to mainbook sheet
ImportBook.Sheets(1).Copy Befo=MainBook.Sheets(1)

' closes the import sheet
ImportBook.Close SaveChanges:=False

End Sub
--
Cheers,
Ryan


"Shazbot79" wrote:

Thanks guys that helps me a bit.

I changed the bit of code that was failing to:

importbook.Worksheets(1).Copy _
befo=mainbook.Worksheets(1)

mainbook.Worksheets(1).Cells.Copy
With mainbook.Worksheets(2).Cells
ActiveSheet.Paste
End With

but now it doesn't fail....sadly nor does it copy, if I try to
reference the worksheet directly is fails again.

What I want to do is just copy the cells from importbook.worksheets(1)
to a named worksheet in mainbook.

Can anyone help?
Thanks
.

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,565
Default Copy from one workbook to another failing

Then try this:

Replace this:

importbook.Worksheets(1).Copy _
befo=mainbook.Worksheets(1)

With This:


importbook.Worksheets(1).UsedRange.Copy _
mainbook.Worksheets(1).Range("A1")






"Shazbot79" wrote in message
...
Thanks guys that helps me a bit.

I changed the bit of code that was failing to:

importbook.Worksheets(1).Copy _
befo=mainbook.Worksheets(1)

mainbook.Worksheets(1).Cells.Copy
With mainbook.Worksheets(2).Cells
ActiveSheet.Paste
End With

but now it doesn't fail....sadly nor does it copy, if I try to
reference the worksheet directly is fails again.

What I want to do is just copy the cells from importbook.worksheets(1)
to a named worksheet in mainbook.

Can anyone help?
Thanks



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Copy from one workbook to another failing

Thanks so much...I'm still not 100% why the code wasn't working but
I've made changes as suggested and it works fine now!
Thanks
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,565
Default Copy from one workbook to another failing

If you want to get the first sheet of each workbook that you open, then use
the sheet index instead of the sheet name:

importbook.Worksheets(1).Copy _
befo=mainbook.Worksheets(1)

If you only want to copy those sheets named Sheet1, then:

On Error GoTo ErrHndl:
importbook.Worksheets("sheet1").Copy _
befo=mainbook.Worksheets(1)

ErrHndl:
If Err.Number . 0 Then
If Err.Number = 9 Then
MsgBox "Sheet1 not found"
Err.Clear
Else
MsgBox Err.Number & " has occured, consult help file" _
& " for trappable errors."
End If
End If
On Error GoTo 0



"Shazbot79" wrote in message
...
I have copied this code pretty much entirely from another post but it
throws up Run time error 9 subscript out of range'. Can anyone help?
Thanks


Public Sub ImportData()
Dim myFilename As Variant
Dim importbook As Workbook
Dim mainbook As Workbook



MsgBox "Please select the Do Not Trace workbook you wish to import."

myFilename = Application.GetOpenFilename("Excel Files (*.xls), *.xls")
'launches prompt to select import file

Set mainbook = ActiveWorkbook

Set importbook = Workbooks.Open(Filename:=myFilename) 'sets variable
to be workbook that opens



importbook.Worksheets("sheet1").Copy _
befo=mainbook.Worksheets(1)


importbook.Close savechanges:=False 'closes the import sheet
end sub



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy code failing LiAD Excel Programming 2 November 5th 09 07:36 AM
Macros failing when workbook is in shared mode M Hanlon Excel Programming 1 September 27th 08 04:04 AM
copy method of object failing vj2india Excel Programming 0 April 20th 06 02:21 AM
loop through a column on a workbook copying data on each row to another workbook, then copy data back to the original workbook burl_rfc Excel Programming 1 April 1st 06 08:48 PM
Excel Copy Method Failing. [email protected] Excel Programming 2 May 11th 05 06:01 PM


All times are GMT +1. The time now is 09:49 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"