通过;搜索加载的csv文件;作为分隔符 [英] Search through loaded csv file with ; as the delimiter
问题描述
我使用以下代码将csv文件作为二维数组加载到内存中:
I loaded a csv file as a 2-D array to memory with following code:
Sub readcsv()
Dim wbCSV As Workbook
Dim Data As Variant
Application.ScreenUpdating = False
Set wbCSV = Workbooks.Open(Filename:="C:\Users\File.csv")
With wbCSV
Data = .Sheets(1).UsedRange.Value
.Close
End With
End Sub
下一步,我想搜索数据并选择一些.关键是我想这样做而无需打开csv本身.有什么办法可以搜索加载的数组吗?我也知道csv中感兴趣的数据的i-j.csv使用;作为分隔符.用此代码
next step i want to search through the data and pick some. the point is that I want to do so without opening the csv itself. is there any way to search through the loaded array? I also know i-j of the intrested data in the csv. The csv uses ; as delimiter. with this code
data(i,j)
我希望在i-j单元格中包含该元素,但它会返回无意义的字符串.所以问题是,vba是否可以识别;以这种方式加载文件时作为分隔符?还是我需要将加载的数据排列成矩阵形式,然后调用特定的单元格?
I expect to have the element in the i-j cell but it returns the nonsense string. So the question is, does vba recognise the ; as delimiter when the file is loaded in this way? or i need to arrange the loaded data into a form of a matrix and then call a specific cell?
更新:
我发现了
data(i,j)
返回奇怪的字符串,因为vba将视为分隔符.因此,例如,在第5行中,我有a; b; 1,2345; c data(5,2)返回2345; c
returns strange string because vba considers , as the delimiter. So for example when in the 5th row I have a;b;1,2345;c data(5,2) returns 2345;c
谢谢
推荐答案
您可以使用文件I/O将CSV文件作为文本文件打开,逐行循环浏览数据,直到找到所需的内容为止;并且然后关闭文件
You can open the CSV file as a text file using File I/O, loop through the data line by line until you find what you are looking for, and then close the file
这篇关于通过;搜索加载的csv文件;作为分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!