+ Reply to Thread
Results 1 to 8 of 8

Loop Through 1 Range (Down a Column) and Find Values that Do NOT Exist in another Range?

  1. #1
    Registered User
    Join Date
    08-10-2011
    Location
    Cali
    MS-Off Ver
    Excel 365
    Posts
    62

    Loop Through 1 Range (Down a Column) and Find Values that Do NOT Exist in another Range?

    Bit stuck on this one (had been on a roll previously).

    We have some reports that require importing data into a new sheet within a workbook each month (that much is fine) and then comparing the rows against the previous month.

    Example sheet attached with very basic random generated data, but what I want to do is loop through the new worksheet (19-Jan) in column B and look at the value(s) in B (IF column A is a 4, but I can probably add that later). I then want to compare each value in column B (in a row with a 4 in column A) to the range in column B on Sheet 18-Dec (last month, and again if Column A has a 4 at the same row).

    This would be an If statement, with the idea being that if B4 (value=1200.01.) on sheet 19-Jan exists somewhere in the range of B4:B1570 on sheet 18-Dec, then you move on.

    If value in B4 (1200.01.) on Sheet 19-Jan does not exist in range B4:B1570 on Sheet 18-Dec, then it would do something (either store in a string, prompt a msgbox, etc). This is part I am stuck on as I haven't really moved past for and if statements with VBA into index/match/search/etc which I am assuming is what we are looking for. The value has to be an exact match, and the end game would be to get a list at the end of the loop with all values in the range on 19-Jan that DO NOT exist in the range on 18-Dec.

    Any help appreciated, I will keep plugging away.

    Thanks,
    Last edited by Norcal1; 01-30-2019 at 09:13 PM.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Loop Through 1 Range (Down a Column) and Find Values that Do NOT Exist in another Rang

    You could do it with a formula. On sheet 19 Jan, put something like this in row 3 any empty column and copy down.

    =IF(A3=4,IF(ISERROR(MATCH(B3,'18-Dec'!B:B,0)),"No Match",""),"")

    You could have a macro insert the formula.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

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

    Re: Loop Through 1 Range (Down a Column) and Find Values that Do NOT Exist in another Rang

    Or something like this.
    Please Login or Register  to view this content.
    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.

  4. #4
    Registered User
    Join Date
    08-10-2011
    Location
    Cali
    MS-Off Ver
    Excel 365
    Posts
    62

    Re: Loop Through 1 Range (Down a Column) and Find Values that Do NOT Exist in another Rang

    Thanks for the responses, was/am trying to learn the right way to do this with all VBA but inserting the formula in a helper column may be my best bet to get this done now. I need to review more, but that snippet of VBA is (appears) way over my head at the moment.

    I was trying to do it with nested for + if loops which I am sure is not the most efficient way, but even that I hit a deadend at least for now. The idea was

    If A is 4, then
    Check value in B
    Go to other sheet
    If A is 4 then
    Check B against B from previous sheet, if match end loop
    If no match, go to next row
    Etc, etc

    Will give it a go again tomorrow

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

    Re: Loop Through 1 Range (Down a Column) and Find Values that Do NOT Exist in another Rang

    The code I provided does exactly that.
    Attached Files Attached Files
    Last edited by bakerman2; 01-31-2019 at 02:12 AM.

  6. #6
    Registered User
    Join Date
    08-10-2011
    Location
    Cali
    MS-Off Ver
    Excel 365
    Posts
    62

    Re: Loop Through 1 Range (Down a Column) and Find Values that Do NOT Exist in another Rang

    Bakerman.

    Thanks, I'll try and review and step through it. Beyond copy-pasting it into the editor, I don't think I could recreate it on my own yet so I'll study up and see if I can get a handle on what each line is doing so I can tweak to our final product. Thanks again

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

    Re: Loop Through 1 Range (Down a Column) and Find Values that Do NOT Exist in another Rang

    Please Login or Register  to view this content.
    This creates 2 dictionaries (some sort of containers)

    Please Login or Register  to view this content.
    This puts the values of Column A & B from Jan-sheet into an array and loops all values.
    When there's a 4 in column A it' puts the corresponding value of Column B into the first dictionary (dic )

    Please Login or Register  to view this content.
    This does the same but for december sheet (dic2)

    Please Login or Register  to view this content.
    This compares every element from the 1st dictionary (dic) with the second dictionary (dic2)
    If there's a match then the value gets deleted from the 1 dictionary (dic)
    Please Login or Register  to view this content.
    This puts the remaining values in 1st dictionary (dic) onto your worksheet.

  8. #8
    Registered User
    Join Date
    08-10-2011
    Location
    Cali
    MS-Off Ver
    Excel 365
    Posts
    62

    Re: Loop Through 1 Range (Down a Column) and Find Values that Do NOT Exist in another Rang

    Awesome, thank you for the explanation. I had to change "Sheet4" to "Sheets("19-Jan") and "Sheet1" to "Sheets("18-Dec") or else I was getting a 424 error on the first with statement.

    Really appreciate the step explanations - few questions if you have time:

    We are setting dic and dic2 to be objects (just a location to store the arrays of the two ranges, correct?)

    Is "sn" a variable of some type? Does it not need to be declared? (still very green with all the but the basics)

    Is the "range within a range" just saying to start at A3, then count down the number of rows and that is your new range, then resize that range (with A3 still as the upper left reference point) no additional rows but 1 additional column wider to capture column B as well, and then store that as range as "sn"?

    Assuming UBound is the "upper bound" of sn rows ie the last row? The for then loops down the range in sn and like said, if A(i) = 4 then store B(i) within the dictionary established as dic2?

    What role does x0 play? Is that another variable or some type of assignment to the dictionary unique to it?

    The For Each Key makes sense, I just probably never would have known that syntax existed, thanks!

    Is the final transpose just because the dictionary is implicity storing those values in a 1 Row, multiple column array and we want it in a multi row, 1 column array?

    Thanks again for the help. Either way this seems to work and I'll keep reviewing it, I just like to fully understand the code I've been adding so that I can understand how and where to tweak it to do different things.





    Quote Originally Posted by bakerman2 View Post
    Please Login or Register  to view this content.
    This creates 2 dictionaries (some sort of containers)

    Please Login or Register  to view this content.
    This puts the values of Column A & B from Jan-sheet into an array and loops all values.
    When there's a 4 in column A it' puts the corresponding value of Column B into the first dictionary (dic )

    Please Login or Register  to view this content.
    This does the same but for december sheet (dic2)

    Please Login or Register  to view this content.
    This compares every element from the 1st dictionary (dic) with the second dictionary (dic2)
    If there's a match then the value gets deleted from the 1 dictionary (dic)
    Please Login or Register  to view this content.
    This puts the remaining values in 1st dictionary (dic) onto your worksheet.

+ 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] Check if other strings exist in column based on range string values
    By jeroenft in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 11-12-2018, 11:47 AM
  2. [SOLVED] Check if other strings exist in column based on range string values
    By jeroenft in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-12-2018, 07:49 AM
  3. [SOLVED] Find and Replace Loop that looks up values in range for Search criteria
    By lougs7 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-06-2018, 04:31 PM
  4. Find text in range of cells and add to table if it does not exist in range
    By dgadler1 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 02-12-2013, 12:54 PM
  5. Replies: 3
    Last Post: 03-08-2012, 03:32 PM
  6. Macro Loop to Extract Specific Values and Min/Max Values From Column/Rows Range
    By ExcelQuestFL in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 12-19-2010, 09:19 AM
  7. Need to find specific values in range using loop
    By frk1980 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-10-2005, 10:52 PM

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