0

我有以下 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
4

1 回答 1

0

I'm currently mobile, so I cannot provide any example code, but you can automate excel to open the .csv file, format all cells as text and use excel's own ability to split the lines into columns. Finally, you can delete unwanted columns or process only the columns that you want. I suggest that you carry out the above steps manually, recording a macro. Then you can edit the generated code as you desire. If you need more information, please tell me so I can post some code once I have access to vba de.

于 2013-09-28T12:29:42.763 回答