I using this function to export a data range in CSV:
Sub Fct_Export_CSV()
Dim Value As String
Dim size As Integer
Value = ThisWorkbook.Path & "\Export_" & Sheets(1).range("B20").Value & ".csv"
chemincsv = Value
Worksheets("Database").Select
Dim Plage As Object, oL As Object, oC As Object, Tmp As String, Sep$
Sep = ";"
size = Worksheets("Database").range("B" & Rows.Count).End(xlUp).Row
Set Plage = ActiveSheet.range("A1:G" & size)
Open chemincsv For Output As #1
For Each oL In Plage.Rows
Tmp = ""
For Each oC In oL.Cells
Tmp = Tmp & CStr(oC.Text) & Sep
Next
Print #1, Tmp
Next
Close
MsgBox "OK! Export to " & Value
End Sub
Export works but i have one too ";" at the end of each line.
Example export CSV:
Folder;Tree;Group;FirstName;LastName;sAMAccountName;Rules;
FOLDER;TREE;DL-FOLDER-R;John;TOTO;JTOT;R;
Where do i modify this VBA script to generate a CSV without ";" at the end ?
My goal:
Folder;Tree;Group;FirstName;LastName;sAMAccountName;Rules
FOLDER;TREE;DL-FOLDER-R;John;TOTO;JTOT;R
There are multiple ways to accomplish this. For eg. you could put a if condition in Tmp = Tmp & CStr(oC.Text) & Sep
to not append sep if the iteration is the last one.
Another way would be to use left function. Without modifying much of your code, I think this would be a good approach. Here is a sample code(havnt tried):
Sub Fct_Export_CSV()
Dim Value As String
Dim size As Integer
Value = ThisWorkbook.Path & "\Export_" & Sheets(1).range("B20").Value & ".csv"
chemincsv = Value
Worksheets("Database").Select
Dim Plage As Object, oL As Object, oC As Object, Tmp As String, Sep$
Sep = ";"
size = Worksheets("Database").range("B" & Rows.Count).End(xlUp).Row
Set Plage = ActiveSheet.range("A1:G" & size)
Open chemincsv For Output As #1
For Each oL In Plage.Rows
Tmp = ""
For Each oC In oL.Cells
Tmp = Tmp & CStr(oC.Text) & Sep
Next
'take one less than length of the string number of characters from left, that would eliminate the trailing semicolon
Tmp = left(Tmp, Len(Tmp)-1)
Print #1, Tmp
Next
Close
MsgBox "OK! Export to " & Value
End Sub
Added one line of code before the last print statement, should work. lemme know.
如果您在将分号设置为本地分隔符的机器上工作,则可以将工作表另存为csv并指定Local:=True
Worksheets("Database").SaveAs Filename:=Value, FileFormat:=xlCSV, Local:=True
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.