CSV files in UTF-8 format can be read in VBA without garbled characters

Source: Internet
Author: User

Import data in Excel. (Data-> Import external data)

Sub macro1 ()

'
'Macro1 macro
'Test for sunhaifeng
'Sunhf@cn.ibm.com
'

If fileexists ("C: \ bookfortestdata.csv") then
Kill "C: \ bookfortestdata.csv"
End if

Workbooks. Add

With activesheet. querytables. Add (connection: = "text; E: \ test.csv", destination _
: = Range ("A1 "))
. Name = "test"
. Fieldnames = true
. Rownumbers = false
. Filladjacentformulas = false
. Preserveformatting = true
. Refreshonfileopen = false
. Refreshstyle = xlinsertdeletecells
. Savepassword = false
. Savedata = true
. Adjustcolumnwidth = true
. Refreshperiod = 0
. Textfilepromptonrefresh = false
. Textfileplatform = 932
'Here, the code of my CVS file is shift-JIS. If you need another code, you can record it yourself.
. Textfilestartrow = 1
. Textfileparsetype = xldelimited
. Textfiletextqualifier = xltextqualifierdoublequote
. Textfileconsecutivedelimiter = false
. Textfiletabdelimiter = true
. Textfilesemicolondelimiter = false
. Textfilecommadelimiter = true
. Textfilespacedelimiter = false
. Textfilecolumndatatypes = array (1, 1, 1, 1)
. Textfiletrailingminusnumbers = true
. Refresh backgroundquery: = false
End

Activeworkbook. saveas filename: = "C: \ bookfortestdata.csv", fileformat: = xlcsv ,_
Createbackup: = false

Activeworkbook. Close savechanges: = true

Dim mychar

Dim strtext as string
Dim filenum as integer
Dim STR () as string

Dim count as integer
Dim count1 as integer
Count = 1

Open "C: \ bookfortestdata.csv" for input as #1
Do while not EOF (1)
Line input #1, strtext

STR = Split (strtext ,",")

Count1 = ubound (STR)

For I = 0 to count1
Worksheets (1). cells (I + 1, count). value = STR (I)
Next I

Count = count + 1

Loop
Close #1

If fileexists ("C: \ bookfortestdata.csv") then
Kill "C: \ bookfortestdata.csv"
End if

End sub

private function fileexists (fname) as Boolean
dim X as string
X = Dir (fname)
If x <> "" Then fileexists = true _
else fileexists = false
end function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.