VBA: Find column header and reformat (date) values in columns

fr0z3nfyr

New Member
Joined
Jun 28, 2013
Messages
14
I have VBA code to perform some actions on data in excel file and then convert all that data into semi-colon separated CSV/text file (code below).

Now, all I want is to add VBA code in the existing macro to find a column header (say, "Application date") and then convert all the dates into YYYY-MM-DD format. The original values in this column don't have a fixed date format.



Code:
[B]<code>Public Sub ExportToCsvFile(FName As String, _     Sep As String, SelectionOnly As Boolean, _     AppendDataOnExistingFile As Boolean)  Dim WholeLine As String Dim FNum As Integer Dim RowNdx As Long Dim ColNdx As Integer Dim FirstRow As Long Dim LastRow As Long Dim FirstCol As Integer Dim LastCol As Integer Dim CellValue As String   Application.ScreenUpdating = False On Error GoTo EndMacro: FNum = FreeFile  If SelectionOnly = True Then     With Selection         FirstRow = .Cells(1).Row         FirstCol = .Cells(1).Column         LastRow = .Cells(.Cells.Count).Row         LastCol = .Cells(.Cells.Count).Column     End With Else     With ActiveSheet.UsedRange         FirstRow = .Cells(1).Row         FirstCol = .Cells(1).Column         LastRow = .Cells(.Cells.Count).Row         LastCol = .Cells(.Cells.Count).Column     End With End If  If AppendDataOnExistingFile = True Then     Open FName For Append Access Write As #FNum Else     Open FName For Output Access Write As #FNum End If  For RowNdx = FirstRow To LastRow     WholeLine = ""     For ColNdx = FirstCol To LastCol         If Cells(RowNdx, ColNdx).Value = "" Then             CellValue = Chr(34) & Chr(34)         Else            CellValue = Cells(RowNdx, ColNdx).Value            CellValue = Replace(Replace(CellValue, Chr(150), Chr(45)), Chr(151), Chr(45))            CellValue = Replace(Replace(CellValue, Chr(60), Chr(60) & Chr(32)), Chr(10), "
")            CellValue = Chr(34) & Replace(CellValue, Chr(34), Chr(34) & Chr(34)) & Chr(34)         End If         WholeLine = WholeLine & CellValue & Sep     Next ColNdx     WholeLine = Left(WholeLine, Len(WholeLine) - Len(Sep))     Print #FNum, WholeLine Next RowNdx  EndMacro: On Error GoTo 0 Application.ScreenUpdating = True Close #FNum  End Sub  Sub ExportToSemiColonCsv()     Dim FileName As Variant     Dim Sep As String     FileName = Application.GetSaveAsFilename(InitialFileName:=vbNullString, FileFilter:="CSV Files (*.csv),*.csv")     If FileName = False Then         Exit Sub     End If      ExportToCsvFile FName:=CStr(FileName), Sep:=";", _        SelectionOnly:=False, AppendDataOnExistingFile:=True End Sub</code>[/B]
 
Last edited:

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Below is my VBA code, I'm sorry i pasted it directly from VB editor and didn't realise that it will be messed up like that.

Code:
Public Sub ExportToCsvFile(FName As String, _
    Sep As String, SelectionOnly As Boolean, _
    AppendDataOnExistingFile As Boolean)

Dim WholeLine As String
Dim FNum As Integer
Dim RowNdx As Long
Dim ColNdx As Integer
Dim FirstRow As Long
Dim LastRow As Long
Dim FirstCol As Integer
Dim LastCol As Integer
Dim CellValue As String


Application.ScreenUpdating = False
On Error GoTo EndMacro:
FNum = FreeFile

If SelectionOnly = True Then
    With Selection
        FirstRow = .Cells(1).Row
        FirstCol = .Cells(1).Column
        LastRow = .Cells(.Cells.Count).Row
        LastCol = .Cells(.Cells.Count).Column
    End With
Else
    With ActiveSheet.UsedRange
        FirstRow = .Cells(1).Row
        FirstCol = .Cells(1).Column
        LastRow = .Cells(.Cells.Count).Row
        LastCol = .Cells(.Cells.Count).Column
    End With
End If

If AppendDataOnExistingFile = True Then
    Open FName For Append Access Write As #FNum
Else
    Open FName For Output Access Write As #FNum
End If

For RowNdx = FirstRow To LastRow
    WholeLine = ""
    For ColNdx = FirstCol To LastCol
        If Cells(RowNdx, ColNdx).Value = "" Then
            CellValue = Chr(34) & Chr(34)
        Else
           CellValue = Cells(RowNdx, ColNdx).Value
           CellValue = Replace(Replace(CellValue, Chr(150), Chr(45)), Chr(151), Chr(45))
           CellValue = Replace(Replace(CellValue, Chr(60), Chr(60) & Chr(32)), Chr(10), "
")
           CellValue = Chr(34) & Replace(CellValue, Chr(34), Chr(34) & Chr(34)) & Chr(34)
        End If
        
        WholeLine = WholeLine & CellValue & Sep
    Next ColNdx
    WholeLine = Left(WholeLine, Len(WholeLine) - Len(Sep))
    Print #FNum, WholeLine
Next RowNdx

EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
Close #FNum

End Sub

Sub ExportToSemiColonCsv()
    Dim FileName As Variant
    Dim Sep As String
    FileName = Application.GetSaveAsFilename(InitialFileName:=vbNullString, FileFilter:="CSV Files (*.csv),*.csv")
    If FileName = False Then
        Exit Sub
    End If
    
    ExportToCsvFile FName:=CStr(FileName), Sep:=";", _
       SelectionOnly:=False, AppendDataOnExistingFile:=True
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,609
Messages
6,131,723
Members
449,667
Latest member
PSAv

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top