Excel 如何在csv导入期间更改列格式
我通过导入.csv文件Excel 如何在csv导入期间更改列格式,excel,csv,import,vba,Excel,Csv,Import,Vba,我通过导入.csv文件 Sub Datei_Importieren() Dim strFileName As String, arrDaten, arrTmp, lngR As Long, lngLast As Long Const cstrDelim As String = VBA.Constants.vbTab 'Trennzeichen With Application.FileDialog(msoFileDialogFilePicker) .AllowMultiSe
Sub Datei_Importieren()
Dim strFileName As String, arrDaten, arrTmp, lngR As Long, lngLast As Long
Const cstrDelim As String = VBA.Constants.vbTab 'Trennzeichen
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Title = "Datei wählen"
.InitialFileName = "C:\Test\*.csv" 'Pfad anpassen
.Filters.Add "CSV-Dateien", "*.csv", 1
If .Show = -1 Then
strFileName = .SelectedItems(1)
End If
End With
If strFileName <> "" Then
Application.ScreenUpdating = False
Open strFileName For Input As #1
arrDaten = Split(Input(LOF(1), 1), vbCrLf)
Close #1
For lngR = 1 To UBound(arrDaten)
arrTmp = Split(arrDaten(lngR), cstrDelim)
If UBound(arrTmp) > -1 Then
With ActiveSheet
lngLast = .Cells(Rows.Count, 1).End(xlUp).Row + 1
lngLast = Application.Max(lngLast, 10)
.Cells(lngLast, 1).Resize(, UBound(arrTmp) + 1) _
= Application.Transpose(Application.Transpose(arrTmp))
End With
End If
Next lngR
End If
End Sub
该功能工作完美,但我想将D列设置为文本,但就是找不到参数。有谁能帮我吗?这段代码适合我
Sub Import_Zeros()
Application.CutCopyMode = False
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\test\export.xls" _
, Destination:=Range("$A$1"))
.Name = "export"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 936
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 2, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Range("D19").Select
Application.WindowState = xlMaximized
End Sub
您可以在此处调整格式:Array1、1、1、2、1、1、1、1。范围1:D&lngLast.NumberFormat=@是否始终希望它显示3位数字?如果是这样,请使用=000try=或=0I刚才我记得您可以通过vba以与手动相同的方式导入CSV。这可能是更好的方法,因为您可以直接在其中定义数据类型。获取它的一个简单方法是记录自己手动操作的宏,然后根据需要进行调整。