+ Reply to Thread
Results 1 to 6 of 6

Copying/Pasting Data from All Files in Folder to Master Sheet According to Headers

Hybrid View

  1. #1
    Registered User
    Join Date
    07-21-2015
    Location
    Howell, Michigan
    MS-Off Ver
    MS Office 2010
    Posts
    3

    Copying/Pasting Data from All Files in Folder to Master Sheet According to Headers

    Hello all,

    I'm new to coding vba. The goal of my macro would be to loop through all files in a folder running a macro that rearranges the files' format and copies the desired range. Then copy this range of data to a master sheet matching the data range to the correct header which is a date.

    So far I have code on the personal workbook that formats the individual files and copies the data range I want. I know there is code that can loop this macro through a folder running it on each file; I just have not integrated this code with mine yet. But, I do not know how I will be to match the copied data from the individual files to the column headers they need to be pasted under. It'd be much appreciated if anyone could help with this.

    The macro on the personal workbook reformats this original txt file.

    Original txt-min.png

    The macro turns the original text file into the format below.

    after mac.png

    So the overall vba code should be activated by the Weld Scrap Dollars button, call my macro (which is called WeldScrap()), and paste scrap dollar values to master sheet according to code and date.

    transfer2.png

    If anyone could help with this I'd really appreciate it!

    The vba code on the personal workbook I put in a word file attached to look at sorry for the inconvenience.
    Several input files and a skeleton of the master sheet are attached. I would attach the full master sheet if it wasn't due to file size constraints.
    Attached Files Attached Files
    Last edited by sdwhiteh; 07-22-2015 at 10:21 AM.

  2. #2
    Registered User
    Join Date
    07-21-2015
    Location
    Howell, Michigan
    MS-Off Ver
    MS Office 2010
    Posts
    3

    Re: Copying/Pasting Data from All Files in Folder to Master Sheet According to Headers

    Sub Weld_Scrap()
    
    'Copying Column with date selected in it
    Columns("I").Copy
    Columns("X").Select
    ActiveSheet.Paste
    
    'Deleting unnecessary cells
    Rows("1:7").Delete
    Columns("B:Q").Delete
    
    'Selecting range for dictionary
    
        Dim rng As Range, dict As Object
    
    'Offsets range selected from column A
        Set rng = Range(Range("A2"), Cells(Rows.Count, 1).End(xlUp)).Resize(, 2)
    
    'Puts associated dollar values into column D and sets Subtotals function
        Set dict = SubTotals(rng, 1, 2)
        DumpDict dict, Range("D1")
    
    End Sub
    
    'k =  code
    'v = dollars
    'Creates dictionary using a range and an object
    'Also runs subtotal
    Function SubTotals(rng As Range, colKey As Long, colVal As Long) As Object
        Dim rv As Object, rw As Range, k, v
        Set rv = CreateObject("scripting.dictionary")
        For Each rw In rng.Rows
            k = rw.Cells(colKey).Value
            v = rw.Cells(colVal).Value
            If Not IsError(k) And Not IsError(v) Then
                If Len(k) > 0 And IsNumeric(v) Then
                    rv(k) = rv(k) + v
                End If
            End If
        Next rw
        Set SubTotals = rv
    End Function
    'Offsets value of column A so that the values being subtotaled are the values associated with their scrap codes
    Sub DumpDict(dict As Object, rng As Range)
        Dim i As Long, k
        i = 0
        For Each k In dict.keys
            With rng.Cells(1)
                .Offset(i, 0).Value = k
                .Offset(i, 1).Value = dict(k)
            End With
            i = i + 1
        Next
        
    'Now the codes are matched with their subtotals
        
    'Deleting all rows that don't contain a scrap code
    Dim dontDelete
    dontDelete = Array("7101", "7102", "7103", "7104", "7105", "7106", "7107", "7108", "7109", "7110", "7111", "7112", "7113", "7114", "7115", "7116")
    
    Dim o As Long, p As Long
    
    Dim isThere As Boolean
    
    'Loop for deleting, shifts cells up
    For o = Range("D" & Rows.Count).End(xlUp).Row To 1 Step -1
        For p = LBound(dontDelete) To UBound(dontDelete)
            If StrComp(Range("D" & o), dontDelete(p), vbTextCompare) = 0 Then
                isThere = True
            End If
        Next p
        If Not isThere Then
            Range("D" & o).EntireRow.Delete Shift:=xlUp
        End If
        isThere = False
    Next o
    
    'Sorts codes from least to greatest
    Dim lr As Long
    Application.ScreenUpdating = False
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    Range("E1:D" & lr).Sort key1:=Range("D1"), order1:=1
    Application.ScreenUpdating = True
    
    'Inserts blank cells for scrap codes that have no values
        Dim m As Long, z, r As Range
        For m = Range("d" & Rows.Count).End(xlUp).Row To 2 Step -1
            z = Mid$(Cells(m, "d"), 2) - Mid$(Cells(m - 1, "d"), 2)
            If z > 1 Then
                Rows(m).Resize(z - 1).Insert
                Cells(m - 1, "d").AutoFill Cells(m - 1, "d").Resize(z), 2
            End If
        Next
        
    'Giving headers to data
    Rows(1).Insert Shift:=xlDown
    Range("$D$1").Value = "Scrap Code"
    Range("$E$1").Value = "Scrap Dollars"
    
    'Puts date in A1 for loop to recognize
    With ActiveSheet
    LastVal = Range("H65536").End(xlUp)
    End With
    Range("A1") = LastVal
    
    'Copies range of cells
    
    End Sub

  3. #3
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Question Master sheet


    Hi !

    Quote Originally Posted by sdwhiteh View Post
    […] a macro that rearranges the files' format and copies the desired range.
    Then copy this range of data to a master sheet
    Seems you forgot to explain what is desired range ! And where is master sheet ?! Attach the workbook …

  4. #4
    Registered User
    Join Date
    07-21-2015
    Location
    Howell, Michigan
    MS-Off Ver
    MS Office 2010
    Posts
    3

    Re: Master sheet

    The desired range of data to copy would be the cells in the green box under "Scrap Dollars" in the second picture. So it would be E2:E17. And, sorry it forced me to wait an hour before I could upload any more attachments, but here is the master sheet attached.Scrap Master Sheet Rev3.xlsx

  5. #5
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Exclamation Text files issue …


    Not all very obvious but for now I can't read your attached text files with my usual code :
    got an error #62 Input past end of file

    If there are not your true files, attach a couple of real ones. Otherwise, I go my way …

  6. #6
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Exclamation Duplicate …

    Duplicate …
    Last edited by Marc L; 07-22-2015 at 01:27 PM.

+ 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. Copying columns from one sheet and pasting to another based on headers
    By magnus12 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-08-2015, 10:43 PM
  2. Marcto to copy sheets from one workbook to a master sheet, for all files in folder
    By crombieguy91 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-14-2013, 11:12 AM
  3. copying data from excel files in a source folder to target folder
    By Javed07 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-22-2013, 04:27 PM
  4. Opening 4 excel files, copying set table data and pasting into "master file".
    By Belloni in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-29-2012, 09:50 AM
  5. Copying data from multiple files in a folder to a master file
    By kjshep in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-22-2012, 12:31 PM
  6. Pasting data from a master file to two other files
    By Bob@Sun in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-17-2009, 02:50 PM
  7. VBA Code for copying/pasting from Master-to-target files
    By Pedroluna in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-02-2009, 09:34 AM

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