Combine 2 Macros - saving csv


New Member
Oct 12, 2010

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.
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)
    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
      x = x + 1
    End If

'New version saved
  MsgBox "New file version saved (version " & x & ")"

Exit Sub

'Error Handler
  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

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
                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)
    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.

Forum statistics

Latest member

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...