VBA find non-matching value in a column

Ron903

New Member
Joined
Nov 28, 2017
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hello, looking for some assistance with a macro. I am trying to figure out how to search a column to locate the 1st cell that does NOT match a specific value. I would like to avoid looping thru rows as the spreadsheet can be rather large (100's of columns x 1000's of rows).

For example:
Column A contains only 2 possible values: "P" and something else, say "String A" but this will vary. So need to search/find <> "P" to extract "String A" for copying to another sheet.
Column B contains only 2 possible values: "P" and something else, say "String B" but this will vary. So need to search/find <> "P" to extract "String B" for copying to another sheet.
...and so on.

Thanks for any help and suggestions.
Ron
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’).

For example, if you are using Excel 365, if I understand you correctly, I think you can use the new FILTER function to do this quite easily.
See: FILTER function - Microsoft Support
 
Upvote 0
Thanks Joe for the tip on the Account Details. :)

...and on FILTER. I see how for example =FILTER(GS2:GT11,GS2:GS11<>"P") works in my spreadsheet. Just not sure how to incorporate this function into VBA to extract the 1st occurrence of a non P value, when the columns that I am needing to search aren't in the exact same position for every project. They are identified by column headings/numbers derived from a loop.

For additional clarification:
COL_ACOL_BCOL_C
PNEED THIS VALUEP
PPP
NEED THIS VALUEPP
PPNEED THIS VALUE
PPP
 
Upvote 0
So, how do you know what columns to search? What is the logic?

I am having a hard-time visualizing everything from your explanations.
Can you show us some real examples of what your data looks like AND your expected results?

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in. One of the nice things about this tool is that it will show us the row and column headers so that we can see where everything resides.

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
I would like to avoid looping thru rows as the spreadsheet can be rather large (100's of columns x 1000's of rows).
Actually it is significantly faster if you do it in memory (via an array).
Here's an example to search in col A:
VBA Code:
Sub Ron903()
Dim i As Long
Dim va

va = Range("A1", Cells(Rows.Count, "A").End(xlUp))

For i = 2 To UBound(va, 1)  'i = 2 if data start at row 2

    If va(i, 1) <> "P" Then
        Debug.Print "A" & i  'the first cell that meets the criteria
        Exit For
    End If
  
Next

End Sub
 
Upvote 0
Actually it is significantly faster if you do it in memory (via an array).
Here's an example to search in col A:
VBA Code:
Sub Ron903()
Dim i As Long
Dim va

va = Range("A1", Cells(Rows.Count, "A").End(xlUp))

For i = 2 To UBound(va, 1)  'i = 2 if data start at row 2

    If va(i, 1) <> "P" Then
        Debug.Print "A" & i  'the first cell that meets the criteria
        Exit For
    End If
 
Next

End Sub

Thank you Akuini, I was able to use a slightly modified version of this example, along with a few tweaks to my original code to minimize the # of columns that need searching, and it ran pretty quick.

And thanks also Joe4 for your suggestions (and patience).

I need to stop over complicating things. Sigh.
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,215,169
Messages
6,123,412
Members
449,098
Latest member
ArturS75

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