+ Reply to Thread
Results 1 to 15 of 15

Macro to combine daily extract to existing worksheet

Hybrid View

  1. #1
    Registered User
    Join Date
    09-24-2014
    Location
    Calgary, AB
    MS-Off Ver
    Microsoft Office 2007
    Posts
    10

    Macro to combine daily extract to existing worksheet

    I need to export a spreadsheet daily and combine it with an existing spreadsheet for tracking.

    What I need to accomplish is a macro that will run a check on the new worksheet (Export) and existing worksheet (Sheet1) to combine them, marking all items that are not present on the Export as complete on Sheet1 and adding any new items from the Export to Sheet1. Every line has a unique identifier (ItemID) so I am guessing that the best way to accomplish this would be with a v-lookup using this, however my experience with this in a macro is very limited. I only need to extract information from specific columns from the Export, and the columns don't line up, also there are never a set number of lines on either worksheet.

    On the Sheet1 I have 19 columns with A being ItemID. On the Export I have 13 columns with L being the ItemID.

    If the ItemID on Sheet1 is no longer on the Export, an "X" needs to be put in Column S (Completed) if one does not already exist.

    If the ItemID on Sheet1 is still on the Export, however an "X" appears in the Completed column, highlight the line.

    If the ItemID is not on Sheet1 create a new line and copy the information from the same line as the ItemID in the Export.

    Copy from Export to Sheet1
    Column L - Column A
    Column A - Column E
    Column B - Column F
    Column D - Column G
    Column E - Column H
    Column F - Column J
    Column G - Column K
    Column J - Column L
    Column K - Column M

    I don't know if I am going about this the right way or if there is an easier way, any help would be greatly appreciated.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,704

    Re: Macro to combine daily extract to existing worksheet

    In which column does the unique ID appear on the Export Sheet and in which column does the unique ID appear in Sheet1.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    09-24-2014
    Location
    Calgary, AB
    MS-Off Ver
    Microsoft Office 2007
    Posts
    10

    Re: Macro to combine daily extract to existing worksheet

    Column A on Sheet1 and Column L on Export. Sorry if I didn't make that very clear, I was trying to convey any relevant information.

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro to combine daily extract to existing worksheet

    Hi,

    Welcome to the forum - I'll have something for you tomorrow!
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  5. #5
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro to combine daily extract to existing worksheet

    It's untested as there was no sample provided - but see if it works:

    Sub ThirteenThree(): Dim w1 As Worksheet, we As Worksheet, e As Long, r As Long
    Set w1 = Sheets("Sheet1"): Set we = Sheets("Export")
    With CreateObject("Scripting.Dictionary")
    For e = 1 To we.Range("L" & Rows.Count).End(xlUp).Row
    ID = Range("L" & e): .Item(ID) = e: Next e
    For r = 1 To w1.Range("A" & Rows.Count).End(xlUp).Row: ID = Range("A" & r)
    If Not .Exists(ID) Then
    w1.Range("S" & r) = "X"
    ElseIf w1.Range("S" & r) = "X" Then
    w1.Range("S" & r).EntireRow.Interior.ColorIndex = 6
    End If: Next r: End With
    With CreateObject("Scripting.Dictionary")
    For r = 1 To w1.Range("A" & Rows.Count).End(xlUp).Row: ID = Range("A" & r)
    .Item(ID) = r: Next r
    For e = 1 To we.Range("L" & Rows.Count).End(xlUp).Row: ID = Range("L" & e)
    If Not .Exists(ID) Then
    w1.Range("A" & r) = we.Range("L" & e)
    w1.Range("E" & r) = we.Range("A" & e)
    w1.Range("F" & r) = we.Range("B" & e)
    w1.Range("G" & r) = we.Range("D" & e)
    w1.Range("H" & r) = we.Range("E" & e)
    w1.Range("J" & r) = we.Range("F" & e)
    w1.Range("K" & r) = we.Range("G" & e)
    w1.Range("L" & r) = we.Range("J" & e)
    w1.Range("M" & r) = we.Range("K" & e)
    r = r + 1: End If: Next e: End With
    End Sub

  6. #6
    Registered User
    Join Date
    09-24-2014
    Location
    Calgary, AB
    MS-Off Ver
    Microsoft Office 2007
    Posts
    10

    Re: Macro to combine daily extract to existing worksheet

    As is, it marks all lines on Sheet1 as complete and adds all lines from Export including the header.

    Due to the sensitive data, I cannot share the spreadsheet, however I will try and create a fake sheet for an example.

    Thank you again for your time and effort.

  7. #7
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro to combine daily extract to existing worksheet

    I didn't know there was a header and I was thinking that the procedure should be run in reverse and, in case of discrepancies between sheets, I've added a little sanitizing of the ID's - try:

    Sub ThirteenThree(): Dim w1 As Worksheet, we As Worksheet, e As Long, r As Long
    Set w1 = Sheets("Sheet1"): Set we = Sheets("Export")
    
    With CreateObject("Scripting.Dictionary")
    For r = 2 To w1.Range("A" & Rows.Count).End(xlUp).Row
    ID = Trim(LCase(Range("A" & r))): .Item(ID) = r: Next r
    For e = 2 To we.Range("L" & Rows.Count).End(xlUp).Row
        ID = Trim(LCase(Range("L" & e)))
    If Not .Exists(ID) Then
    w1.Range("A" & r) = we.Range("L" & e)
    w1.Range("E" & r) = we.Range("A" & e)
    w1.Range("F" & r) = we.Range("B" & e)
    w1.Range("G" & r) = we.Range("D" & e)
    w1.Range("H" & r) = we.Range("E" & e)
    w1.Range("J" & r) = we.Range("F" & e)
    w1.Range("K" & r) = we.Range("G" & e)
    w1.Range("L" & r) = we.Range("J" & e)
    w1.Range("M" & r) = we.Range("K" & e)
    r = r + 1: End If: Next e: End With
    
    With CreateObject("Scripting.Dictionary")
    For e = 2 To we.Range("L" & Rows.Count).End(xlUp).Row
    ID = Trim(LCase(Range("L" & e))): .Item(ID) = e: Next e
    For r = 2 To w1.Range("A" & Rows.Count).End(xlUp).Row
        ID = Trim(LCase(Range("A" & r)))
    If Not .Exists(ID) Then
    w1.Range("S" & r) = "X"
    ElseIf w1.Range("S" & r) = "X" Then
    w1.Range("S" & r).EntireRow.Interior.ColorIndex = 6
    End If: Next r: End With
    
    End Sub

  8. #8
    Registered User
    Join Date
    09-24-2014
    Location
    Calgary, AB
    MS-Off Ver
    Microsoft Office 2007
    Posts
    10

    Re: Macro to combine daily extract to existing worksheet

    Sample Sheet.xlsm

    It still appears to copy everything and marking everything complete.

    I have attached a sample that is like what I am looking at.

    Thank you again,

  9. #9
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro to combine daily extract to existing worksheet

    It lacked a little specificity - what about sheet2 and sheet3?

    Sub ThirteenThree(): Dim w1 As Worksheet, we As Worksheet, e As Long, r As Long
    Dim ID As String: Set w1 = Sheets("Sheet1"): Set we = Sheets("Export")
    
    With CreateObject("Scripting.Dictionary")
    For r = 2 To w1.Range("A" & Rows.Count).End(xlUp).Row
    ID = Trim(LCase(w1.Range("A" & r))): .Item(ID) = r: Next r
    For e = 2 To we.Range("L" & Rows.Count).End(xlUp).Row
        ID = Trim(LCase(we.Range("L" & e)))
    If Not .Exists(ID) Then
    w1.Range("A" & r) = we.Range("L" & e)
    w1.Range("E" & r) = we.Range("A" & e)
    w1.Range("F" & r) = we.Range("B" & e)
    w1.Range("G" & r) = we.Range("D" & e)
    w1.Range("H" & r) = we.Range("E" & e)
    w1.Range("J" & r) = we.Range("F" & e)
    w1.Range("K" & r) = we.Range("G" & e)
    w1.Range("L" & r) = we.Range("J" & e)
    w1.Range("M" & r) = we.Range("K" & e)
    r = r + 1: End If: Next e: End With
    
    With CreateObject("Scripting.Dictionary")
    For e = 2 To we.Range("L" & Rows.Count).End(xlUp).Row
    ID = Trim(LCase(we.Range("L" & e))): .Item(ID) = e: Next e
    For r = 2 To w1.Range("A" & Rows.Count).End(xlUp).Row
        ID = Trim(LCase(w1.Range("A" & r)))
    If Not .Exists(ID) Then
    w1.Range("S" & r) = "X"
    ElseIf w1.Range("S" & r) = "X" Then
    w1.Range("S" & r).EntireRow.Interior.ColorIndex = 6
    End If: Next r: End With
    
    End Sub

  10. #10
    Registered User
    Join Date
    09-24-2014
    Location
    Calgary, AB
    MS-Off Ver
    Microsoft Office 2007
    Posts
    10

    Re: Macro to combine daily extract to existing worksheet

    Sheet3 is nothing and doesn't need to be there. Sheet2 contains dropdowns for some of the columns in Sheet1.

    That is really a thing of beauty, works great! I wish that I understood everything that took place, and I will try to, but it currently goes a bit above my head.

    I honestly could not thank you enough, this is going to make life so much easier and the function of the spreadhseet more efficient. This was something we did once a month manually while archiving all competed items.

    Thank you again.
    Last edited by 13373; 09-25-2014 at 05:04 PM.

  11. #11
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro to combine daily extract to existing worksheet

    So - Did it work??

  12. #12
    Registered User
    Join Date
    09-24-2014
    Location
    Calgary, AB
    MS-Off Ver
    Microsoft Office 2007
    Posts
    10

    Re: Macro to combine daily extract to existing worksheet

    Yes - Yes it did!!

    Sorry, I just edited my last post, should have posted a new one.

    Works amazing and I greatly appreciate your help.

  13. #13
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro to combine daily extract to existing worksheet

    The Scripting Dictionary is a wonderful matching tool:
    With CreateObject("Scripting.Dictionary") ‘Initializes a “dictionary”
    'With this code we build the “Words”(ID’S) and their meanings(Rows)
    For e = 2 To we.Range("L" & Rows.Count).End(xlUp).Row
    ID = Trim(LCase(we.Range("L" & e))): .Item(ID) = e: Next e
    'Now we scan the Sheet1 ID’S to see whether they match in the Exports
    For r = 2 To w1.Range("A" & Rows.Count).End(xlUp).Row
        ID = Trim(LCase(w1.Range("A" & r)))
    If Not .Exists(ID) Then  ‘I hope this code speaks for itself
    w1.Range("S" & r) = "X"
    ElseIf w1.Range("S" & r) = "X" Then
    w1.Range("S" & r).EntireRow.Interior.ColorIndex = 6
    End If: Next r: End With

  14. #14
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro to combine daily extract to existing worksheet

    You're welcome and thanks for both the compliment and for the rep!

  15. #15
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Macro to combine daily extract to existing worksheet

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

+ 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. Replies: 11
    Last Post: 11-04-2013, 04:32 PM
  2. Replies: 3
    Last Post: 10-14-2013, 03:06 PM
  3. Extract existing IDs of entries repeated in a worksheet in a separate column
    By harsh2209 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-07-2011, 07:56 AM
  4. Macro to create new worksheet with values from existing worksheet
    By SamBam in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-18-2011, 05:33 AM
  5. Activating a worksheet with a daily changing name, using a single macro
    By Sccye in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-13-2010, 11:06 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