0

I have a xlsm that amonst others runs through all .xslx files in a directory, runs a sub, saves them. (Thank you Tabias) inside this sub I am now trying to add something that would add the last column from a third file. My first problem here is how to define the sourcefile. We need to take data from the exact file, with a similar name. So MC.xslx ahs to copy from MC12february.xlsx and KA.xlsx has to import from KAwhateverdate.xlsx

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

unfortunately, active.workbook.name includes the extention, so OR you guys can tell me a solution OR i have to save the files date+name first and change it into wbA = Workbooks.Open("C:\files" & "\*" & ActiveWorkbook.Name) right?

The same goes for the sheet. Those wil, depending on the file, be called MC, KA,KC,... Next since i only want to copy the last column of the file into the last column of the other file I'm quite confused. I found this code and thought it was the most understandable.

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

None of the more simpler solutions seemed fit either. example

As you see I'm completely stuck at how to define the last column and the name of the sheet. This code is to uncomplete for me to check by doing. Can someone put me on the right path? thank you.

Community
  • 1
  • 1
Lara
  • 29
  • 6
  • Sorry, can you clarify the issue a little more? The issue is opening your sheets? If you have .xlsx files in a directory, you can use [looping through a directory](http://stackoverflow.com/questions/10380312/loop-through-files-in-a-folder-using-vba) or see [this page](http://www.thespreadsheetguru.com/the-code-vault/2014/4/23/loop-through-all-excel-files-in-a-given-folder) which should provide some help. Also, if you set the workbook name to a variable, you can remove the extension with `WBname = Replace(wb.Name, ".xls", "")` – BruceWayne Apr 15 '16 at 13:52
  • Im' sorry I'm known to be very chaotic. – Lara Apr 15 '16 at 13:58
  • I am already looping through the directory filled with files called KA.xslx ,KC.xslx ,MC.xslx ,WT.xslx.... So in the loop a file is opened (duh), something words changed, some sheets made and all sheets are checked for empty columns. I want to add a step where I copy the last column from another file into the file that is now active. The other file is different for every file in the directory. So KA.xlsx has to copy the last column of a file that is called KA1564.xlsx and KC.xlsx must take ti from KC68787.xlsx – Lara Apr 15 '16 at 14:05
  • putting the name in a variable! ok this helps! Part 1 of my problem probably solved. Now remains the part where i don't understand how to select the last column of a sheet to copy in the last column of another sheet. Add left maybe? – Lara Apr 15 '16 at 14:06
  • Create variables for each workbook, then create variable for the last column in that workbook (something like `lastCol = Cells(1,columns.count).End(xlToLeft).Column` to get the last column), then just do like `newWB.sheets("newSheet").Cells(1,1).Value = oldWB.Sheets("oldSheet").Cells(1,1).Value` or similar. – BruceWayne Apr 15 '16 at 14:09
  • And where do i use the lastCol? – Lara Apr 18 '16 at 09:39

1 Answers1

0

As a supplement, I'd suggest creating a simeple, re-usable file open functions where you can provide a filename as a String that you'd like to search for. The function will loop through a directory (as Batman suggested) and, optionally, pull the most recent version (using date modified) of that file. Below is a set of functions that I use frequently. There is a subfolder parameter `subF' that will allow you to search within subfolder(s) relative to the current file location.

'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

You could tweak this method where the filename would have to start file the String you pass in by simply removing the wildcard character before flNM. To use, you would simply call the opnWB function, passing in "MC" or whatever general file name you'd like to open:

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)

Hope this helps.

David
  • 41
  • 5
  • Thanks but I don't understand how this can eb used. I am already looping through all the files opening them one by one. Would this be to replace my looping code (not seen in this question). – Lara Apr 18 '16 at 09:28
  • I am already looping through all the files opening them, applying some subs, saving them, opening the next file in the directory etc.This code allows me to open one specific file? I don't understand how this has an advantage over just saying `Workbooks.Open(ThisWorkbook.Path & "\MC.xlsx")` Which is in itself not something i could use, since I need to open all files. Do you reccommend explicitly opening every file in the directory? – Lara Apr 18 '16 at 09:38