Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 274
Default Why doesn't this work?

Hi All,

The following code snippet is invoked from my personal.xls workbook.

What l am attempting to do is select any excel file in any directory
and then create a .xls file for each worksheet in the selected
workbook into a 'TEMP' directrory

The code is failing at the 'sh.Copy' line. Can anybody tell me what is
wrong please?

FileNameOnly & DirOnly are simply functions that l have used many
times to extract the relevant information from the string returned by
GetOpenFilename

Additionally l need to add some code to check if the 'TEMP' directory
has already exists, any ideas, example code would be gratefully
appreciated.

Sub CreateXLFiles()

Dim afile As String 'Source workbook to be rebuilt
Dim adir As String 'Directory of sheet files
Dim sh As Worksheet

afile = Application.GetOpenFilename(, , "Select the source
file", , False)
Application.ScreenUpdating = False
adir = (DirOnly(afile) & "\" & FileNameOnly(afile) & "-TEMP")
MkDir adir
Workbooks.Open afile, UpdateLinks:=False
For Each sh In Workbooks(FileNameOnly(afile)).Worksheets
sh.Copy <<<<<<<<<<<<<<<ERROR HERE
ActiveWorkbook.SaveAs adir & ActiveSheet.Name & ".xls"
ActiveWorkbook.Close SaveChanges:=False
Next
Workbooks(FileNameOnly(afile)).Close SaveChanges:=False

End Sub

Regards

Michael
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Why doesn't this work?

On Jul 25, 4:08*pm, "michael.beckinsale"
wrote:
Hi All,

The following code snippet is invoked from my personal.xls workbook.

What l am attempting to do is select any excel file in any directory
and then create a .xls file for each worksheet in the selected
workbook into a 'TEMP' directrory

The code is failing at the 'sh.Copy' line. Can anybody tell me what is
wrong please?

FileNameOnly & DirOnly are simply functions that l have used many
times to extract the relevant information from the string returned by
GetOpenFilename

Additionally l need to add some code to check if the 'TEMP' directory
has already exists, any ideas, example code would be gratefully
appreciated.

Sub CreateXLFiles()

* * Dim afile As String * * * * * 'Source workbook to be rebuilt
* * Dim adir As String * * * * * *'Directory of sheet files
* * Dim sh As Worksheet

* * afile = Application.GetOpenFilename(, , "Select the source
file", , False)
* * Application.ScreenUpdating = False
* * adir = (DirOnly(afile) & "\" & FileNameOnly(afile) & "-TEMP")
* * MkDir adir
* * Workbooks.Open afile, UpdateLinks:=False
* * For Each sh In Workbooks(FileNameOnly(afile)).Worksheets
* * * * sh.Copy *<<<<<<<<<<<<<<<ERROR HERE
* * * * ActiveWorkbook.SaveAs adir & ActiveSheet.Name & ".xls"
* * * * ActiveWorkbook.Close SaveChanges:=False
* * Next
* * Workbooks(FileNameOnly(afile)).Close SaveChanges:=False

End Sub

Regards

Michael


Hi - use the foll code to make sure a folder exists
Sub FolderExists()
Dim fso
Dim folder As String
folder = "C:\My Documents" ' change to match the folder path
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FolderExists(folder) Then
MsgBox folder & " is a valid folder/path.", vbInformation, "Path
Exists"
Else
MsgBox folder & " is not a valid folder/path.", vbInformation,
"Invalid Path"
End If
End Sub

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Why doesn't this work?

Hi Michael

Difficult to see your problem without seeing all the code

Try to adapt this example
http://www.rondebruin.nl/copy6.htm

--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"michael.beckinsale" wrote in message
...
Hi All,

The following code snippet is invoked from my personal.xls workbook.

What l am attempting to do is select any excel file in any directory
and then create a .xls file for each worksheet in the selected
workbook into a 'TEMP' directrory

The code is failing at the 'sh.Copy' line. Can anybody tell me what is
wrong please?

FileNameOnly & DirOnly are simply functions that l have used many
times to extract the relevant information from the string returned by
GetOpenFilename

Additionally l need to add some code to check if the 'TEMP' directory
has already exists, any ideas, example code would be gratefully
appreciated.

Sub CreateXLFiles()

Dim afile As String 'Source workbook to be rebuilt
Dim adir As String 'Directory of sheet files
Dim sh As Worksheet

afile = Application.GetOpenFilename(, , "Select the source
file", , False)
Application.ScreenUpdating = False
adir = (DirOnly(afile) & "\" & FileNameOnly(afile) & "-TEMP")
MkDir adir
Workbooks.Open afile, UpdateLinks:=False
For Each sh In Workbooks(FileNameOnly(afile)).Worksheets
sh.Copy <<<<<<<<<<<<<<<ERROR HERE
ActiveWorkbook.SaveAs adir & ActiveSheet.Name & ".xls"
ActiveWorkbook.Close SaveChanges:=False
Next
Workbooks(FileNameOnly(afile)).Close SaveChanges:=False

End Sub

Regards

Michael


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 247
Default Why doesn't this work?

On 25 Iul, 14:08, "michael.beckinsale"
wrote:
The code is failing at the 'sh.Copy' line. Can anybody tell me what is
wrong please?


Sub CreateXLFiles()


* * Dim sh As Worksheet


* * * * sh.Copy *<<<<<<<<<<<<<<<ERROR HERE
Michael


Maybe you Dim sh As Worksheet but forget to Set the sh :
Set sh = (your sheet) or Array(your sheets)

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 274
Default Why doesn't this work?

Hi All,

Many thanks for all your input.

The problem was caused by a 'hidden' sheet in the workbook l was using
for testing.

I have revised the code to test for visibilty and now all is OK.

Tausif - thanks for the code which l have now incorporated.

As an aside the next step is to import the created sheets in to new
workbook, the problem l am having is keeping the order of the sheets
the same as the original. Additionally l would like to delete the
sheets of the new workbook ie typically Sheet1, Sheet2, Sheet3

As you might have gathered l am creating a 'REBUILD' utility to help
reduce workbook bloat.

Any ideas gratefully received

Regards

Michael



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Why doesn't this work?

Maybe this will help you a bit
But there are many things to check (links for example that are not pointing to the correct workbook anymore)

Jan Karel Pieterse built one also and I think you can beta test it
Send JKP a mail (I believe his is on vacation now)

Sub Test()
Dim wb1 As Workbook
Dim wb2 As Workbook

Set wb1 = ActiveWorkbook
Set wb2 = Workbooks.Add(1)
wb2.Sheets(1).Name = "qqqqqqqqqwwwwwwwww"
wb1.Sheets.Copy after:=wb2.Sheets(wb2.Sheets.Count)

Application.DisplayAlerts = False
wb2.Sheets(1).Delete
Application.DisplayAlerts = True

End Sub


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"michael.beckinsale" wrote in message
...
Hi All,

Many thanks for all your input.

The problem was caused by a 'hidden' sheet in the workbook l was using
for testing.

I have revised the code to test for visibilty and now all is OK.

Tausif - thanks for the code which l have now incorporated.

As an aside the next step is to import the created sheets in to new
workbook, the problem l am having is keeping the order of the sheets
the same as the original. Additionally l would like to delete the
sheets of the new workbook ie typically Sheet1, Sheet2, Sheet3

As you might have gathered l am creating a 'REBUILD' utility to help
reduce workbook bloat.

Any ideas gratefully received

Regards

Michael


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
flash object dont work in my excel work sheet Nitn Excel Discussion (Misc queries) 0 July 4th 09 08:00 AM
HOW TO MAKE A LIST OF WORK SHEET IN WORK BOOK IN EXCEL 2007 goutam Excel Programming 1 February 1st 08 07:40 AM
If I have a work sheet protected and try to run a macro to hide rows or columns it won't work. Correct? Marc Excel Programming 2 July 12th 06 04:10 AM
Counting dates in multiple work sheets and work books Savage Excel Discussion (Misc queries) 0 December 19th 05 11:41 PM
Is there away to keep "auto save" from jumping to the first work sheet in the work book? Marc New Users to Excel 2 April 21st 05 01:27 AM


All times are GMT +1. The time now is 03:42 AM.

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"