+ Reply to Thread
Results 1 to 11 of 11

Code to update or insert if doesnt exist

  1. #1
    Registered User
    Join Date
    11-03-2010
    Location
    Detroit, MI
    MS-Off Ver
    Excel 2010
    Posts
    65

    Code to update or insert if doesnt exist

    I have 2 worksheets. A master and a monthly update. Both contain student number (primary key) and fields like address, phone number, etc. I am trying to update the master sheet with any changes from the monthly file or insert a row if it is a new student. So for example if student 1 exists but has a new phone number it will update that cell, if student 1000 does not exist in the master it will insert a new row.

    Both sheets have the exact same format and same column headers. In SQL I would do a normal update/insert process but is there an easy way to loop through and perform the same in VBA?

    Thank you in advance!

  2. #2
    Forum Expert Vikas_Gautam's Avatar
    Join Date
    06-04-2013
    Location
    Ludhiana,Punjab, India
    MS-Off Ver
    Excel 2013
    Posts
    1,850

    Re: Code to update or insert if doesnt exist

    As it will involve VBA code, it will need some testing as well.
    So
    Attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic
    Regards,
    Vikas Gautam
    Excel-buzz.blogspot.com

    Excel is not a matter of Experience, its a matter of Application.

    Say Thanks, Click * Add Reputation

  3. #3
    Registered User
    Join Date
    11-03-2010
    Location
    Detroit, MI
    MS-Off Ver
    Excel 2010
    Posts
    65

    Re: Code to update or insert if doesnt exist

    I think it is fairly straightforward. Sheet 1 (aka master) has columns studentid, name, address, city, state, .....; Sheet 2 (aka 2015 updates) has same columns studentid, name, address, city, state. VBA code would read through Sheet 2, find the matching student in sheet1 and update name, address, city, state, etc. If no matching student is found in sheet1, then the new student would be inserted into sheet1.
    Does that make sense? Is that straightforward enough or do I need to mock up a excel doc?
    Thank you very much in advance for your help!

  4. #4
    Forum Expert Vikas_Gautam's Avatar
    Join Date
    06-04-2013
    Location
    Ludhiana,Punjab, India
    MS-Off Ver
    Excel 2013
    Posts
    1,850

    Re: Code to update or insert if doesnt exist

    Okay then try this code:-
    Please Login or Register  to view this content.
    Last edited by Vikas_Gautam; 01-24-2015 at 01:13 AM.

  5. #5
    Registered User
    Join Date
    11-03-2010
    Location
    Detroit, MI
    MS-Off Ver
    Excel 2010
    Posts
    65

    Re: Code to update or insert if doesnt exist

    Thank you. I am in the process of testing it and will let you know. Thank you for taking the time to help.

    One question I was wondering about is if in the future I decided to only update/insert columns A-E for example is there an easy way to change "ID.EntireRow.Copy mSh.Cells(Lr, 1)" to just update a particular range?

    Thanks.

  6. #6
    Registered User
    Join Date
    11-03-2010
    Location
    Detroit, MI
    MS-Off Ver
    Excel 2010
    Posts
    65

    Re: Code to update or insert if doesnt exist

    Looks like I am getting a "application defined or object defined error". Could it be that 'LR' is not defined nor populated? the error appears on the first record and on line...."ID.EntireRow.Copy mSh.Cells(Lr, 1) 'updating the current record."

    Thank you

  7. #7
    Forum Expert Vikas_Gautam's Avatar
    Join Date
    06-04-2013
    Location
    Ludhiana,Punjab, India
    MS-Off Ver
    Excel 2013
    Posts
    1,850

    Re: Code to update or insert if doesnt exist

    Sorry, was my mistake (highlighted in red)
    I have adjusted the code to cover only A-E columns.

    Just make sure that the Sheet Names match with your actual workbook and column A contains StudentId.

    Please Login or Register  to view this content.
    Last edited by Vikas_Gautam; 01-25-2015 at 06:58 AM.

  8. #8
    Registered User
    Join Date
    11-03-2010
    Location
    Detroit, MI
    MS-Off Ver
    Excel 2010
    Posts
    65

    Re: Code to update or insert if doesnt exist

    Thank you very much, this seems to be working great. I have one more question that happens occasionally but maybe this code could handle it. Both these spreadsheets have the same column names. Is it possible to change the update/insert to insert/update all the columns in the '2015 update' sheet? So for example if the update sheet was missing 'state', then the address, city, etc would still update. I usually check the 'update sheet' to make sure all the columns match and in same order but maybe this code can read the column names in '2015 update' and update the matching column/cell in the master sheet?

    Thank you again for all your help!!

  9. #9
    Forum Expert Vikas_Gautam's Avatar
    Join Date
    06-04-2013
    Location
    Ludhiana,Punjab, India
    MS-Off Ver
    Excel 2013
    Posts
    1,850

    Re: Code to update or insert if doesnt exist

    Try this... Untested one.. Hopefully will do the job..

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    11-03-2010
    Location
    Detroit, MI
    MS-Off Ver
    Excel 2010
    Posts
    65

    Re: Code to update or insert if doesnt exist

    Thank you! The row "mSh.Cells(RowFnd, Col.Column) = ID.Cells(ID.Row, ColFnd)" is returning a type mismatch error.

    Thanks again!!
    Last edited by bosco2; 01-25-2015 at 09:26 PM.

  11. #11
    Forum Expert Vikas_Gautam's Avatar
    Join Date
    06-04-2013
    Location
    Ludhiana,Punjab, India
    MS-Off Ver
    Excel 2013
    Posts
    1,850

    Re: Code to update or insert if doesnt exist

    Okay.. this one is working...
    Assuming Row 1 is column head..

    Please Login or Register  to view this content.
    Check the attached file..
    Attached Files Attached Files

+ 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. How to add a value to the last blank cell of a column if it doesnt exist there already.
    By yoursamrit2000 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-20-2015, 04:42 AM
  2. [SOLVED] Find Value in all worksheets, if doesnt exist then...
    By bg_enigma1 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-06-2012, 12:30 PM
  3. If named range doesnt exist next k
    By jordan2322 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-13-2012, 12:17 AM
  4. Delete row if value(array) doesnt exist in column
    By wobaby in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-24-2009, 02:34 AM
  5. find number that doesnt exist among others.
    By TheBean in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-26-2006, 09:28 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