2013-07-02 57 views
1

我是VBA新手,我正在嘗試編寫一個代碼,用於從存儲在同一目錄中的多個csv文件複製以下數據。從指定目錄中的excel文件中提取某些單元格

我需要它打開每個csv文件

檢查是否行8列H至CA對於具有值= 「TotalLMP」 的任何細胞(樣品:細胞H8 = 「TotalLMP」)

那麼將第8行中具有該值=「TotalLMP」的任何列的第7行和第19行的值複製到兩個新列中(樣本:SINCE H8 =「TotalLMP」,COPY H7 =「100」作爲列A,COPY H19 = 「26.437」 AS柱B)

然後在第三列從細胞$ A $ 9值複製(樣品:COPY A9 = 「20100101」 作爲列C「)

通過每個CSV文件關閉完成循環,並進入下一

然後在空白的新活動工作表後,Excel文件將存儲每個值如下:

.......一個。 ............. B ................ C

1 .. 100 .... 26.437 .... 20100101

2 .. 200 .... 26.585 .... 20100101

+3

如果您有任何現有的代碼,你應該包括在你的問題 - 即使它不工作。只要發佈要求,但沒有任何跡象表明您已嘗試自行解決問題,通常會導致問題被關閉。 –

+0

+1對Tim的評論如上。我們希望你已經嘗試了一些東西.... – brettdj

回答

3

讓我現在來幫助您使用CSV循環,因爲這是r ather很難爲初學者。我相信你會弄清楚如何在第8行測試一個值。如果沒有,你總是可以要求更多的幫助!

爲此,您必須使用Microsoft Scripting Runtime。

我建議將您想要打開的所有csv文件放在同一個目錄中,並且只有那些才能避免潛在問題。

打開一個新的工作簿並轉到VBE(ALT + F11)。創建一個新模塊。點擊這個新模塊,然後進入工具>參考> Microsoft Scripting Runtime。這將讓它知道它將不得不使用該模塊及其對象。

保存在同一目錄作爲您的CSV工作簿作爲一個啓用宏的工作簿文件(.xls或.xslm較新的版本)(或別的地方...)

然後開始編碼:

Sub Import_all_Csv() 
' Reference Needed: Microsoft Scripting Runtime 

' Dim some pointers to know what objects you will be manipulating thereafter 
Dim MyWs, CSV As Worksheet 
Set MyWs = ActiveSheet ' Meaning you will have to run the macro from the spreadsheet you want to export to. Feel free to replace that 
Dim wbCSV As Workbook 

' Those are the objects that belong to the reference Microsoft Scripting Runtime 
Dim oFSO As FileSystemObject 
Dim oFld As Folder 
Dim oFile As File 

Dim File As String 

' Initialize the FileSystemObject 
Set oFSO = New FileSystemObject 

' That will only work on windows so I'm adding an error handler to ignore it if need be 
On Error Resume Next 
ChDir ThisWorkbook.Path 
On Error GoTo 0 ' I'm asking VBA to throw an error now 

' Dialog box to select the first csv file (this will let you choose another directory everytime) 
File = Application.GetOpenFilename("Comma Separated Values File (*.csv*), *.csv*") 
If File = "False" Then 
    Exit Sub ' Quit the macro if user canceled 
Else 
    ' Else get the path of the parent folder of that file 
    Set oFld = oFSO.GetFolder(oFSO.GetParentFolderName(File)) 
End If 

' Go through each file in that folder 
For Each oFile In oFld.Files 

    ' Only open the CSV files 
    If oFile.Type = "Microsoft Excel Comma Separated Values File" Then 
     ' Open it and set the first sheet (There is only one anyway) 
     Set wbCSV = Workbooks.Open(oFile) 
     Set CSV = wbCSV.Sheets(1) 
     ' ============================ 
     ' Do what you want to do Here 

     ' THIS IS A PLACEHOLDER 
     ' Example to copy value of H8 in the CSV file to A2 the destination worksheet so you can see how to point to the correct cells in both files 
     MyWs.cells(1,2).value = wCSV.cells(8,8).value 

     ' End of what you want to do 
     ' ============================ 

     ' Close the CSV file without savings changes before going through the next one 
     wbCSV.Close False 

    End If 

Next oFile 

End Sub 

我希望這有助於!祝你好運學習更多VBA!

最佳, 朱利安

+1

使用帶有通配符的'Dir'可以更有效地測試目錄中每個文件的FileType。看到http://stackoverflow.com/questions/10380312/loop-through-files-in-a-folder-using-vba/10382861#10382861 – brettdj

+0

哦,這似乎很酷。我一定會研究它(在Mac上工作?)。謝謝! –

+0

不是MAC VBA專家,所以不確定:) – brettdj

相關問題