excel save sheet to CSV with UTF 8 encoding

'VBA routine to save the currently active worksheet to a CSV file
'without losing focus AND retaining Unicode characters. This routine
'is extremely fast (instantaneous) and produces no flicker:

Sub SaveSheetAsCSV()
    Dim i&, j&, iMax&, jMax&, chk$, listsep$, s$, v
    Const Q = """", QQ = Q & Q
    listsep = Application.International(xlListSeparator)
    chk = Q & "," & listsep & "," & vbLf
    With ActiveSheet
        v = .UsedRange.Value
        iMax = UBound(v, 1): jMax = UBound(v, 2)
        For i = 1 To iMax
            For j = 1 To jMax
                If Not IsError(v(i, j)) Then s = v(i, j) Else s = .Cells(i, j).Text
                If AnyIn(s, Q, listsep, vbLf) Then s = Replace(s, Q, QQ): s = Q & s & Q
                BuildString s & listsep
            Next
            If i < iMax Then BuildString vbCrLf, -1
        Next
        s = .Parent.Path & Application.PathSeparator & Left(.Parent.Name, InStrRev(.Parent.Name, ".")) & .Name & ".csv"
        SaveStringAsTextFile BuildString(Done:=True, Adjust:=-1), s
    End With
End Sub

Function BuildString(Optional txt$, Optional Adjust&, Optional Done As Boolean, Optional Size = "20e6")
    Static p&, s$
    If Len(p) Then p = p + adjust
    If Done Then BuildString = Left(s, p - 1): p = 0: s = "": Exit Function
    If p = 0 Then: p = 1: s = Space(Size)
    Mid$(s, p, Len(txt)) = txt
    p = p + Len(txt)
End Function

Function AnyIn(s$, ParamArray checks()) As Boolean
    Dim e
    For Each e In checks
        If InStrB(s, e) Then AnyIn = True: Exit Function
    Next
End Function

Function SaveStringAsTextFile$(s$, fName$)
    Const adSaveCreateOverWrite = 2
    With CreateObject("ADODB.Stream")
        .Charset = "utf-8"
        .Open
        .WriteText s
        .SetEOS
        .SaveToFile fName, adSaveCreateOverWrite
        .Close
    End With
End Function

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
excel save as utf-8 csv excel 2013 export encoding utf-8 csv excel export encoding utf-8 csv excel encoding utf-8 csv csv to excel utf 8 excel csv utf 8 excel to csv utf-8 online how to save csv file with utf-8 in excel 2016 convert csv to utf-8 excel excel save .csv file utf-8 df save as csv as utf8 utf 8 encoding csv excel format csv to get excel to as utf-8 excel csv utf-8 save excel open csv with utf-8 encoding xls to csv utf-8 encoding c# export csv utf-8 excel excel export file as csv utf-8 convert excel to utf-8 csv excel csv save as utf8 saving excel sheet as utf 8 csv excel save csv as utf8 convert excel file to utf-8 csv saving as .csv utf-8 in Excel convert excel to csv utf-8 online convert excel to csv utf-8 excel save csv utf8 excel save csv with utf 8 excel save csv utf-8 how to save csv in utf 8 format in python how to save csv in utf 8 format save csv utf-8 excel 2016 excel export csv utf-8 excel csv encoding utf-8 to_csv encoding utf-8 excel csv utf8 encoding excel convert to utf 8 csv how to save excel csv file in utf-8 excel 2013 save csv utf-8 generate csv from excel python utf8 comma separated how to encode a csv file in utf-8 excel convert csv to utf8 to csv format utf 8 how to save excel as utf-8 how to save utf-8 csv change character set on csv in ms excel csv encoding utf-8 change csv encoding how to change encoding of csv file save csv with utf-8 encoding how to convert csv file in csv utf 8 excel export all sheets to csv encoding utf 8 csv file cant encode utf-8 csv file utf 8 encoding csv file utf8 encoding how t oconvert excel to text file with UTF-8 how to save a csv with ascii encoding save csv with ascii encoding notepad++ convert csv to utf-8 excel to csv with utf 8 encoding excel to csv utf-8 csv to utf-8 saving disabled not utf-8 encoded convert csv to excel csv encoding utf-8 excel how to fix csv utf8 libreoffice open utf-8 how to utf encode a csv file csv utf-8 which Unicode UTF-8 is good for saving file .csv export-csv encoding change csv file to utf-8 convert csv to utf8 excel export csv charset save csv with unicode urf 8 csv utf8 csv in 2010 excel excel export sheets to csv utf-8 store csv excel utf8 notepad++ csv save as UTF-8 how to resave a file as utf-8 make csv utf 8 change csv encoding to utf-8 convert excel to csv utf8 excel export csv encoding utf-8 export exel encoding after download mac save file as utf exel encode csv utf8 how to conver to utf-8 csv how to convert file csv comma to utf8 in excel how to convert file csv comma to utf8 how to create Excel file as UTF-8 csv not utf-8 encoded excel export unicode 8 encoding type when saving excel as comma delimited file excel to utf-8 csv excel export csv as utf 8 export csv encoding utf-8 How to save csv file with utf 8 in excel 2016 excel change encoding of csv in excel on save numbers save csv utf for python export write csv in utf-8 excel save sheet to CSV with UTF 8 encoding
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source