+ Reply to Thread
Results 1 to 17 of 17

Compare columns and insert values in correct place

  1. #1
    Registered User
    Join Date
    07-17-2013
    Location
    Lisbon, Portugal
    MS-Off Ver
    Excel 2003
    Posts
    14

    Compare columns and insert values in correct place

    Hi! So, I have 2 very big columns with numbers that i want to compare. If a number in column C of the worksheet CADIM is also in column B of worksheet sheet3 it's ok, so I go to the next number. When a number is not in sheet3, I want to insert it in the correct position. Both columns are in decreasing order (although they have different sizes).
    Example:
    columnC(CADIM):
    10
    9
    6
    2

    columnB(sheet3):
    11
    10
    6
    2
    1

    In this case the only number of CADIM that is not in sheet3 is the 9, so I need to insert a row between row2 and row3 and write 9 in it.
    This is what I got so far, I'm only starting to learn VBA and I can't make this work... The current error is the first line in yellow, which I don't know what it means, and when I click the button with the macro nothing happens...

    Any help would be appreciated!!

    Please Login or Register  to view this content.
    Last edited by HugoRibeiro; 07-18-2013 at 07:56 AM.

  2. #2
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,601

    Re: Compare columns and insert values in correct place

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

  3. #3
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Compare columns and insert values in correct place

    Hi Hugo,
    try so
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    05-28-2013
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    45

    Re: Compare columns and insert values in correct place

    Make sure you put that code into a module and not directly into a sheet

  5. #5
    Registered User
    Join Date
    07-17-2013
    Location
    Lisbon, Portugal
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Compare columns and insert values in correct place

    Hey nilem, thanks for such a quick answer!

    I opened Sheet3, clicked on view code, put there that code and saved, but do I need to do anything else for it to work? Because when I click on the macro I have nothing happens...
    Btw, relating to the code you wrote, do both sheets CADIM and Sheet3 need to be on the same workbook?

  6. #6
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Compare columns and insert values in correct place

    Paste this code into a standard module (Module1). Sheets CADIM and Sheet3 must be in the same book (the active workbook)

  7. #7
    Registered User
    Join Date
    07-17-2013
    Location
    Lisbon, Portugal
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Compare columns and insert values in correct place

    It works perfectly!! Thank you so much!! :D

  8. #8
    Registered User
    Join Date
    07-17-2013
    Location
    Lisbon, Portugal
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Compare columns and insert values in correct place

    Can you make a small modification? When copying the new values, I would like to copy the cell in column B (which is a small "index" that can be 1A,2A,3A or 4A) together with the one in cell C, so that on Sheet3 I have on column A that index together with the new number on column B. I don't need to compare those "indexes" (because they don't vary with the same number), I just need to compare the number and then insert the new number together with it's index on Sheet3.
    Example:
    columns B and C (CADIM):
    4A 10
    3A 9
    2A 6
    1A 2

    columns A and B (sheet3):
    4A 11
    4A 10
    2A 6
    1A 2
    1A 1

    Thanks again!

  9. #9
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Compare columns and insert values in correct place

    try
    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    07-17-2013
    Location
    Lisbon, Portugal
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Compare columns and insert values in correct place

    Great!! Thanks a lot!

  11. #11
    Registered User
    Join Date
    07-17-2013
    Location
    Lisbon, Portugal
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Compare columns and insert values in correct place

    I'm afraid there's a little problem, sorry to bother again

    I was trying the code in a test file, with only 2 columns on each sheet, and it all worked well. However, I tried to implement it on the file I really need, which has a lot of columns on Sheet3, and the code only moved 2 columns down. So I did some alterations to the code (the columns have changed) and this is what I have now:

    Please Login or Register  to view this content.
    Sheet3 was already done by someone and sheet CADIM was made by me, so now I'm using ONLY columns A and B. The ideal way to do what I need would be to insert an entire row on Sheet3 and then write in columns B and C the corresponding values of columns A and B from CADIM (all the other columns of that row would be left blank).
    37em.jpg
    What is happening now is it does insert the new values on columns B and C as I want, but it also inserts random (?) values on the rest of the columns. It appears that it copies those values from some other row, but I can't tell why because they are too many.
    Last edited by HugoRibeiro; 07-19-2013 at 11:00 AM.

  12. #12
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Compare columns and insert values in correct place

    Hugo, in this case you need to attach a sample of your file (if your file contains confidential data, replace them with bananas or oranges)

  13. #13
    Registered User
    Join Date
    07-17-2013
    Location
    Lisbon, Portugal
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Compare columns and insert values in correct place

    Do you need me to attach one? I attached a printscreen, the rest of the file is just more numbers...

  14. #14
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Compare columns and insert values in correct place

    need a file .xls, not a picture

  15. #15
    Registered User
    Join Date
    07-17-2013
    Location
    Lisbon, Portugal
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Compare columns and insert values in correct place

    Sorry, I didn't read well the first time what you said. Here goes the file, maybe you'll find it a bit more complicated than at first.

    The sheet I want to write in is "Base de Dados" (the new lines will have the new Numbers and respective Type and the rest of the columns will be left blank, to later be filled manualy one by one), and I want to insert the numbers from sheet CADIM and also from sheet AUTOCAD.
    Module3 is the one i'm writing in, you can delete what is there.

    Have a nice weekend!
    Attached Files Attached Files

  16. #16
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Compare columns and insert values in correct place

    I made changes in Module1 and Module3 and deleted all the unnecessary from a file.
    see attachment
    Attached Files Attached Files

  17. #17
    Registered User
    Join Date
    07-17-2013
    Location
    Lisbon, Portugal
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Compare columns and insert values in correct place

    Amazing! Thank you so much, I hope God pays you! :P

+ 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. [SOLVED] Compare 4 column values then insert a 5th columns value to a blank column/cell value
    By JasonKMcCoy in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 10-27-2012, 06:01 PM
  2. Replies: 1
    Last Post: 09-05-2012, 04:11 PM
  3. Replies: 0
    Last Post: 08-03-2012, 04:31 PM
  4. Compare columns in worksheets and move to correct sheet.
    By CRIMEDOG in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-29-2011, 12:25 PM
  5. [SOLVED] How do I create a macro that will compare columns and place data
    By CompuCat in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 03-20-2006, 02:25 PM

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