如何使用 VBA 将制表符分隔的数据导入 Excel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15231420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to import tab delimited data into Excel using VBA
提问by Arlen Beiler
Given tab delimited data that I am importing from Excel, how do I insert it into the spreadsheet so that it ends up in multiple cells like it does when I paste it.
给定我从 Excel 导入的制表符分隔数据,如何将其插入到电子表格中,使其像粘贴时一样最终出现在多个单元格中。
Edit:I have the text in a variable, so I don't want to go through the file system.
编辑:我有一个变量中的文本,所以我不想通过文件系统。
回答by Arlen Beiler
This is basically what I finally came up with, it was a little more complex and used arrays, but that is the gist of it.
这基本上是我最终想出的,它有点复杂并且使用了数组,但这就是它的要点。
i = 1
For Each Row In VBA.Split(text, vbCrLf)
j = 1
For Each Col In VBA.Split(Row, vbTab)
ActiveSheet.Cells(i, j).Value = Col
j = j + 1
Next Col
i = i + 1
Next Row
回答by www
It should not be too hard. Please try: http://www.zerrtech.com/content/excel-vba-open-csv-file-and-import
应该不会太难。请尝试:http: //www.zerrtech.com/content/excel-vba-open-csv-file-and-import
If you want to do it from string variable, split text by end line:
如果要从字符串变量执行此操作,请按结束行拆分文本:
Dim linesSplit As Variant
linesSplit = Split(yourTextVar, "\n")
For each linesSplit, split by comma:
对于每一行分割,用逗号分割:
Dim lineSplit As Variant
lineSplit = Split(linesSplit[i], ",")
Put result in worksheet. GL!
将结果放在工作表中。高!