2016-04-15 97 views
0

我有一個xlsm,其他人通過目錄中的所有.xslx文件運行,運行一個子,保存它們。 (謝謝Tabias) 在這個子內我現在試圖添加一些東西,將添加第三個文件的最後一列。 我的第一個問題是如何定義源文件。我們需要從確切的文件中獲取數據,並使用相似的名稱。所以MC.xslx AHS從MC12february.xlsx複製和KA.xlsx必須從KAwhateverdate.xlsx進口excel-VBA:複製最後一列的動態路徑和名稱

Set wbA = Workbooks.Open("C:\files" & "\" & ActiveWorkbook.Name & "*.xlsx") 

不幸的是,active.workbook.name包括進一步擴展,所以OR你們能告訴我一個解決方案或我必須先保存文件日期+名稱並將其更改爲wbA = Workbooks.Open("C:\files" & "\*" & ActiveWorkbook.Name)對不對?

表單也一樣。這些將根據文件被稱爲MC,KA,KC,... 接下來,因爲我只想將文件的最後一列複製到另一個文件的最後一列,所以我很困惑。 I found this code,並認爲這是最容易理解的。

Sub import() 

Dim Range_to_Copy As Range 
Dim Range_Destination As Range 

Dim Sheet_Data As Worksheet 'sheet from where we pull the data 
Dim Sheet_Destination As Worksheet ' destination 
Dim workbook_data As Workbook 
Dim workbook_destination As Workbook 



Set workbook_data = "N:\blah\deposit" & "\*" & ActiveWorkbook.Name 
Set workbook_detination = ActiveWorkbook 
Set Sheet_Data = ThisWorkbook.Sheets("Sheet1") 'help, how do i do this? 
Set Sheet_Destination = ThisWorkbook.Sheets("Sheet1") ' and this? 

Set Range_to_Copy = sht.UsedRange.Rows(sht.UsedRange.Rows.Count).Row 

Set Range_Destination = sht.UsedRange.Rows(sht.UsedRange.Rows.Count).Row 

Range_to_Copy.Copy Range_Destination 'this copies from range A to B (basically A.copy B), but i changed variable names to make it easier... 


'you can simplify without variables like this: 
'Sheets("Sheet1").Range("D1").Copy Sheets("Summary).Range("A1")   <===== does the same as the above coding 

沒有更簡單的解決方案似乎也適合。 example

正如你所看到的,我完全停留在如何定義最後一列和工作表名稱上。這段代碼對我來說是不完整的。有人能讓我走上正確的道路嗎?謝謝。

+0

對不起,你能澄清一點問題嗎?問題是打開你的牀單?如果你在一個目錄中有.xlsx文件,你可以使用[循環目錄](http://stackoverflow.com/questions/10380312/loop-through-files-in-a-folder-using-vba)或看看[這個頁面]應該提供一些幫助(http://www.thespreadsheetguru.com/the-code-vault/2014/4/23/loop-through-all-excel-files-in-a-given-folder) 。此外,如果將工作簿名稱設置爲變量,則可以使用'WBname = Replace(wb.Name,「.xls」,「」)' – BruceWayne

+0

Im'對不起,我知道這個擴展非常混亂。 – Lara

+0

我已經循環遍歷了名爲KA.xslx,KC.xslx,MC.xslx,WT.xslx等文件的目錄....因此,在循環中打開一個文件(duh),更改了某些單詞,製作了一些表單並檢查所有表單是否爲空列。我想添加一個步驟,將其他文件的最後一列複製到現在處於活動狀態的文件中。另一個文件對於目錄中的每個文件都不相同。因此,KA.xlsx必須複製名爲KA1564.xlsx的文件的最後一列,並且KC.xlsx必須從KC68787.xlsx – Lara

回答

0

作爲補充,我建議創建一個simeple,可重用的文件打開功能,您可以提供一個文件名作爲您想要搜索的字符串。該函數將循環訪問一個目錄(如Batman建議的那樣),並且可以選擇使用該文件的最新版本(使用修改日期)。以下是我經常使用的一組功能。有一個子文件夾參數「subF」,它允許您在子文件夾內搜索相對於當前文件位置的文件夾。

'FUNCTION opnWB 
'--Opens a workbook based on filename parameter 
'----WILDCARDS before and after the filename are used to allow for filename flexibility 
'----Subfolder is an OPTIONAL PARAMETER used if the location of the file is located in a subfolder 
Public Function opnWB(ByVal flNM As String, Optional ByVal subF As String = "") As Workbook 
    If subF <> "" Then subF = "\" & subF 

    Dim pthWB As String 
     pthWB = "\*" & flNM & "*" 'wildcard characters before and after filename 
     pthWB = filePull(subF, pthWB) 

    Set opnWB = Workbooks.Open(ActiveWorkbook.path & subF & "\" & pthWB, UpdateLinks:=0) 

End Function 
'FUNCTION filePull 
'--Cycles through folder for files that match the filename parameter (with WILDCARDS) 
'--If there is more than one file that matches the filename criteria (with WILDCARDS), 
'----the file "Date Modified" attribute is used and the most recent file is "selected" 
Private Function filePull(ByVal subF As String, ByVal path As String) As String 
    Dim lDate, temp As Date 
    Dim rtrnFl, curFile As String 

    Filename = Dir(ActiveWorkbook.path & subF & path) 
    Do While Filename <> "" 
     curFile = Filename 
     curFile = ActiveWorkbook.path & subF & "\" & Filename 

     If lDate = 0 Then 
      rtrnFl = Filename 
      lDate = GetModDate(curFile) 
     Else 
      temp = GetModDate(curFile) 
     End If 

     If temp > lDate Then 
      rtrnFl = Filename 
      lDate = temp 
     End If 

     Filename = Dir() 
    Loop 

    filePull = rtrnFl 

End Function 
'FUNCTION GetModDate 
'--Returns the date a file was last modified 
Public Function GetModDate(ByVal filePath As String) As Date 
    GetModDate = CreateObject("Scripting.FileSystemObject").GetFile(filePath).DateLastModified 
End Function 

你可以調整這個方法,其中的文件名必須啓動文件,你在通過flNM之前簡單地刪除通配符傳遞字符串。要使用,只需將調用opnWB功能,傳遞「MC」或任何一般的文件名,你想開:

Dim wbTarMC as Workbook 
Set wbMC = opnWB("MC", "Source Files") 'this would open up MC.xlsx file within the subfolder "Source Files" (relative to current file location) 

希望這有助於。

+0

謝謝,但我不明白如何使用eb。我已經循環遍歷所有打開它們的文件。這是否會取代我的循環代碼(在這個問題中沒有看到)。 – Lara

+0

我已經循環遍歷所有打開它們的文件,應用一些子文件,保存它們,打開目錄中的下一個文件等。此代碼允許我打開一個特定的文件?我不明白這與僅僅說'Workbooks.Open(ThisWorkbook.Path&「\ MC.xlsx」)相比具有什麼優勢,「這本身不是我可以使用的,因爲我需要打開所有文件。你是否建議明確打開目錄中的每個文件? – Lara