vba to check to see if a a cell equals a ranged list

ahrion

New Member
Joined
Nov 30, 2020
Messages
4
Office Version
  1. 365
  2. 2019
Hello, I'm new here but I have used this forum quite a bit.

I currently have module that calls to the below code any time a cell is is changed in a sheet. It takes a little bit of time to execute the code, a fraction of a second maybe, which is too much for what I'm doing. Is there any way to make this happen quicker and more elegantly? I essentially need to check if X cell is equal to Y ranged list, else = ""

VBA Code:
Dim C As Range

    With ActiveWorkbook.Worksheets("Exception")
        For Each C In Range(ActiveWorkbook.Worksheets("Helper").Range("Q4"), ActiveWorkbook.Worksheets("Helper").Range("Q" & Rows.Count).End(xlUp))
            If C.Value <> "" And .Range("E26") = C.Value Then
                Exit For
            Else
                .Range("E36") = ""
            End If
        Next C
    End With
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
I forgot to mention that X cell can = any of Y in the list of ranged cells Q4:Q21, as long as it's not ""
 
Upvote 0
Not sure what you're trying to achieve, but note the below comments in your code:

VBA Code:
Dim C As Range

    With ActiveWorkbook.Worksheets("Exception")
        For Each C In Range(ActiveWorkbook.Worksheets("Helper").Range("Q4"), ActiveWorkbook.Worksheets("Helper").Range("Q" & Rows.Count).End(xlUp))
            If C.Value <> "" And .Range("E26") = C.Value Then
                
                'do nothing and skip any subsequent comparisons
                Exit For
            Else
                ' do this once ...
                .Range("E36") = ""
                
                ' ... more than once is useless, so skip any subsequent comparisons:
                Exit For
            End If
        Next C
    End With
 
Upvote 0
Not sure what you're trying to achieve, but note the below comments in your code:

VBA Code:
Dim C As Range

    With ActiveWorkbook.Worksheets("Exception")
        For Each C In Range(ActiveWorkbook.Worksheets("Helper").Range("Q4"), ActiveWorkbook.Worksheets("Helper").Range("Q" & Rows.Count).End(xlUp))
            If C.Value <> "" And .Range("E26") = C.Value Then
               
                'do nothing and skip any subsequent comparisons
                Exit For
            Else
                ' do this once ...
                .Range("E36") = ""
               
                ' ... more than once is useless, so skip any subsequent comparisons:
                Exit For
            End If
        Next C
    End With
Thank you. I'm simple trying to see if E26 = any of the values in the Q4: range. If it does, do nothing, if it doesn't, then make E26 blank.
 
Upvote 0
How about
VBA Code:
   Dim Ary As Variant, Res As Variant
   With ActiveWorkbook.Worksheets("Helper")
      Ary = .Range("Q4", .Range("Q" & Rows.Count).End(xlUp)).Value
   End With
   With ActiveWorkbook.Worksheets("main")
      Res = Application.Match(.Range("E26").Value, Ary, 0)
      If IsError(Res) Then .Range("E26").Value = ""
   End With
 
Upvote 0
Solution
How about
VBA Code:
   Dim Ary As Variant, Res As Variant
   With ActiveWorkbook.Worksheets("Helper")
      Ary = .Range("Q4", .Range("Q" & Rows.Count).End(xlUp)).Value
   End With
   With ActiveWorkbook.Worksheets("main")
      Res = Application.Match(.Range("E26").Value, Ary, 0)
      If IsError(Res) Then .Range("E26").Value = ""
   End With
This seemed to be the most elegant solution. Worked well for some other stuff too and it make code execution seem quicker. Thank you.

I can do the code below instead, right?
VBA Code:
Dim Ary, Res As Variant
 
Upvote 0
As both variables are variants then yes you can, but if you did
VBA Code:
        Dim Ary, Res As Range
then only Res would be a range
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,849
Members
449,051
Latest member
excelquestion515

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top