UTF-8 Format

Daviadio

New Member
Joined
Nov 15, 2014
Messages
3
Hi

I need to export selected cells as an UTF-8 format compatible file.

I am ok if it were just a test file - but have no idea how to ensure it is UTF-8 compatible.

Any leads/pointers or assistance appreciated. I am happy to post once the solution once I have it working

Cheers
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Try just saving as txt file. It works for me.
 
Upvote 0
The following macro is based on a solution offered by Vladimir in the following thread...

http://www.mrexcel.com/forum/excel-questions/707325-export-excel-text-file-utf16-encoded.html

Try...

Code:
[COLOR=darkblue]Option[/COLOR] [COLOR=darkblue]Explicit
[/COLOR]
[COLOR=darkblue]Sub[/COLOR] SaveSelectionAsUTF8()

    [COLOR=darkblue]Dim[/COLOR] oStream             [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Object[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] sDestFolder         [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] sFilename           [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] vData               [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Variant[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] aCols()             [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] aRows()             [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] sText               [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] RowCount            [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] ColCount            [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] r                   [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] c                   [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    
    [COLOR=darkblue]If[/COLOR] TypeName(Selection) <> "Range" [COLOR=darkblue]Then[/COLOR] [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Sub[/COLOR]
    
    [COLOR=darkblue]Const[/COLOR] sListSep [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR] = vbTab [COLOR=green]'or ","[/COLOR]
    [COLOR=darkblue]Const[/COLOR] sLineSep [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR] = vbCrLf
    
    sDestFolder = "C:\Users\Domenic\Desktop\" [COLOR=#008000]'change the path to the source folder accordingly[/COLOR]
    [COLOR=darkblue]If[/COLOR] Right(sDestFolder, 1) <> "" [COLOR=darkblue]Then[/COLOR] sDestFolder = sDestFolder & "\"
    
    sFilename = "sample.txt" [COLOR=#008000]'change the file name accordingly[/COLOR]
    
    vData = Selection.Value
    
    RowCount = [COLOR=darkblue]UBound[/COLOR](vData, 1)
    ColCount = [COLOR=darkblue]UBound[/COLOR](vData, 2)
    
    [COLOR=darkblue]ReDim[/COLOR] aRows(1 [COLOR=darkblue]To[/COLOR] RowCount)
    [COLOR=darkblue]ReDim[/COLOR] aCols(1 [COLOR=darkblue]To[/COLOR] ColCount)
    
    [COLOR=darkblue]For[/COLOR] r = 1 [COLOR=darkblue]To[/COLOR] RowCount
        [COLOR=darkblue]For[/COLOR] c = 1 [COLOR=darkblue]To[/COLOR] ColCount
            aCols(c) = vData(r, c)
        [COLOR=darkblue]Next[/COLOR] c
        aRows(r) = Join(aCols, sListSep)
    [COLOR=darkblue]Next[/COLOR] r
        
    sText = Join(aRows, sLineSep)
    
    [COLOR=darkblue]Set[/COLOR] oStream = CreateObject("ADODB.Stream")
    
    [COLOR=darkblue]With[/COLOR] oStream
        .Charset = "UTF-8"
        .Mode = 3 [COLOR=green]'adModeReadWrite[/COLOR]
        .Type = 2 [COLOR=green]'adTypeText[/COLOR]
        .Open
        .WriteText sText
        .SaveToFile sDestFolder & sFilename, 2 [COLOR=green]'1=adSaveCreateNotExist, 2=adSaveCreateOverWrite[/COLOR]
        .Close
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,215,584
Messages
6,125,678
Members
449,248
Latest member
wayneho98

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top