vba 自动将数据从 CSV 导入到 excel/calc 表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7008084/
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
automatically import data from CSV to excel/calc sheet
提问by Meloun
I have here 12 CSV source file (just numbers separated with semicolon).
我这里有 12 个 CSV 源文件(只是用分号分隔的数字)。
I need on one action/click take this data and import it to excel/calc to 12 sheets (1 sheet for 1 csv source).
我需要一个动作/单击获取此数据并将其导入到 excel/calc 到 12 张(1 张表用于 1 个 csv 源)。
Any Idea how can I do that?
任何想法我该怎么做?
回答by JMax
you can do it with Excel vba:
你可以用 Excel vba 做到这一点:
- create a new workbook(or use the current one depending on your needs)
- ask the user to select several csv files(if they are on the same folder, else, you can ask the user if he still have some more to import) >> doc here
- copy or move the imported worksheetto the new or current workbook (>> Excel 2003 VBA: Move a sheet into a new workbook that is referenced by a variable)
- 创建一个新工作簿(或根据您的需要使用当前工作簿)
- 要求用户选择几个 csv 文件(如果它们在同一个文件夹中,否则,您可以询问用户是否还有更多要导入的文件)>> doc here
- 将导入的工作表复制或移动到新的或当前的工作簿(>> Excel 2003 VBA:将工作表移动到由变量引用的新工作簿中)
Please let us know if you encounter some trouble building your procedure
如果您在构建过程时遇到问题,请告诉我们
回答by jonsca
By using the code in this postcombined with a loop in which you step through the filenames (which you could keep in an array of string) and vary the .Destination
with the sheets in the workbook(you can step through them by making as many new sheets as you need in the workbook and then going from Sheets(1)
through Sheets(N)
, where N is the number of sheets.
通过使用这篇文章中的代码结合一个循环,您可以在其中逐步执行文件名(您可以将其保存在一个字符串数组中)并随着.Destination
工作簿中的工作表改变它们(您可以通过制作尽可能多的新工作表来逐步执行它们当您在工作簿需要,然后从去Sheets(1)
通过Sheets(N)
,其中N是片材的数量。