I've been searching and searching for something that is probably simple but I can't seem to find the answer and I don't know enough about VBA myself to figure it out on my own. This macro was initially created by someone else in our company (no idea who) and I'm trying to craft it to better fit what I need it to do. Below is what I have now. The macro creates a CSV file from a worksheet of formulas pulling data from another worksheet. The formulas return blanks ("") for cells in rows with zeroes or no real data. I need the CSV to contain none of those rows with no data.
I think the only successful addition I've made to the original Macro was to skip rows with no data, though I guess that's not really even that successful since it still returns empty rows on the CSV and it turns out I actually need them deleted altogether.
Here's a simple example of an output CSV, followed by how I need it to be.
I think the only successful addition I've made to the original Macro was to skip rows with no data, though I guess that's not really even that successful since it still returns empty rows on the CSV and it turns out I actually need them deleted altogether.
VBA Code:
Public Sub Upload_Converter()
Const DELIMITER As String = "|"
Dim LastRow As Long
Dim myField As String
Dim nFileNum As Long
Dim sOut As String
Dim FilePath As String
Dim dte As String
dte = InputBox("Please Enter Event Date (mm/dd/yyyy Format): ", Default:=Format(Now, "mm/dd/yyyy"))
FilePath = ActiveWorkbook.Path & "\AR_Entry_" & Format(dte, "yyyymmdd") & "_" & Format(Now(), "hh24mmss") & ".csv"
LastRow = ActiveSheet.UsedRange.Rows.Count
sOut = Empty
nFileNum = FreeFile
Open FilePath For Output As #nFileNum
For i = 2 To LastRow
For j = 1 To 7
If j = 1 Then
myField = Cells(i, j).Value
' myField = Right("0000000000" & myField, 10)
sOut = sOut & myField
' ElseIf j = 3 Then
' myField = Format(Cells(i, j).Value, "mm/dd/yyyy")
ElseIf j = 7 Then
myField = Format(Cells(i, j).Value, "###0.000")
If IsEmpty(Cells(i, j).Value) Then
myField = Format(0, "###0.000")
End If
sOut = sOut & DELIMITER & myField
Else
myField = Cells(i, j).Value
sOut = sOut & DELIMITER & myField
End If
Next j
Print #nFileNum, Mid(sOut, 1)
sOut = Empty
Next i
Close #nFileNum
MsgBox ("File is converted - reference file AR_Entry.csv in " & FilePath)
End Sub
Here's a simple example of an output CSV, followed by how I need it to be.