Condividi tramite

Creare un file .csv UTF-8 conVBA in Excel

Anonimo
2024-09-13T20:08:58+00:00

Buongiorno a tutte e a tutti,

Ho il seguente problema:

Ho creato un file .xlsx al quale ho aggiunto del codice VBA (quindi salvato come .xlsm) attraverso il quale viene creato un file .csv.

Il codice funziona, ma il formato finale desiderato NON È CORRETTO.

Se provvedo a creare il file .csv manualmente (File, Salva come, .csv UTF-8), l'esportazione funziona bene. Per esempio il nome “Jürg Müller” rimane tale e quale nel file .csv.

Invece il file creato tramite codice VBA il risultato è “Jrg Mller

Impiego Excel per Mac, ma il codice dovrebbe funzionare anche su Windows.

Qualcuno può aiutarmi per favore?

GRAZIE MILLE.

Qui il codice VBA che ho usato. In grassetto le parti relative alla creazione del file .csv.

Sub ExportToCSV()

    Dim savePath As String

    Dim csvFileName As String

    Dim currentFilePath As String

    Dim ws As Worksheet

    Dim lastRow As Long

    Dim dataComplete As Boolean

    Dim rowCounter As Long

    Dim col As Integer

    ' current file path

    currentFilePath = ThisWorkbook.Path

    Set .CSV file name

csvFileName = Format(Now(), "yyyy - mm-dd - HH-mm-ss") & " - users list.csv"

    ' Set active worksheet

    Set ws = ThisWorkbook.ActiveSheet

    ' Determine last row with data in column A

    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

    ' Initialize flag dataComplete to True

    dataComplete = True

    ' Cicle on rows from 5 to last with data

    For rowCounter = 5 To lastRow

        ' Check all columns from A to L for current row

        For col = 1 To 12 ' From 1 to 12 represent columns from A to L

            If IsEmpty(ws.Cells(rowCounter, col)) Or Trim(ws.Cells(rowCounter, col).Value) = "" Then

                ' If a cell is empty, set dataComplete to False and exit from cicle

                dataComplete = False

                Exit For ' Exit from the cycle For col

            End If

        Next col

        ' If a row is not complete, exit from cycle

        If Not dataComplete Then Exit For

    Next rowCounter

    ' If the data are not complete, show a message and stop the procedure

    If Not dataComplete Then

        MsgBox "Verify data completeness to proceed", vbExclamation

        Exit Sub

    End If

    If the data are complete, proceed with the export to CSV

    ' Delete the “Sheet2” sheet if it exists

    On Error Resume Next

    Application.DisplayAlerts = False ' Disable warning messages

    ThisWorkbook.Sheets("Sheet2").Delete

    Application.DisplayAlerts = True ' Ensable warning messages

    On Error GoTo 0

    ' Create a temporary copy of the active sheet

    ThisWorkbook.ActiveSheet.Copy

    Set ws = ActiveSheet

    ' Delete the first three lines

    ws.Rows("1:3").Delete

' Save the temporary sheet as a .csv file

ws.SaveAs FileName:=currentFilePath & "" & csvFileName, FileFormat:=xlCSV, Local:=True

**** ' Close the temporary sheet without saving the changes

    Application.DisplayAlerts = False ' Disable warning messages

    ws.Parent.Close SaveChanges:=False

    Application.DisplayAlerts = True ' Enable warning messages

    MsgBox ".csv file exported successfully!"

End Sub

Microsoft 365 e Office | Excel | Per il lavoro | MacOS

Domanda bloccata. Questa domanda è stata eseguita dalla community del supporto tecnico Microsoft. È possibile votare se è utile, ma non è possibile aggiungere commenti o risposte o seguire la domanda.

0 commenti Nessun commento

4 risposte

Ordina per: Più utili
  1. Anonimo
    2024-09-15T14:48:56+00:00

    Perfetto,

    Applicherò la modifica.

    Grazie mille.

    Ciao.

    0 commenti Nessun commento
  2. Anonimo
    2024-09-15T14:40:16+00:00

    Provando mal mio pc (Windows) in locale il csv viene creato correttamente e mantenendo i caratteri speciali utilizzato il formato xlCSVUTF8.

    ciao

    0 commenti Nessun commento
  3. Anonimo
    2024-09-15T12:55:20+00:00

    Buongionro Casanmaner,

    Grazie per la risposta. Appena possibile provo da PC.

    Infatti sul Mac il codice funzionava (tranne che sparivano i caratteri speciali) quando avevo il file nella cartella sincronizzata con OneDrive.

    Ora che ho spostato il file in locale, pare che a VBA non piaccia il formato .csv con il metodo SaveAs...

    Mistero.

    Grazie.

    0 commenti Nessun commento
  4. Anonimo
    2024-09-14T06:10:06+00:00

    Ciao,

    per il "FileFormat" prova ad utilizzare xlCSVUTF8

    0 commenti Nessun commento