Macro export naar csv komma gescheiden

Status
Niet open voor verdere reacties.

Tweety1

Gebruiker
Lid geworden
6 mrt 2013
Berichten
637
Onderstaande macro gebruik ik om een excel bestand op te slaan als csv bestand met komma.
Nu zou ik deze graag willen aanpassen
Export ziet er nu uit als: Test1,Test2,Test3
Dit wil ik veranderen in "Test1","Test2","Test3"

Code:
Option Private Module

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' ExportToTextFile
' This exports a sheet or range to a text file, using a
' user-defined separator character.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Sub ExportToTextFile(FName As String, _
    Sep As String, SelectionOnly As Boolean, _
    AppendData As Boolean)

Dim WholeLine As String
Dim FNum As Integer
Dim RowNdx As Long
Dim ColNdx As Integer
Dim StartRow As Long
Dim EndRow As Long
Dim StartCol As Integer
Dim EndCol As Integer
Dim CellValue As String


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

If SelectionOnly = True Then
    With Selection
        StartRow = .Cells(1).Row
        StartCol = .Cells(1).Column
        EndRow = .Cells(.Cells.Count).Row
        EndCol = .Cells(.Cells.Count).Column
    End With
Else
    With ActiveSheet.UsedRange
        StartRow = .Cells(1).Row
        StartCol = .Cells(1).Column
        EndRow = .Cells(.Cells.Count).Row
        EndCol = .Cells(.Cells.Count).Column
    End With
End If

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

For RowNdx = StartRow To EndRow
    WholeLine = ""
    For ColNdx = StartCol To EndCol
        If Cells(RowNdx, ColNdx).Value = "" Then
            CellValue = Chr(34) & Chr(34)
        Else
           CellValue = Cells(RowNdx, ColNdx).Value
        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


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' END ExportTextFile
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub DoTheExportVervangen()

   ExportToTextFile FName:=ThisWorkbook.Path & "\Test.csv", Sep:=",", _
       SelectionOnly:=False, AppendData:=False
End Sub

mvg

Kasper
 
Wijzig deze regel:
WholeLine = WholeLine & CellValue & Sep

In dit:
WholeLine = WholeLine & Chr(34) & CellValue & Chr(34) & Sep
 
of:

Code:
Sub M_snb()
   Cells(1).CurrentRegion.Copy
  
   With GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
      .GetFromClipboard
      CreateObject("scripting.filesystemobject").createtextfile("G:\OF\bewijs.csv").write Chr(34) & Replace(Replace(.GetText, vbTab, Chr(34) & ";" & Chr(34)), vbCrLf, Chr(34) & vbCrLf & Chr(34))
      .Clear
    End With
End Sub
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan