PC Review


Reply
Thread Tools Rate Thread

All Workbook Saved except This Workbook - Part2

 
 
Len
Guest
Posts: n/a
 
      13th Aug 2010
Hi,

I tried to find another alternative codes to achieve the same result
with more efficient and time saving.
I decided to replace the following codes in the earlier post of "All
Workbooks Saved except This Workbook" with For Next Loop and it works
perfectly when there is only 2 excel files ( ie 1 open workbook plus
open workbook containing codes )

However, after working around several times and yet it fails with
error message " Subscript Out of Range" when there are more than 3
excel files ( ie > 1 open workbook plus open workbook containing
codes )

Sub Button2_Click()
Dim WB As Workbook
Dim i As Long
For i = 1 To Workbooks.Count
If Workbooks.Count > 1 Then
If (Workbooks(i).Name <> ThisWorkbook.Name) Then <--------
Error " Subscript Out of Range"
With Workbooks(i)
.Sheets(1).Activate
SheetName = .ActiveSheet.Name
FileExtStr = ".xls": FileFormatNum = 56
.SaveAs Filename:=SheetName & FileExtStr, _
FileFormat:=FileFormatNum
.Close SaveChanges:=True
End With
End If

Else
MsgBox "Only 1 Open File"
Exit Sub
End If
Next

MsgBox "All Workbooks Saved under this Directory Folder "

End Sub

Any Helps will be much appreciated and thanks in advance

Regards
Len
 
Reply With Quote
 
 
 
 
Jim Cone
Guest
Posts: n/a
 
      13th Aug 2010

If you start with 3 workbooks and close one of them, then workbook 3 doesn't exist any more.
Excel will try to find it and throw an error.

Good programming practice: Add "Option Explicit" as the first line in all modules.
It forces variable declaration.
'--
Sub Button2_Click()
Dim WB As Workbook
Dim SheetName As String
Dim FileExtStr As String
Dim FileFormatNum As Long

If Workbooks.Count > 1 Then
For Each WB In Workbooks
If WB.Name <> ThisWorkbook.Name Then
WB.Sheets(1).Activate
SheetName = WB.Name
FileExtStr = ".xls": FileFormatNum = 56
WB.SaveAs Filename:=SheetName & FileExtStr, _
FileFormat:=FileFormatNum
WB.Close SaveChanges:=True
End If
Next
Else
MsgBox "Only 1 Open File"
Exit Sub
End If
MsgBox "All Open Workbooks Saved and Closed"
End Sub
--
Jim Cone
Portland, Oregon USA
http://www.mediafire.com/PrimitiveSoftware

..
..
..

"Len" <(E-Mail Removed)>
wrote in message
news:7629062d-ceac-487d-9d4b-(E-Mail Removed)...
Hi,
I tried to find another alternative codes to achieve the same result
with more efficient and time saving.
I decided to replace the following codes in the earlier post of "All
Workbooks Saved except This Workbook" with For Next Loop and it works
perfectly when there is only 2 excel files ( ie 1 open workbook plus
open workbook containing codes )

However, after working around several times and yet it fails with
error message " Subscript Out of Range" when there are more than 3
excel files ( ie > 1 open workbook plus open workbook containing
codes )

Sub Button2_Click()
Dim WB As Workbook
Dim i As Long
For i = 1 To Workbooks.Count
If Workbooks.Count > 1 Then
If (Workbooks(i).Name <> ThisWorkbook.Name) Then <--------
Error " Subscript Out of Range"
With Workbooks(i)
.Sheets(1).Activate
SheetName = .ActiveSheet.Name
FileExtStr = ".xls": FileFormatNum = 56
.SaveAs Filename:=SheetName & FileExtStr, _
FileFormat:=FileFormatNum
.Close SaveChanges:=True
End With
End If

Else
MsgBox "Only 1 Open File"
Exit Sub
End If
Next
MsgBox "All Workbooks Saved under this Directory Folder "
End Sub

Any Helps will be much appreciated and thanks in advance
Regards
Len
 
Reply With Quote
 
Jim Cone
Guest
Posts: n/a
 
      13th Aug 2010
Further...

When deleting items from a collection, it is usually best to work backwards, from the end back to the start.
Your code should work as is just by changing...
For i = 1 To Workbooks.Count
-to-
For i = Workbooks.Count to 1 Step -1
--
Jim Cone
Portland, Oregon USA

 
Reply With Quote
 
Len
Guest
Posts: n/a
 
      13th Aug 2010
Hi Jim,

Thanks for your advice and your codes
After amending the codes and run it, Great !, it works perfectly

Regards
Len
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
All Workbook Saved except This Workbook Len Microsoft Excel Programming 3 12th Aug 2010 03:07 AM
Why is Referenced Workbook Saved When ActiveWorkbook is Saved? RyanH Microsoft Excel Programming 1 29th Oct 2008 08:20 PM
Save column J of WorkBook when WorkBook is not saved. mikeburg Microsoft Excel Programming 2 14th Jun 2006 09:57 PM
Running a macro to protect a workbook on a already protected workbook UNprotects the workbook ?? WimR Microsoft Excel Programming 9 25th Jul 2005 12:44 PM
Saved Workbook and the Whole workbook is now Protected Beth Microsoft Excel New Users 1 16th Jun 2004 03:27 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:19 AM.