我有以下 VBA 代码将所选列从 csv 文件导入到 excel。所选列应作为文本导入。例如:000001 应该按原样导入。它不应该截断前导 0。我使用文件系统对象编写了一个 VBA 来选择整个文件。但是,它只选择前 2 个。
我的要求: 1. 只选择某些列 2. 都应该作为文本导入
我的代码:
Sub Read_Text_File()
Dim fsoObj As Scripting.FileSystemObject
Dim fsoFile As Scripting.File
Dim fsoTS As Scripting.TextStream
Dim vaData As Variant
Dim i As Long, j As Long
Set fsoObj = New Scripting.FileSystemObject
Set fsoFile = fsoObj.GetFile("C:\Users\Sandeep\Downloads\TestFile.csv")
Set fsoTS = fsoFile.OpenAsTextStream(ForReading, TristateFalse)
j = 1
Do Until fsoTS.AtEndOfStream
vaData = Split(fsoTS.ReadLine, Chr(44))
Range(Cells(j, 1), Cells(j, 2)).Value = vaData
j = j + 1
Loop
fsoTS.Close
Set fsoFile = Nothing
Set fsoObj = Nothing
End Sub