Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 82
Default Why is worksheet.usedrange empty?

Hi,
I want to copy the data in one worksheet into an other worksheet in a new
workbook.
This is my code:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Worksheets("Sheet1").Activate
ActiveSheet.UsedRange.Select
Selection.Copy
Workbooks.Add
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveWorkbook.SaveAs Filename:="C:\NewCopiedBook.xls", FileFormat:= _
xlNormal, Password:="", WriteResPassword:="",
ReadOnlyRecommended:=False _
, CreateBackup:=False
ActiveWindow.Close

I can see the cells being selected and copied. And a new workbook created
and the cells being pasted. And then the new workbook being closed.
But when I open it there is no data in the cells.

When i debug I can see that the worksheet.usedrange is empty.
The data in the "original" workbook from has just been pasted, probably not
saved. But I tried to save the "original" workbook before the code above.

What am I missing?

/Sara
  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 35,218
Default Why is worksheet.usedrange empty?

I think excel gets confused with the activeworkbook or activesheet.

In either case, I'd do:

Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim NewWkbk As Workbook

Set NewWkbk = Workbooks.Add(1) 'single sheet

Me.Worksheets("Sheet1").UsedRange.Copy _
Destination:=NewWkbk.Worksheets(1).Range("a1")

NewWkbk.SaveAs Filename:="C:\NewCopiedBook.xls", _
FileFormat:=xlNormal, Password:="", _
WriteResPassword:="", ReadOnlyRecommended:=False, _
CreateBackup:=False

NewWkbk.Close savechanges:=False

'uncomment the next line when you're testing
'Cancel = True
End Sub

Sara wrote:

Hi,
I want to copy the data in one worksheet into an other worksheet in a new
workbook.
This is my code:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Worksheets("Sheet1").Activate
ActiveSheet.UsedRange.Select
Selection.Copy
Workbooks.Add
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveWorkbook.SaveAs Filename:="C:\NewCopiedBook.xls", FileFormat:= _
xlNormal, Password:="", WriteResPassword:="",
ReadOnlyRecommended:=False _
, CreateBackup:=False
ActiveWindow.Close

I can see the cells being selected and copied. And a new workbook created
and the cells being pasted. And then the new workbook being closed.
But when I open it there is no data in the cells.

When i debug I can see that the worksheet.usedrange is empty.
The data in the "original" workbook from has just been pasted, probably not
saved. But I tried to save the "original" workbook before the code above.

What am I missing?

/Sara


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 82
Default Why is worksheet.usedrange empty?


Thanks a LOT!!!
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
empty box/worksheet johnalexwood Excel Discussion (Misc queries) 5 January 26th 07 05:56 PM
Excel 2007 BUG UsedRange/LastCell differences with Excel2003. keepITcool Excel Discussion (Misc queries) 2 May 31st 06 06:18 PM
Real Value of .UsedRange.Rows.Count dazman Excel Worksheet Functions 2 August 25th 05 03:24 PM
How do I delete an empty worksheet. Jimmy Excel Worksheet Functions 10 February 3rd 05 05:43 PM
empty rows at bottom of worksheet michael g Excel Discussion (Misc queries) 2 January 18th 05 09:17 PM


All times are GMT +1. The time now is 10:15 PM.

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

About Us

"It's about Microsoft Excel"