dice1976
New Member
- Joined
- Oct 12, 2010
- Messages
- 4
Hello,
I have 2 macros that are working but I need to have it be done in a single macro. I can not figure out where to merge them. I'm hoping someone can help.
The first macro saves a new csv file name with the next version number counting up.
The second, script I have more or less does the same thing as above but looks to the excel sheet and only saves the lines that have actual values in it and not the cells that show a formula therefore keeping the extra ,,,, off my csv file
What I am looking to do is utilize the first code and insert the 2nd code to only save the lines that actually have values in them and ignore the cells that are blank but contain a formula.
I have 2 macros that are working but I need to have it be done in a single macro. I can not figure out where to merge them. I'm hoping someone can help.
The first macro saves a new csv file name with the next version number counting up.
Code:
Sub SaveNewVersion_Excel()
'DEFINITION: Save file as csv, if already exists add a new version number filename
Dim FolderPath As String
Dim myPath As String
Dim SaveName As String
Dim SaveExt As String
Dim VersionExt As String
Dim Saved As Boolean
Dim x As Long
TestStr = ""
Saved = False
x = 2
'Version Indicator (change to liking)
VersionExt = "_v"
'Pull info about file
On Error GoTo NotSavedYet
myPath = ActiveWorkbook.FullName
myFileName = Mid(myPath, InStrRev(myPath, "\") + 1, InStrRev(myPath, ".") - InStrRev(myPath, "\") - 1)
FolderPath = Left(myPath, InStrRev(myPath, "\"))
SaveExt = "." & "csv"
On Error GoTo 0
'Determine Base File Name
If InStr(1, myFileName, VersionExt) > 1 Then
myArray = Split(myFileName, VersionExt)
SaveName = myArray(0)
Else
SaveName = myFileName
End If
'Test to see if file name already exists
If FileExist(FolderPath & SaveName & SaveExt) = False Then
ActiveWorkbook.SaveAs FolderPath & SaveName & SaveExt, FileFormat:=xlCSV
Exit Sub
End If
'Need a new version made
Do While Saved = False
If FileExist(FolderPath & SaveName & VersionExt & x & SaveExt) = False Then
ActiveWorkbook.SaveAs FolderPath & SaveName & VersionExt & x & SaveExt, FileFormat:=xlCSV
Saved = True
Else
x = x + 1
End If
Loop
'New version saved
MsgBox "New file version saved (version " & x & ")"
Exit Sub
'Error Handler
NotSavedYet:
MsgBox "This file has not been initially saved. " & _
"Cannot save a new version!", vbCritical, "Not Saved To Computer"
End Sub
The second, script I have more or less does the same thing as above but looks to the excel sheet and only saves the lines that have actual values in it and not the cells that show a formula therefore keeping the extra ,,,, off my csv file
Code:
Option Explicit
Sub CreateCSV()
Dim ws As Worksheet
Dim rng As Range
Dim strFileName As String
Dim FF As Long
Dim NoVals As Long
Dim arrVals
Set ws = Worksheets("Sheet1")
Set rng = Range("A1")
strFileName = ws.Name & ".csv"
FF = FreeFile()
Open "C:\_Adam\-_- Move to Box\INV Demo\" & strFileName For Output As #FF
While rng.Value <> ""
NoVals = Application.WorksheetFunction.CountA(rng.EntireRow)
If NoVals = 1 Then
Print #FF, rng.Value
Else
arrVals = rng.Resize(, NoVals).Value
arrVals = Application.WorksheetFunction.Transpose(arrVals)
arrVals = Application.WorksheetFunction.Transpose(arrVals)
Print #FF, Join(arrVals, ",")
End If
Set rng = rng.Offset(1)
Wend
Close #FF
End Sub
What I am looking to do is utilize the first code and insert the 2nd code to only save the lines that actually have values in them and ignore the cells that are blank but contain a formula.