본문 바로가기

Project/PO-MI

엑셀파일 csv로 따옴표 추가해서 저장하기

728x90

엑셀파일에서 Alt+F11 누르고 우클릭-삽입-모듈

 

모듈에 코드 입력 후

Sub CSVFile()
'updateby Extendoffice
    Dim xRg As Range
    Dim xRow As Range
    Dim xCell As Range
    Dim xStr As String
    Dim xSep As String
    Dim xTxt As String
    Dim xName As Variant
    On Error Resume Next
    If ActiveWindow.RangeSelection.Count > 1 Then
      xTxt = ActiveWindow.RangeSelection.AddressLocal
    Else
      xTxt = ActiveSheet.UsedRange.AddressLocal
    End If
    Set xRg = Application.InputBox("Please select the data range:", "Kutools for Excel", xTxt, , , , , 8)
    If xRg Is Nothing Then Exit Sub
    xName = Application.GetSaveAsFilename("", "CSV File (*.csv), *.csv")
    xSep = Application.International(xlListSeparator)
    Open xName For Output As #1
    For Each xRow In xRg.Rows
        xStr = ""
        For Each xCell In xRow.Cells
            xStr = xStr & """" & xCell.Value & """" & xSep
        Next
        While Right(xStr, 1) = xSep
            xStr = Left(xStr, Len(xStr) - 1)
        Wend
        Print #1, xStr
    Next
    Close #1
    If Err = 0 Then MsgBox "The file has saved to: " & xName, vbInformation, "Kutools for Excel"
End Sub

F5를 누르고 엑셀파일에서 셀 영역 지정 후 저장하면 따옴표 추가 저장 된다

 

참고 : https://ko.extendoffice.com/documents/excel/3620-excel-save-worksheet-data-as-csv-file-with-without-double-quotes.html#a1

728x90
LIST