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


Hello,

I need some help debugging my code.
From an opened excel spreadsheet I would like to determine the last row
that has data in it and copy that data to a new workbook.

The error I am receiving is in the code to copy and is as follows:
Run-time error ‘1004’: Application defined or object defined error.

Thanks for your time and advise.

Ron


Private Sub SaveData_Click()

Dim ExcelLastCell As Object
Dim LastRowWithData As String
Dim Row As String
Dim NewBook As Object


' Find last cell with data in starting at cell A10

Range("A10").Select

Set ExcelLastCell = ActiveSheet.Cells.SpecialCells(xlLastCell)

' Determine the last row with data in it

LastRowWithData = ExcelLastCell.Row
Row = ExcelLastCell.Row
Do While Application.CountA(ActiveSheet.Rows(Row)) = 0 And Row < 1
Row = Row - 1
Loop
LastRowWithData = Row

' Create new workbook

Set NewBook = Workbooks.Add
With NewBook
..SaveAs Filename:="new.xls"
End With

' Copy selected cells from original workbook sheet 5 to sheet 1 of the
new workbook

Workbooks("original.xls").Worksheets("sheet
5").Range("A10:LastRowWithData").Copy _
Destination:=Workbooks("new.xls").Worksheets("Shee t1").Range("A1")

End Sub


------------------------------------------------
~~ Message posted from http://www.ExcelTip.com/
~~View and post usenet messages directly from http://www.ExcelForum.com/

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Copy from active workbook to new workbook.


Error in this code

Workbooks("original.xls").Worksheets("shee
5").Range("A10:LastRowWithData").Copy
Destination:=Workbooks("new.xls").Worksheets("Shee t1").Range("A1")

should be

Workbooks("original.xls").Worksheets("sheet 5").Range("A10:"
LastRowWithData).Copy
Destination:=Workbooks("new.xls").Worksheets("Shee t1").Range("A1")



Also if you have one column that has an entry in each row then
suggest you use


dim LastRowWithData
LastRowWithData = Range("a65536").End(xlUp).Row

' Create new workbook

Set NewBook = Workbooks.Add
With NewBook
.SaveAs Filename:="new.xls"
End With


Workbooks("original.xls").Worksheets("sheet 5").Range("A10:"
LastRowWithData).Copy
Destination:=Workbooks("new.xls").Worksheets("Shee t1").Range("A1"

-----------------------------------------------
~~ Message posted from http://www.ExcelTip.com
~~View and post usenet messages directly from http://www.ExcelForum.com

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Copy from active workbook to new workbook.

suggested correction:
Workbooks("original.xls").Worksheets("sheet 5").Range("A10:" &
LastRowWithData).Copy _
Destination:=Workbooks("new.xls").Worksheets("Shee t1").Range("A1")

has an error,
should be

Workbooks("original.xls").Worksheets("sheet 5").Range("A10:A" &
LastRowWithData).Copy _
Destination:=Workbooks("new.xls").Worksheets("Shee t1").Range("A1")

LastRowWithData appears to be a string containing a row number (no column
letter)

--
Regards,
Tom Ogilvy



mudraker wrote in message
...

Error in this code

Workbooks("original.xls").Worksheets("sheet
5").Range("A10:LastRowWithData").Copy _
Destination:=Workbooks("new.xls").Worksheets("Shee t1").Range("A1")

should be

Workbooks("original.xls").Worksheets("sheet 5").Range("A10:" &
LastRowWithData).Copy _
Destination:=Workbooks("new.xls").Worksheets("Shee t1").Range("A1")



Also if you have one column that has an entry in each row then i
suggest you use


dim LastRowWithData
LastRowWithData = Range("a65536").End(xlUp).Row

' Create new workbook

Set NewBook = Workbooks.Add
With NewBook
SaveAs Filename:="new.xls"
End With


Workbooks("original.xls").Worksheets("sheet 5").Range("A10:" &
LastRowWithData).Copy _
Destination:=Workbooks("new.xls").Worksheets("Shee t1").Range("A1")


------------------------------------------------
~~ Message posted from http://www.ExcelTip.com/
~~View and post usenet messages directly from http://www.ExcelForum.com/



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Copy from active workbook to new workbook.


Thanks mudraker and Tom for replies.

I am still experiencing problems. No errors, but the data is not copin
to new workbook.
My code is now:

Dim LastRowWithData
LastRowWithData = Range("a65536").End(xlUp).Row

Set NewBook = Workbooks.Add
With NewBook
.SaveAs Filename:="new.xls"
End With

Workbooks("original.xls").Worksheets("Sheet 5").Range("A10:A" &
LastRowWithData).Copy _
Destination:=Workbooks("new.xls").Worksheets("Shee t1").Range("A1")

End Su

-----------------------------------------------
~~ Message posted from http://www.ExcelTip.com
~~View and post usenet messages directly from http://www.ExcelForum.com

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Copy from active workbook to new workbook.

this is troublesome:
With NewBook
SaveAs Filename:="new.xls"
End With

should be

With NewBook
.SaveAs Filename:="new.xls"
End With

or
NewBook.SaveAs Filename:="New.xls"

I am not sure what the unqualified SAVEAS would referto.

Other than that, I don't see a problem with your code if you are not getting
any errors and you don't have errors suppressed.

--
Regards,
Tom Ogilvy

"Ron" wrote in message
...

Thanks mudraker and Tom for replies.

I am still experiencing problems. No errors, but the data is not coping
to new workbook.
My code is now:

Dim LastRowWithData
LastRowWithData = Range("a65536").End(xlUp).Row

Set NewBook = Workbooks.Add
With NewBook
SaveAs Filename:="new.xls"
End With

Workbooks("original.xls").Worksheets("Sheet 5").Range("A10:A" &
LastRowWithData).Copy _
Destination:=Workbooks("new.xls").Worksheets("Shee t1").Range("A1")

End Sub


------------------------------------------------
~~ Message posted from http://www.ExcelTip.com/
~~View and post usenet messages directly from http://www.ExcelForum.com/



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 value from active sheet to another workbook sak New Users to Excel 2 June 19th 09 10:52 AM
Macro to copy active worksheet to new workbook Macca Excel Discussion (Misc queries) 1 May 25th 08 02:07 PM
I want to copy the active workbook name to a cell... trumb1mj Excel Discussion (Misc queries) 2 May 30th 06 06:30 PM
copy worksheet from closed workbook to active workbook using vba mango Excel Worksheet Functions 6 December 9th 04 07:55 AM
Preventing opening workbook inside active workbook. Serge[_4_] Excel Programming 2 November 4th 03 07:51 PM


All times are GMT +1. The time now is 04:17 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"