列:“替换第1列”和“替换第2列”需要将“,”替换为“.”
另外,我需要用UTF-8编码打开文件。
如何使用这种数据转换打开 CSV?是否可以在 VBA 的 let 公式中不提及标题的名称(当我录制宏时)?
下面是我录制的宏:
Sub Macro3()
ActiveWorkbook.Queries.Add Name:="ENG CSV", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Csv.Document(File.Contents(""E:\ENG CSV.csv""),[Delimiter="","", Columns=4, Encoding=65001, QuoteStyle=QuoteStyle.None])," & Chr(13) & "" & Chr(10) & " #""Promoted Headers"" = Table.PromoteHeaders(Source, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(#""Promoted Headers"",{{""Date"", type" & _
" datetime}, {""Created By"", type text}, {""Replace Column 1"", type text}, {""Replace Column 2"", type text}})," & Chr(13) & "" & Chr(10) & " #""Replaced Value"" = Table.ReplaceValue(#""Changed Type"","","",""."",Replacer.ReplaceText,{""Replace Column 1"", ""Replace Column 2""})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Replaced Value"""
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""ENG CSV"";Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [ENG CSV]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "ENG_CSV"
.Refresh BackgroundQuery:=False
End With
End Sub