+ Reply to Thread
Results 1 to 12 of 12

Copy and paste Filename to left of data

Hybrid View

  1. #1
    Registered User
    Join Date
    10-03-2013
    Location
    Jacksonville, USA
    MS-Off Ver
    Excel 2010
    Posts
    25

    Question Copy and paste Filename to left of data

    Hello excel gurus,

    I have a spreadsheet that copies and pastes data from excel spreadsheets within a directory. It can copy a set number of columns depending on the known files size. I have the problem, however, that the data is not labelled in any way, shape, or form. Here is the code.
    Sub OpenSubfoldersFileUpdate()
    Dim strFile As String
       Dim objFSO, destRow As Long
       Dim mainFolder, mySubFolder
       Set objFSO = CreateObject("Scripting.FileSystemObject")
        mFolder = Worksheets(1).Range("B12").Value
            If mFolder = 0 Then
            MsgBox " Address Missing"
            Else: Set mainFolder = objFSO.GetFolder(mFolder)
            End If
            For Each mySubFolder In mainFolder.subfolders
            strFile = Dir(mySubFolder & "\*.csv*")
            Do While strFile <> ""
                If Worksheets(1).Range("H12").Value = 0 _
                Then
                MsgBox "Missing Value!"
                End If
                If Worksheets(1).Range("H12").Value = 1 _
                Then
                Workbooks.Open mySubFolder & "\" & strFile
                Range("A2:AT2").Copy
                End If
                If Worksheets(1).Range("H12").Value = 2 _
                Then
                Workbooks.Open mySubFolder & "\" & strFile
                Range("A2:AT3").Copy
                End If
                If Worksheets(1).Range("H12").Value = 3 _
                Then
                Workbooks.Open mySubFolder & "\" & strFile
                Range("A2:AT4").Copy
                End If
                If Worksheets(1).Range("H12").Value = 4 _
                Then
                Workbooks.Open mySubFolder & "\" & strFile
                Range("A2:AT5").Copy
                End If
                If Worksheets(1).Range("H12").Value = 5 _
                Then
                Workbooks.Open mySubFolder & "\" & strFile
                Range("A2:AT6").Copy
                End If
            Application.DisplayAlerts = False
            ActiveWorkbook.Close
            erow = Sheet1.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Row
            Worksheets("Sheet 1").Paste Destination:=Worksheets("Sheet 1").Cells(erow, 2)
            strFile = Dir
            Loop
        Next
    End Sub
    Is there any way I can copy and paste the actual filename to the left of the data that is already being copy/pasted?

    Any help you guys can give would be most appreciated.

  2. #2
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,414

    Re: Copy and paste Filename to left of data

    Hello,

    Like this maybe ?
    Sub OpenSubfoldersFileUpdate()
       Dim strFile As String
       Dim objFSO, destRow As Long
       Dim mainFolder, mySubFolder
       Set objFSO = CreateObject("Scripting.FileSystemObject")
       mFolder = Worksheets(1).Range("B12").Value
       If mFolder = 0 Then
          MsgBox " Address Missing"
       Else: Set mainFolder = objFSO.GetFolder(mFolder)
       End If
       For Each mySubFolder In mainFolder.subfolders
          strFile = Dir(mySubFolder & "\*.csv*")
          Do While strFile <> ""
             If Worksheets(1).Range("H12").Value = 0 _
                Then
                MsgBox "Missing Value!"
             End If
             If Worksheets(1).Range("H12").Value = 1 _
                Then
                Workbooks.Open mySubFolder & "\" & strFile
                Range("A2:AT2").Copy
             End If
             If Worksheets(1).Range("H12").Value = 2 _
                Then
                Workbooks.Open mySubFolder & "\" & strFile
                Range("A2:AT3").Copy
             End If
             If Worksheets(1).Range("H12").Value = 3 _
                Then
                Workbooks.Open mySubFolder & "\" & strFile
                Range("A2:AT4").Copy
             End If
             If Worksheets(1).Range("H12").Value = 4 _
                Then
                Workbooks.Open mySubFolder & "\" & strFile
                Range("A2:AT5").Copy
             End If
             If Worksheets(1).Range("H12").Value = 5 _
                Then
                Workbooks.Open mySubFolder & "\" & strFile
                Range("A2:AT6").Copy
             End If
             Application.DisplayAlerts = False
             
             erow = Sheet1.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Row
             Worksheets("Sheet 1").Paste Destination:=Worksheets("Sheet 1").Cells(erow, 2)
             Worksheets("Sheet 1").Cells(erow, 1) = ActiveWorkbook.Name
             ActiveWorkbook.Close
             strFile = Dir
          Loop
       Next
    End Sub
    GC Excel

    If this post helps, then click the star icon (*) in the bottom left-hand corner of my post to Add reputation.

  3. #3
    Registered User
    Join Date
    10-03-2013
    Location
    Jacksonville, USA
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Copy and paste Filename to left of data

    This gives the name of the macro file itself and not the name of the file from which the data was copied from.

    Good idea though, I will maybe try to put it in at another point of the code.

  4. #4
    Registered User
    Join Date
    10-03-2013
    Location
    Jacksonville, USA
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Copy and paste Filename to left of data

    I tried to change it
    ActiveWorkbook.Name
    to
    strFile.Name
    But it said it was an invalid command :/

  5. #5
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Copy and paste Filename to left of data

    Use this instead
    Split(strFile,"\")(UBound(Split(strFile,"\")))

  6. #6
    Registered User
    Join Date
    10-03-2013
    Location
    Jacksonville, USA
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Copy and paste Filename to left of data

    I put it in like this.
    Worksheets("Sheet 1").Cells(erow, 1) = Split(strFile, "\")(UBound(Split(strFile, "\"))).Name
    I get an "Object Required" error

    I also tried it without the .Name and it gave an "Subscript out of Range" error.

    I am very bad with vba I apologize if I used it wrong.

  7. #7
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Copy and paste Filename to left of data

    Oh, seems like strFile itself is already the file name. So just use this:
    Worksheets("Sheet 1").Cells(erow, 1) = strFile

  8. #8
    Registered User
    Join Date
    10-03-2013
    Location
    Jacksonville, USA
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Copy and paste Filename to left of data

    Thank you that worked. The only caveat is that it only pastes it once. Sometimes the data has 4 lines of data and it won't paste it in front of the next three lines, is there any way to accomplish that?

  9. #9
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Copy and paste Filename to left of data

    Try:
    Worksheets("Sheet 1").Range(Cells(erow, 1), Worksheets("Sheet 1").Cells(Rows.Count, 2).End(xlUp).Offset(, -1)).Value = strFile
    Another thing is, the way you are referencing the worksheet is very inconsistent, you have:
    Worksheets(1)
    Sheet1
    Worksheets("Sheet 1")

    Not that it is a problem now, but may cause error if your worksheet is no longer named "Sheet 1".

  10. #10
    Registered User
    Join Date
    10-03-2013
    Location
    Jacksonville, USA
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Copy and paste Filename to left of data

    Thank you millz, I will make sure to make it more consistent. That fix worked perfectly.

  11. #11
    Registered User
    Join Date
    10-03-2013
    Location
    Jacksonville, USA
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Copy and paste Filename to left of data

    This change solved the problem but I found that it only worked when I had the worksheet open. If I run it while on a different worksheet it gives me an application-defined or object-defined error.

  12. #12
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Copy and paste Filename to left of data

    Try this amended code:
    Sub OpenSubfoldersFileUpdate()
        Dim strFile As String
        Dim objFSO, destRow As Long
        Dim mainFolder, mySubFolder
        Dim ws1, ws2 As Worksheet
        Set ws1 = Worksheets(1)
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        mFolder = ws1.Range("B12").Value
        If mFolder = 0 Then
           MsgBox " Address Missing"
        Else: Set mainFolder = objFSO.GetFolder(mFolder)
        End If
        For Each mySubFolder In mainFolder.subFolders
            strFile = Dir(mySubFolder & "\*.csv*")
            Do While strFile <> ""
                If ws1.Range("H12").Value = 0 Then
                   MsgBox "Missing Value!"
                ElseIf ws1.Range("H12").Value >= 1 And ws1.Range("H12").Value <= 5 Then
                   Workbooks.Open mySubFolder & "\" & strFile
                   Set ws2 = ActiveWorkbook.ActiveSheet
                End If
                If ws1.Range("H12").Value = 1 Then ws2.Range("A2:AT2").Copy
                If ws1.Range("H12").Value = 2 Then ws2.Range("A2:AT3").Copy
                If ws1.Range("H12").Value = 3 Then ws2.Range("A2:AT4").Copy
                If ws1.Range("H12").Value = 4 Then ws2.Range("A2:AT5").Copy
                If ws1.Range("H12").Value = 5 Then ws2.Range("A2:AT6").Copy
                Set ws2 = Nothing
                Application.DisplayAlerts = False
                
                erow = ws1.Cells(rows.Count, 2).End(xlUp).Offset(1, 0).row
                ws1.Paste Destination:=ws1.Cells(erow, 2)
                ws1.Range(Cells(erow, 1), ws1.Cells(rows.Count, 2).End(xlUp).Offset(, -1)).Value = strFile
                ActiveWorkbook.Close
                strFile = Dir
            Loop
        Next
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Copy slection and paste it in ceratian workbook with PART filename as cell value
    By Dibbley247 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 11-15-2012, 09:56 AM
  2. Replies: 0
    Last Post: 11-01-2012, 09:28 AM
  3. copy and paste a range of cells to the left
    By michaelram in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-04-2010, 12:24 AM
  4. Copy and paste filename
    By edk08 in forum Excel General
    Replies: 1
    Last Post: 07-03-2007, 09:05 AM
  5. [SOLVED] VB copy paste column data to another left to right sheet across sh
    By baz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-23-2006, 04:45 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1