Hello Everyone
i would like to save single sheet named (Sample test) to CSV from current workbook
i already find this code online but its for active sheet that want to change to specific name of sheet and name of CSV workbook is automatic date and time of creation and sheet inside this workbook is the same sheet for original file (sample test)
Option Explicit
Public Sub ExcelRowsToCSV()
Dim iPtr As Integer
Dim sFileName As String
Dim intFH As Integer
Dim aRange As Range
Dim iLastColumn As Integer
Dim oCell As Range
Dim iRec As Long
Set aRange = Range("A1:E97") ' in case you need specific range other than you can use the following line to select range wanted
' Set aRange = Application.InputBox("Select a range:-", , Selection.Address, , , , , Type:=8)
iLastColumn = aRange.Column + aRange.Columns.Count - 1
iPtr = InStrRev(ActiveWorkbook.FullName, ".")
sFileName = Left(ActiveWorkbook.FullName, iPtr - 1) & ".csv"
sFileName = Application.GetSaveAsFilename(InitialFileName:=sFileName, FileFilter:="CSV (Comma delimited) (*.csv), *.csv")
If sFileName = "False" Then Exit Sub
Close
intFH = FreeFile()
Open sFileName For Output As intFH
iRec = 0
For Each oCell In aRange
If oCell.Column = iLastColumn Then
Print #intFH, oCell.Value
iRec = iRec + 1
Else
Print #intFH, oCell.Value; ",";
End If
Next oCell
Close intFH
MsgBox "Finished: " & CStr(iRec) & " records written to " _
& sFileName & Space(10), vbOKOnly + vbInformation
End Sub
and how can combine this code after modification with the following code in one button
Sub Button1_Click()
'
' Macro1 Macro
'
'
Application.DisplayAlerts = False
Worksheets("Sample test").Delete
Application.DisplayAlerts = True
Sheets.Add.Name = "Sample test"
Worksheets("Generator").Visible = xlSheetVisible
Sheets("Generator").Select
Range("A1:E97").Select
Selection.Copy
Worksheets("Generator").Visible = xlSheetHidden
Sheets("Sample test").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("E:E").EntireColumn.AutoFit
Columns("D:D").EntireColumn.AutoFit
Columns("C:C").ColumnWidth = 8.18
Columns("C:C").EntireColumn.AutoFit
Columns("B:B").EntireColumn.AutoFit
Columns("A:A").EntireColumn.AutoFit
Application.CutCopyMode = False
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
Columns("D:E").Select
Selection.NumberFormat = "yyyy-mm-dd;@"
End With
End Sub
Bookmarks