【发布时间】:2020-10-18 17:03:11
【问题描述】:
我正在尝试制作一个宏,让用户可以使用 UTF8 编码从 CSV 导入数据,并将所有列定义为文本(Excel 通常假定相关数据是日期和数字)。我的代码如下,
Sub Macro1()
Const sName As String = "9 1copy"
Dim strFile As String
strFile = Application.GetOpenFilename("Text Files (*.csv),*.csv", , "Please select text file...")
On Error GoTo delQuery
ThisWorkbook.Queries.Add Name:="9 1copy", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Csv.Document(File.Contents(strFile),[Delimiter="","", Columns=64, Encoding=65001, QuoteStyle=QuoteStyle.Csv])," & Chr(13) & "" & Chr(10) & " #""Change Type"" = Table.TransformColumnTypes(Source,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type text}, {""Column6" & _
""", type text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}, {""Column10"", type text}, {""Column11"", type text}, {""Column12"", type text}, {""Column13"", type text}, {""Column14"", type text}, {""Column15"", type text}, {""Column16"", type text}, {""Column17"", type text}, {""Column18"", type text}, {""Column19"", type text}, {""C" & _
"olumn20"", type text}, {""Column21"", type text}, {""Column22"", type text}, {""Column23"", type text}, {""Column24"", type text}, {""Column25"", type text}, {""Column26"", type text}, {""Column27"", type text}, {""Column28"", type text}, {""Column29"", type text}, {""Column30"", type text}, {""Column31"", type text}, {""Column32"", type text}, {""Column33"", type t" & _
"ext}, {""Column34"", type text}, {""Column35"", type text}, {""Column36"", type text}, {""Column37"", type text}, {""Column38"", type text}, {""Column39"", type text}, {""Column40"", type text}, {""Column41"", type text}, {""Column42"", type text}, {""Column43"", type text}, {""Column44"", type text}, {""Column45"", type text}, {""Column46"", type text}, {""Column47" & _
""", type text}, {""Column48"", type text}, {""Column49"", type text}, {""Column50"", type text}, {""Column51"", type text}, {""Column52"", type text}, {""Column53"", type text}, {""Column54"", type text}, {""Column55"", type text}, {""Column56"", type text}, {""Column57"", type text}, {""Column58"", type text}, {""Column59"", type text}, {""Column60"", type text}, {" & _
"""Column61"", type text}, {""Column62"", type text}, {""Column63"", type text}, {""Column64"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Change Type"""
On Error GoTo 0
ThisWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""9 1copy"";Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [9 1copy]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "_9_1copy"
.Refresh BackgroundQuery:=False
End With
ActiveSheet.ListObjects("_9_1copy").ShowHeaders = False
ActiveSheet.ListObjects("_9_1copy").ShowTableStyleRowStripes = False
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Exit Sub
delQuery:
Dim v
For Each v In ActiveWorkbook.Queries
If v.Name = sName Then _
v.Delete
Resume
Next v
MsgBox "Error No: " & Err.Number & vbLf & Err.Description
Stop
End Sub
运行代码时遇到错误
“运行时错误 1004:应用程序定义或对象定义错误”
上线
.ListObject.DisplayName = "_9_1copy"
在调试时突出显示。 你能帮我修复代码并让我知道我做错了什么吗?
【问题讨论】:
-
在设置参数之前,该函数调用中的其他行均未使用
ListObject。为什么不使用.DisplayName? -
您是否尝试这种方式只是为了确保所有字段都作为文本导入?
-
@FaneDuru 是的,这就是目标。此外,一旦以文本格式导入,我必须以某种方式重新排列单元格值。所有这些都已准备就绪并且运行良好。只需要导入功能
-
您尝试以文本形式打开什么类型的 csv?我的意思是制表符、逗号等分隔,你知道它有多少列吗?是否始终存在相同数量的列?
-
逗号分隔所有值。列数始终为 64
标签: excel vba csv import formatting