Hello again Chris,
I suggest that you use the following code in lieu of my previous post. I
found and fixed some problems with it and now it is also generic for the
number of worksheets that you have in the workbook to be copied.
Tested in xl2007 and xl2002. However, always saves new workbook as xl97-2003
format.
Sub CopyWorkBook()
Dim wbThisWb As Workbook
Dim wbCopyWb As Workbook
Dim strPath As String
Dim strNewFileName As String
Dim intDotPos As Integer
Dim strDirTest As String
Dim msgResponse
Dim intShtsCount As Integer
Dim shtArray()
Dim i As Integer
Set wbThisWb = ThisWorkbook
strPath = wbThisWb.Path & "\"
'Start of InputBox method**************************
strNewFileName = wbThisWb.Name
intDotPos = InStr(strNewFileName, ".")
strNewFileName = Left(strNewFileName, intDotPos - 1)
strNewFileName = Application.InputBox _
("Enter name for new file." & vbCrLf & _
"(May exclude the file extension.)", _
"Get Filename", "Copy of " & strNewFileName, 2)
'Remove .xlsm file extension if present
If InStr(1, strNewFileName, ".xlsm") > 0 Then
strNewFileName = Replace(strNewFileName, ".xlsm", "", 1)
End If
'Append .xls file extension if not present
If InStr(1, strNewFileName, ".xls") = 0 Then
strNewFileName = strNewFileName & ".xls"
End If
strDirTest = Dir(strPath & strNewFileName)
If strDirTest <> "" Then
msgResponse = MsgBox("Filename " & _
strNewFileName & " already exists." & _
vbCrLf & "Do you want to replace it?", _
vbYesNo)
If msgResponse = vbNo Then
MsgBox "Processing will terminate." & _
vbCrLf & _
"Run program again and enter" & _
" different filename."
Exit Sub
End If
End If
'End InputBox method*************************
'Can use following line in lieu of InputBox
'strNewFileName = "Copy of Master.xls"
With wbThisWb
intShtsCount = .Sheets.Count
ReDim shtArray(0 To intShtsCount - 1)
For i = 0 To intShtsCount - 1
shtArray(i) = (i + 1)
Next i
.Sheets(shtArray).Select
.Sheets(1).Activate
.Sheets(shtArray).Copy
End With
Set wbCopyWb = ActiveWorkbook
With wbCopyWb
.Sheets(shtArray).Select
.Sheets(1).Activate
Cells.Select
Selection.Copy
Selection.PasteSpecial _
Paste:=xlPasteValues, _
Operation:=xlNone, _
SkipBlanks:=False, _
Transpose:=False
Range("A1").Select
.Sheets(1).Select
Application.CutCopyMode = False
Application.DisplayAlerts = False
.SaveAs Filename:= _
strPath & strNewFileName, _
FileFormat:=xlNormal, Password:="", _
WriteResPassword:="", _
ReadOnlyRecommended:=False, _
CreateBackup:=False
Application.DisplayAlerts = True
.Close
End With
With wbThisWb
.Sheets(1).Select
Range("A1").Select
End With
End Sub