ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Please help in fixing my Macro (https://www.excelbanter.com/excel-programming/450726-please-help-fixing-my-macro.html)

Gaura215

Please help in fixing my Macro
 
Hi All Excel Gurus
I need a little help, I got following code from a website.I modified this to my use and is working exactly fine except a very little problem. this macro creates new workbooks for me. Can someone help me how can i rename the worksheet in the new generated workbook as "Data" by default. Any help is highly appreciated:

Sub sheetsaver()
'Working in 97-2013
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Sourcewb As Workbook
Dim Destwb As Workbook
Dim sh As Worksheet
Dim DateString As String
Dim FolderName As String
With Application
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With
'Copy every sheet from the workbook with this macro
Set Sourcewb = ActiveWorkbook
'Create new folder to save the new files in
DateString = Format(Now, "dd-mm-yyyy hhmmss")
'FolderName = Sourcewb.Path & "\" & Sourcewb.Name & " " & DateString
FolderName = Sourcewb.Path & "\" & "Reconlite Input Files" & " " & DateString

Set FSO = CreateObject("Scripting.FileSystemObject")
If Not FSO.FolderExists(FolderName) Then

MkDir FolderName
End If

'Copy every visible sheet to a new workbook
For Each sh In Sourcewb.Worksheets
'If the sheet is visible then copy it to a new workbook
If sh.Visible = -1 Then
sh.Copy
'Set Destwb to the new workbook
Set Destwb = ActiveWorkbook
'Determine the Excel version and file extension/format
With Destwb
If Val(Application.Version) < 12 Then
'You use Excel 97-2003
FileExtStr = ".xls": FileFormatNum = -4143
Else
'You use Excel 2007-2013
If Sourcewb.Name = .Name Then
MsgBox "Your answer is NO in the security dialog"
GoTo GoToNextSheet
Else
Select Case Sourcewb.FileFormat
Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
Case 52:
If .HasVBProject Then
FileExtStr = ".xlsm": FileFormatNum = 52
Else
FileExtStr = ".xlsx": FileFormatNum = 51
End If
Case 56: FileExtStr = ".xls": FileFormatNum = 56
Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
End Select
End If
End If
End With
'Change all cells in the worksheet to values if you want
If Destwb.Sheets(1).ProtectContents = False Then
With Destwb.Sheets(1).UsedRange
.Cells.Copy
.Cells.PasteSpecial xlPasteValues
.Cells(1).Select
End With
Application.CutCopyMode = False
End If

'Save the new workbook and close it
With Destwb
.SaveAs FolderName _
& "\" & Destwb.Sheets(1).Name & Format(Now - 30, "mmyyyy") & FileExtStr, _
FileFormat:=FileFormatNum
.Close False
End With

End If
GoToNextSheet:
Next sh
ActiveWorkbook.Close

GS[_2_]

Please help in fixing my Macro
 
Try in a standard module...

Option Explicit

Sub SheetSaver2() 'Working in 97-2013
'Copy every sheet from the workbook with this macro
Dim sFileExt$, sDate$, sFolder$
Dim lFileFormat&, lCalcMode&
Dim wkbSource As Workbook, wkbTarget As Workbook
Dim wks, wksTarget As Worksheet

'Create new folder to save the new files in
sDate = Format(Now, "dd-mm-yyyy_hhmmss")
Set wkbSource = ActiveWorkbook
'sFolder = wkbSource.Path & "\" & wkbSource.Name & " " & sDate
sFolder = wkbSource.Path & "\" _
& "Reconlite_Input_Files_" & sDate
If Dir$(sFolder & "\nul") = "" Then MkDir sFolder

With Application
.ScreenUpdating = False: .EnableEvents = False
lCalcMode = .Calculation: .Calculation = xlCalculationManual
End With
On Error GoTo ErrExit

For Each wks In wkbSource.Worksheets
'Copy every visible sheet to a new workbook
If wks.Visible Then
'Set fully qualified refs to the new worksheet/workbook
wks.Copy: Set wksTarget = ActiveSheet
Set wkbTarget = wksTarget.Parent

'Determine the Excel version and file extension/format
If Val(Application.Version) < 12 Then 'Excel 97-2003
sFileExt = ".xls": lFileFormat = -4143
Else 'Excel 2007 and later
'================================================= =====
'At this point wkbTarget has not been named,
'NOR would XL assign the same name as an open file,
'NOR would XL open 2 files with same name.
'Actual name should be "Book#" and so the following
'makes absolutely no sense!
' If wkbSource.Name = .Name Then
' MsgBox "Your answer is NO in the security"
'' Dialog "" '???what is this???
' GoTo GoToNextSheet
' Else
'================================================= =====
Select Case wkbSource.FileFormat
Case 51: sFileExt = ".xlsx": lFileFormat = 51
Case 52:
If wkbTarget.HasVBProject Then
sFileExt = ".xlsm": lFileFormat = 52
Else
sFileExt = ".xlsx": lFileFormat = 51
End If
Case 56: sFileExt = ".xls": lFileFormat = 56
Case Else: sFileExt = ".xlsb": lFileFormat = 50
End Select
'================================================= =====
' End If
'================================================= =====
End If 'Val(Application.Version) < 12

With wksTarget
.Name = "Data"
'Change cell contents to values
If Not .ProtectContents Then
.UsedRange.Value = .UsedRange.Value
End If
End With 'wksTarget

'Save the new workbook and close it
With wkbTarget
.SaveAs sFolder & "\" & wksTarget.Name _
& Format(Now - 30, "mmyyyy") & sFileExt, _
FileFormat:=lFileFormat
.Close True
End With 'wkbTarget

End If 'wks.Visible
'================================================= =====
'GoToNextSheet:
'================================================= =====
Next 'wks
wkbSource.Close

ErrExit:
'Restore Application settings
With Application
.ScreenUpdating = True: .EnableEvents = True
.Calculation = lCalcMode
End With 'Application

'Release memory reserved for objects created
Set wkbSource = Nothing: Set wkbTarget = Nothing
End Sub

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion




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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com