Sub ExportCSV()
Dim F As Object, Prvy As Integer, Ostatne As Integer, Riadkov As Long, i As Long, x As Integer, rngUsed As Range, D(), S As String
Const DEL = ";"
With ActiveSheet
Prvy = .Cells(1, .Columns.Count).End(xlToLeft).Column
Set rngUsed = .UsedRange
Ostatne = Intersect(rngUsed, rngUsed.Offset(1, 0)).Columns.Count
With rngUsed
Riadkov = .Rows.Count
ReDim D(1 To Riadkov, 1 To .Columns.Count)
D = .Value
End With
End With
Set F = CreateObject("Scripting.FileSystemObject").CreateTextFile("D:\CSVExport.csv", True, True)
For i = 1 To Riadkov
S = vbNullString
For x = 1 To IIf(i = 1, Prvy, Ostatne)
S = S & IIf(S = vbNullString, vbNullString, DEL) & D(i, x)
Next x
F.Writeline S
Next i
Set F = Nothing: Set rngUsed = Nothing
End Sub
Sub ExportCSV2()
Dim Prvy As Integer, Ostatne As Integer, Riadkov As Long, i As Long, x As Integer, rngUsed As Range, D(), S As String, Pole() As String
Const DEL = ";"
With ActiveSheet
Prvy = .Cells(1, .Columns.Count).End(xlToLeft).Column
Set rngUsed = .UsedRange
Ostatne = Intersect(rngUsed, rngUsed.Offset(1, 0)).Columns.Count
With rngUsed
Riadkov = .Rows.Count
ReDim D(1 To Riadkov, 1 To .Columns.Count)
D = .Value
End With
End With
ReDim Pole(Riadkov - 1)
For i = 1 To Riadkov
S = vbNullString
For x = 1 To IIf(i = 1, Prvy, Ostatne)
S = S & IIf(S = vbNullString, vbNullString, DEL) & D(i, x)
Next x
Pole(i - 1) = S
Next i
With CreateObject("ADODB.Stream")
.Open
.Charset = "UTF-8"
.WriteText Join(Pole, vbCrLf)
.SaveToFile "D:\CSVExport.csv", 2
.Close
End With
Set rngUsed = Nothing
End Sub
Má to ale jedno podstatné ALE. Už X-krát omieľané kódovanie UTF-8 v CSV a BOM. Prvý kód neurobí UTF-8 ale nejaké "UCS-2 LE BOM". Druhý kód urobí "UTF-8 BOM". To BOM sú 3 bajty v súbore, ktoré robia šarapatu pri importoch do rôznych systémov. Vyskúšajte, alebo sa inšpirujte pri hľadaní iného riešenia.
Možno klasickým exportom urobiť CSV, to v makre otvoriť ako text, zmazať v prvom riadku všetky ";" sprava po najbližší znak a súbor znovu uložiť. Ale zase to ALE, lebo ak by sa Vám aj podaril Export do UTF-8 bez BOM, tak takéto uloženie to opäť degraduje.
Možno má niekto niečo použiteľnejšie.citovat