简体   繁体   中英

Copy cell J1 from multiple files and paste into column of master file

I currently have this code which will take files from a folder, open one each one, print its name into the first column of my "Master file" close it and loop through the entire folder that way.

In each file that is opened, there is information in cell J1 that I would like to copy and paste into column 3 of my "master file". The section of code currently returns an error ( Object does not support this property or method but I cannot tell which line it is referring to) and causes the program to stop after only opening one file.

Any ideas?

FULL CODE:

Sub LoopThroughDirectory()

    Dim objFSO As Object
    Dim objFolder As Object
    Dim objFile As Object
    Dim MyFolder As String
    Dim Sht As Worksheet
    Dim i As Integer

    MyFolder = "C:\Users\trembos\Documents\TDS\progress\"

Set Sht = ActiveSheet

    'create an instance of the FileSystemObject
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    'get the folder object
    Set objFolder = objFSO.GetFolder(MyFolder)
    i = 1
    'loop through directory file and print names
    For Each objFile In objFolder.Files

        If LCase(Right(objFile.Name, 3)) <> "xls" And LCase(Left(Right(objFile.Name, 4), 3)) <> "xls" Then
        Else
            'print file name
            Sht.Cells(i + 1, 1) = objFile.Name
            i = i + 1
            Workbooks.Open Filename:=MyFolder & objFile.Name
        End If
        'Get TDS name of open file
        Range("J1").Select
        Selection.Copy
        Windows("masterfile.xlsm").Activate
        Range("C2").Select
        ActiveSheet.Paste
        objFile.Activate
        ActiveWorkbook.Close
    Next objFile


End Sub

Part of code that is messing up the program:

'Get TDS name of open file
Range("J1").Select
Selection.Copy
Windows("masterfile.xlsm").Activate
Range("C2").Select
ActiveSheet.Paste
objFile.Activate

objFile.Activate is your issue.

objFile is not a workbook variable, it's being assigned a path\\filename from objFolder.Files .

Use the following:

Dim NewWorkbook as Workbook
set NewWorkbook = Workbooks.Open Filename:=MyFolder & objFile.Name
.
.
.
NewWorkbook.Activate
ActiveWorkbook.Close

Now, instead of the last two lines, since you have a variable that's referencing the opened workbook, you can replace those two lines with this one:

NewWorkbook.Close

Read this link for some good advice on other ways to eliminate Activate , Select , etc to make your code cleaner, more readable, less likely to have bugs due to the wrong place having the focus and easier to maintain.

I think the problem is due to unqualified references. Specifically, I am not sure if you can paste a selection from a not-active sheet to a newly active sheet, but I am not sure because I avoid use of .Select and .Activate so I don't have problems.

Try replacing the problematic section with this:

Sht.Range("J1").Copy Workbooks("masterfile.xlsm").Sheets(1).Cells(2,3)
objFile.Activate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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