简体   繁体   English

VBA代码将每日更改的.csv文件复制并粘贴到Excel中

[英]VBA code to copy and paste daily changing .csv file into excel

So I have a .csv file that gets exported everyday into my files, of the format: "FILENAME03_28_2017-14_45_01" so it changes everyday with the date. 因此,我有一个.csv文件,该文件每天都会导出到我的文件中,其格式为:“ FILENAME03_28_2017-14_45_01”,因此它随日期每天都会更改。 I am trying to be able to copy and paste everything from that file into a different excel sheet, but since the file name changes everyday I can't figure out how to write the code to maybe look at a certain cell for the date and concatenate that with the basic FILENAME to open it. 我试图能够将文件中的所有内容复制并粘贴到另一个Excel工作表中,但是由于文件名每天都在变化,因此我无法弄清楚如何编写代码以查看日期中的某个单元格并进行连接使用基本的FILENAME打开它。 I have a code that opens up the latest file in that folder, but its the copy paste part i'm stuck at. 我有一个代码可以打开该文件夹中的最新文件,但是我将其粘贴在粘贴部分。

you can't just do something like (where C15 contains the current date)?? 您不能只做类似的事情(其中C15包含当前日期)?

Windows("C:\Users\ME\Desktop\FOLDERS\FILENAME" & Range("C15")&".csv").Activate
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Windows("EXCELFILE.xlsm").Activate
Sheets("SHEET1").Select
Range("T1").Select
ActiveSheet.Paste`

thank you! 谢谢!

You can use this vba code to capture the date: 您可以使用以下vba代码捕获日期:

format(date, "mm_dd_yyyy") 'returns "03_28_2017"

Date is a keyword in Excel VBA that gives today's date in mm/dd/yyyy format. 日期是Excel VBA中的关键字,它以mm / dd / yyyy格式提供今天的日期。 However, you can use that Format function as written above to cast it into many formats. 但是,您可以使用上面编写的Format函数将其转换为多种格式。 The resulting output is a string, not a date - so you can use it in your dynamic filename. 结果输出是字符串,而不是日期-因此您可以在动态文件名中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM