+ Reply to Thread
Results 1 to 7 of 7

Trying to create a macro to update a large database easily

Hybrid View

  1. #1
    Registered User
    Join Date
    06-11-2015
    Location
    america
    MS-Off Ver
    2007
    Posts
    14

    Trying to create a macro to update a large database easily

    I have a workbook that uses columns A-J on the first sheet and is 18k+ rows long and new items are added to the list all the time.
    The value in Column A is the identifying value, a sku basically, and B-J is relative information pertaining to the sku. My team has to pull some of these skus out daily and use them in reports. BUT sometimes the information in B-J will need to be changed for the report AND permanently. And sometimes we have to add skus to the bottom of the list. For short reports updating info isn't a problem. But when reports are long (like 300 lines) it takes a long time to ctrl+f the value in column A and then change the necessary info in columns B-J.

    Here is what I would love to accomplish:
    Add a sheet to the end of the workbook where I can paste a finished report that still uses columns A-J. The changed info would be highlighted yellow. When the macro runs it would see the cell thats yellow, refer to the relative sku in column A, then find that sku in column A in sheet 1, then change only change the cell that needs to be updated. What would be even more awesome is if we need to add a completely new sku we could highlight the whole row (A-J) and then it would be added to the bottom of the list on sheet 1.

    I know this is a huge thing to ask...but if this is possible..you would be helping me tremendously. I would be very very appreciative. Thank you in advance for your help.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Trying to create a macro to update a large database easily

    I can imagine a simple macro that would:

    1) Cycle through every row on sheet2 to find the matching row on sheet1
    2) Copy/Paste the entire row/values from sheet2 back into sheet1. Not a cell/by/cell comparison of any kind, just copy/paste values.
    3) Anytime a searched value is NOT found on sheet1, it is added to the bottom of sheet1.

    Option Explicit
    
    Sub UpdateSheet1()
    Dim ws2 As Worksheet, ws1 As Worksheet
    Dim MyRng As Range, cell As Range, vFIND As Range
    
    Set ws1 = ThisWorkbook.Sheets("Sheet1")         'edit this sheet name as needed
    Set ws2 = ThisWorkbook.Sheets("Sheet2")         'edit this sheet name as needed
    
    Set MyRng = ws2.Range("A2:A" & ws2.Range("A" & Rows.Count).End(xlUp).Row)
    
    On Error Resume Next
    For Each cell In MyRng
        Set vFIND = ws1.Range("A:A").Find(cell.Value, LookIn:=xlValues, LookAt:=xlWhole)
        If Not vFIND Is Nothing Then
            cell.EntireRow.Copy
            vFIND.PasteSpecial xlPasteValues
            Set vFIND = Nothing
        Else
            cell.EntireRow.Copy
            ws1.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
        End If
    Next cell
    
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    06-11-2015
    Location
    america
    MS-Off Ver
    2007
    Posts
    14

    Re: Trying to create a macro to update a large database easily

    dude you are a genius...Im pretty sure this does what I need! you are the freakin man!

  4. #4
    Registered User
    Join Date
    06-11-2015
    Location
    america
    MS-Off Ver
    2007
    Posts
    14

    Re: Trying to create a macro to update a large database easily

    dude you are a genius...Im pretty sure this does what I need! you are the freakin man!
    one question though.. when the macro is done running, at the bottom of the excel window it says "click enter to paste" or something along those lines. Then it pastes the last line over the top of the first line on the second sheet. But everything works perfectly on the first sheet. It works perfectly other than that aesthetic mishap. But that sheet gets cleared out anyway. Thanks for your help. Seriously.

  5. #5
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,914

    Re: Trying to create a macro to update a large database easily

    Why not?

    Sub UpdateSheet1()
        Dim ws2 As Worksheet, ws1 As Worksheet
        Dim MyRng As Range, cell As Range, vFIND As Range
    
        Set ws1 = ThisWorkbook.Sheets("Sheet1")         'edit this sheet name as needed
        Set ws2 = ThisWorkbook.Sheets("Sheet2")         'edit this sheet name as needed
    
        Set MyRng = ws2.Range("A2:A" & ws2.Range("A" & Rows.Count).End(xlUp).Row)
    
        On Error Resume Next
        For Each cell In MyRng
            Set vFIND = ws1.Range("A:A").Find(cell.Value, LookIn:=xlValues, LookAt:=xlWhole)
            If Not vFIND Is Nothing Then
                vFIND.Resize(, 10).Value = cell.Resize(, 10).Value
                Set vFIND = Nothing
            Else
                ws1.Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(, 10).Value = cell.Resize(, 10).Value
            End If
        Next cell
    End Sub
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Trying to create a macro to update a large database easily

    We can just add one line at the end I think will resolve that.
    Option Explicit
    
    Sub UpdateSheet1()
    Dim ws2 As Worksheet, ws1 As Worksheet
    Dim MyRng As Range, cell As Range, vFIND As Range
    
    Set ws1 = ThisWorkbook.Sheets("Sheet1")         'edit this sheet name as needed
    Set ws2 = ThisWorkbook.Sheets("Sheet2")         'edit this sheet name as needed
    
    Set MyRng = ws2.Range("A2:A" & ws2.Range("A" & Rows.Count).End(xlUp).Row)
    
    On Error Resume Next
    For Each cell In MyRng
        Set vFIND = ws1.Range("A:A").Find(cell.Value, LookIn:=xlValues, LookAt:=xlWhole)
        If Not vFIND Is Nothing Then
            cell.EntireRow.Copy
            vFIND.PasteSpecial xlPasteValues
            Set vFIND = Nothing
        Else
            cell.EntireRow.Copy
            ws1.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
        End If
    Next cell
    Application.CutCopyMode = False
    End Sub
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.
    Last edited by JBeaucaire; 11-27-2016 at 03:25 AM.

  7. #7
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,914

    Re: Trying to create a macro to update a large database easily

    @ Jerry

    Check the last line you've added, please.

+ 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. create one large database, and 30+ partial ones, with a two-way connection
    By treepio in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-07-2015, 09:55 AM
  2. Forecasting in table- easily update & pivot capability
    By OffPisteTele in forum Excel General
    Replies: 4
    Last Post: 03-19-2015, 11:14 AM
  3. [SOLVED] Dynamic Update macro for autofill, rank, sum, large /w example
    By Hyflex in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 01-12-2012, 09:08 AM
  4. Replies: 5
    Last Post: 06-11-2006, 04:45 PM
  5. Replies: 2
    Last Post: 04-11-2006, 03:50 PM
  6. How can I easily update formulas that include new rows?
    By OldKenGoat in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 08-10-2005, 05:05 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