I have two ranges that should be identical (albeit they may be sorted differently). I’m trying to find any values in rangeA that are not in rangeB.
I’m able to find examples that show if values ARE matched in a range, but struggling to find anything if they aren’t.
So far I have:
Sub Compare2()
Dim test1, cell As Range
Dim FoundRange As Range
Set test1 = Sheets("Names").Range("A1:A5")
For Each cell In test1
Set FoundRange = Sheets("Queue & Status").Range("A1:A200").Find(what:=test1, LookIn:=xlFormulas, lookat:=xlWhole)
If FoundRange Is Nothing Then
MsgBox (cell & " not found")
End If
Next cell
End Sub
But it’s showing all values as being not matched, when they are.
asked Dec 2, 2016 at 2:34
4
Try this
Sub Compare2()
Dim test1 As Range
Dim lookIn As Range
Dim c As Range
Dim FoundRange As Range
Set test1 = Sheets("Names").Range("A1:A5")
Set lookIn = Sheets("Queue & Status").Range("A1:A200")
For Each c In test1
Set FoundRange = lookIn.Find(what:=c.Value, lookIn:=xlFormulas, lookat:=xlWhole)
If FoundRange Is Nothing Then
MsgBox (c.Value & " not found")
End If
Next c
End Sub
answered Dec 2, 2016 at 3:03
nightcrawler23nightcrawler23
2,0461 gold badge14 silver badges22 bronze badges
1
what:=test1 should be what:=cell (or even what:=cell.value)
answered Dec 2, 2016 at 2:49
S MS M
133 silver badges4 bronze badges
EXPLANATION
This tutorial shows how to test if a range does not contain a specific value and return a specified value if the formula tests true or false, by using an Excel formula and VBA.
This tutorial provides one Excel method that can be applied to test if a range does not contain a specific value and return a specified value by using an Excel IF and COUNTIF functions. In this example, if the Excel COUNTIF function returns a value of 0, meaning the range does not have cells with a value of 505, the test is TRUE and the formula will return a «Not in Range» value. Alternatively, if the Excel COUNTIF function returns a value of greater than 0, meaning the range has cells with a value of 505, the test is FALSE and the formula will return a «In Range» value.
This tutorial provides one VBA method that can be applied to test if a range does not contain a specific value and return a specified value and return a specified value.
FORMULA
=IF(COUNTIF(range, value)=0, value_if_true, value_if_false)
ARGUMENTS
range: The range of cells you want to count from.
value: he value that is used to determine which of the cells should be counted, from a specified range, if the cells’ value is equal to this value.
value_if_true: Value to be returned if the range does not contains the specific value.
value_if_false: Value to be returned if the range contains the specific value.
-
#2
This returns 0 if Pears is not in A1:A10:
=SUM(IF(A1:A10=»Pears»,1,0))
EDIT:
This returns TRUE if Pears is in the range, FALSE if not:
=NOT(ISERROR(MATCH(«Pears»,A1:A10,0)))
-
#3
Thank you, thats what I wanted
This returns 0 if Pears is not in A1:A10:
=SUM(IF(A1:A10=»Pears»,1,0))
EDIT:
This returns TRUE if Pears is in the range, FALSE if not:
=NOT(ISERROR(MATCH(«Pears»,A1:A10,0)))
-
#4
Hi, Try:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG06Oct36
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, Dta [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] Found [COLOR="Navy"]As[/COLOR] Boolean
[COLOR="Navy"]On[/COLOR] [COLOR="Navy"]Error[/COLOR] [COLOR="Navy"]Resume[/COLOR] [COLOR="Navy"]Next[/COLOR]
Dta = Application.InputBox(prompt:="Please Enter Value ", Title:="Unique Values", Type:=2)
[COLOR="Navy"]If[/COLOR] Dta = "False" [COLOR="Navy"]Then[/COLOR] [COLOR="Navy"]Exit[/COLOR] [COLOR="Navy"]Sub[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
[COLOR="Navy"]If[/COLOR] Dn = Dta [COLOR="Navy"]Then[/COLOR] MsgBox Dta & " - Exists": [COLOR="Navy"]Exit[/COLOR] [COLOR="Navy"]Sub[/COLOR]
[COLOR="Navy"]Next[/COLOR] Dn
Range("A" & Rng.Count + 1).Value = Dta
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
-
#5
I tried this:
Code:
Dim Rng As Range, Dn As Range, Dta As String, Found As Boolean, wsSheet As Worksheet
On Error Resume Next
Dta = ComboType.Text
If Dta = "" Then Exit Sub
Set wsSheet = Worksheets("Types")
With wsSheet
Set myRange = .Range("A2", .Range("A65536").End(xlUp))
End With
For Each Dn In Rng
If Dn = Dta Then Exit Sub
Next Dn
With Worksheets("Devices")
If .Cells(2, 1).Value = "" Then
lCurrentRow = 2 ' (list is empty - start in row 2)
Else
Dim lLastRow As Long
With Worksheets("Devices")
lLastRow = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
End With
lCurrentRow = lLastRow
End If
End With
Worksheets("Devices").Cells(lCurrentRow, 3).Value = ComboType.Text
It doesn’t seem to work, it exits if it is present but doesn’t add if it is.
Any ideas?
Thanks,
Andrew
P.S it seemed to work before I tried to change it
-
#6
Hi, Try This:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG06Oct35
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, Dta [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] lLastRow [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
Dta = ComboType.Text
[COLOR="Navy"]If[/COLOR] Dta = "" [COLOR="Navy"]Then[/COLOR] [COLOR="Navy"]Exit[/COLOR] [COLOR="Navy"]Sub[/COLOR]
[COLOR="Navy"]With[/COLOR] Worksheets("Types")
[COLOR="Navy"]Set[/COLOR] Rng = .Range("A2", .Range("A65536").End(xlUp))
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
[COLOR="Navy"]If[/COLOR] Dn = Dta [COLOR="Navy"]Then[/COLOR] [COLOR="Navy"]Exit[/COLOR] [COLOR="Navy"]Sub[/COLOR]
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]With[/COLOR] Worksheets("Devices")
lLastRow = IIf(.Cells(2, 1).Value = "", 2, .Cells(.Rows.Count, 1).End(xlUp).row + 1)
.Cells(lLastRow, 3).Value = ComboType.Text
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
-
#7
Thanks, got it working
Andrew
-
#8
If you’re looking for a VBA solution, I recommend using the Find method rather than looping through a range, as it will probably be much more efficient.
-
#9
If you’re looking for a VBA solution, I recommend using the Find method rather than looping through a range, as it will probably be much more efficient.
-
#10
Thanks, is there a simple way to modify this so it deletes a value if it finds it?
Andrew
In Microsoft Excel, we can determine if a Cell is within a Range with IF Function, however, when it comes to identify the same via VBA code then we need to use if statement. Below is the VBA code and process which you need to paste in the code module of your file.
1. Open Excel
2. Press ALT + F11
3. The VBA Editor will open.
4. Click anywhere in the Project Window.
5. Click on Insert
6. Click on Module
7. In the Code Window, Copy and Paste the below mentioned Code
Function InRange(Range1 As Range, Range2 As Range) As Boolean
‘ returns True if Range1 is within Range2
Dim InterSectRange As Range
Set InterSectRange = Application.InterSect(Range1, Range2)
InRange = Not InterSectRange Is Nothing
Set InterSectRange = Nothing
End FunctionSub TestInRange()
If InRange(ActiveCell, Range(«A1:D100»)) Then
‘ code to handle that the active cell is within the right range
MsgBox «Active Cell In Range!»»»
Else
‘ code to handle that the active cell is not within the right range
MsgBox «Active Cell NOT In Range!»»»
End If
End Sub
8. Once this is pasted, go to the Excel file
9. Select cell A1
10. Click on the VIEW Tab on the ribbon
11. Click on Macros
12. Click on View Macros
13. Shortcut Key to View Macros is ALT + F8
14. A Window will popup
15. Select the Macro
16. Here the Macro is named as “TestInRange”
17. Select the Macro “TestInRange”
18. Click on Run
19. As this cell is in Range you will get a Popup which says “Active Cell In Range!”
20. Click OK to close the Box
Now we will select Cell G9 which is not in Range
1. Select cell G9
2. Click on the VIEW Tab on the ribbon
3. Click on Macros
4. Click on View Macros
5. Shortcut Key to View Macros is ALT + F8
6. A Window will popup
7. Select the Macro
8. Here the Macro is named as “TestInRange”
9. Select Macro “TestInRange”
10. Click on Run
11. As this cell is not in Range you will get a Popup which says “Active Cell NOT In Range!”
12. Click OK to close the Box
This is how we can determine whether a Cell is within a Range or not using VBA.
VBA that checks if a cell is in a range, named range, or any kind of range, in Excel.
Sections:
Check if Cell is in a Range Macro
Check if Cell is in a Named Range
Notes
Check if Cell is in a Range Macro
Sub CellinRange()
'######################################'
'######### TeachExcel.com #########'
'######################################'
'Tests if a Cell is within a specific range.
Dim testRange As Range
Dim myRange As Range
'Set the range
Set testRange = Range("B3:D6")
'Get the cell or range that the user selected
Set myRange = Selection
'Check if the selection is inside the range.
If Intersect(testRange, myRange) Is Nothing Then
'Selection is NOT inside the range.
MsgBox "Selection is outside the test range."
Else
'Selection IS inside the range.
MsgBox "Selection is inside the test range."
End If
End Sub
How to Use the Macro
B3:D6 change this to the range that you want to check to see if the cell is within it.
Selection keep this the same to use the currently selected cell or range from the user or change it to a regular hard-coded range reference like Range(«A1») or whatever range you want.
‘Selection is NOT inside the range. under this line, put the code that you want to run when the cell is not within the range.
‘Selection IS inside the range. under this line, put the code that you want to run when the cell is within the range.
How Does the Macro Know if the Cell is in the Range?
Intersect(testRange, myRange) this checks if the range within the myRange variable overlaps or intersects with the range in the testRange variable. This function, the Intersect function, will return a range object where the two ranges overlap, or, if they don’t overlap, it will return nothing.
As such, we can use the IF statement to check if nothing was returned or not and we do that using Is Nothing after the Intersect function. The Is Nothing is what checks if the Intersect function returned nothing or not, which evaluates to TRUE or FALSE, which is then used by the IF statement to complete its logic and run.
This is how we get the full first line of the IF statement, and this is where all the magic happens:
If Intersect(testRange, myRange) Is Nothing Then
The rest of the IF statement is just a regular IF statement in VBA, with a section of code that runs if the cell is within the range and one that runs when the cell is outside of the range.
Check if Cell is in a Named Range
This is almost exactly the same as the previous macro except that we need to change the range reference from B3:D6 to the name of the named range; that’s it.
It could look like this:
Sub CellinRange()
'######################################'
'######### TeachExcel.com #########'
'######################################'
'Tests if a Cell is within a specific range.
Dim testRange As Range
Dim myRange As Range
'Set the range
Set testRange = Range("MyNamedRange")
'Get the cell or range that the user selected
Set myRange = Selection
'Check if the selection is inside the range.
If Intersect(testRange, myRange) Is Nothing Then
'Selection is NOT inside the range.
MsgBox "Selection is outside the test range."
Else
'Selection IS inside the range.
MsgBox "Selection is inside the test range."
End If
End Sub
For a more detailed expanation of how this macro works, look to the section above.
Notes
If the user makes a selection of multiple cells or a range, and any of those cells are within the test range, this macro will run the code for when the selection is inside the range.
Testing if a range is within a range simply uses the Intersect function in Excel VBA; however, due to how it works, it can seem confusing since we have to check if it Is Nothing or not. But, once you get used to writing this and seeing this in IF statements, it’s really not that difficult, just remember that, in this instance, you will want to pair the use of Intersect with Is Nothing and everything else should follow quite easily.
Download the sample file to get the macro in Excel.
Similar Content on TeachExcel
Excel Data Validation — Limit What a User Can Enter into a Cell
Tutorial:
Data Validation is a tool in Excel that you can use to limit what a user can enter into a…
Logical Operators in Excel VBA Macros
Tutorial: Logical operators in VBA allow you to make decisions when certain conditions are met.
They…
Make a UserForm in Excel
Tutorial: Let’s create a working UserForm in Excel.
This is a step-by-step tutorial that shows you e…
Sum Values that Equal 1 of Many Conditions across Multiple Columns in Excel
Tutorial:
How to Sum values using an OR condition across multiple columns, including using OR with …
VBA Comparison Operators
Tutorial: VBA comparison operators are used to compare values in VBA and Macros for Excel.
List of V…