+ Reply to Thread
Results 1 to 9 of 9

Need Loop to Search for cell containing two values

  1. #1
    Registered User
    Join Date
    04-25-2015
    Location
    FL
    MS-Off Ver
    2010
    Posts
    6

    Exclamation Need Loop to Search for cell containing two values

    I am using VBA to search an Excel sheet. It will need to loop through column B for a cell containing two values. I have tried a Do Until and If statements, but it will only find the first value and return that cell. This is an issue because there may be multiple cells with the first value but only one cell with the second value. Is there a way to search for the two values at the same time? or Loop through each cell until it finds both values?

    I have tried multiple different codings and ways, I have yet it to find the correct cell for both values. Like I said, it'll find the first cell it comes to containing the first value but cannot get it to loop to the first cell containing both values?

    Please Login or Register  to view this content.
    This is an example of what I was trying to do. It would find a cell containing first value but cannot get it to only find the cell containing both. Thanks for any help in advance.

  2. #2
    Forum Contributor HerryMarkowitz's Avatar
    Join Date
    09-10-2012
    Location
    Istanbul / Turkey
    MS-Off Ver
    2013 - Win10 - 64bit
    Posts
    1,007

    Re: Need Loop to Search for cell containing two values

    Maybe...
    Please Login or Register  to view this content.
    Last edited by HerryMarkowitz; 04-26-2015 at 01:50 PM.
    Sub DontForgetThese()
         If Your thread includes any code Then Please use code tags...
         If Your thread has been solved Then Please mark as solved...
         If Anybody has helped to you Then Please add reputation...
    End Sub

  3. #3
    Registered User
    Join Date
    04-25-2015
    Location
    FL
    MS-Off Ver
    2010
    Posts
    6

    Re: Need Loop to Search for cell containing two values

    I've looked at this post before, and the .find part of it will work to find the first value. I can also find the second value with one search. The problem is, it's not finding the one cell that contains both. In column b I will have thing as followed:

    TTT syscall blah RO1
    TTT syscall blah RO2
    TTT syscall blah BO1

    I need to search for, lets say, TTT and R02, then return which cell it found both values in. So in this case, it would be "B2" where those values were found. I am not certain that I can even do it but for the script to work, I need it to find the cell containing both.

    Also, thanks so much for contributing. Very much appreciated!

  4. #4
    Forum Contributor HerryMarkowitz's Avatar
    Join Date
    09-10-2012
    Location
    Istanbul / Turkey
    MS-Off Ver
    2013 - Win10 - 64bit
    Posts
    1,007

    Re: Need Loop to Search for cell containing two values

    Maybe...
    Please Login or Register  to view this content.

  5. #5
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Need Loop to Search for cell containing two values

    Hi theOctonaut and welcome to ExcelForum,

    You probably only need one find command to solve your problem.

    There are several techniques that can be used to do what you want. They include:
    a. Using .find with a wildcard one time only search WHOLE cell (included below).
    b. Using .find with a wildcard one time only search PART of cell (included below).
    c. Using .find with a wildcard and find all matches search WHOLE cell (included below).
    d. Search for one item only e.g. TTT (part of cell) using .find command and then use:
    (1) Like command to find additional matches in the cell (not included).
    (2) Regular Expressions command to find additional matches in the cell (not included).

    See the attached file which includes a., b. and c. above.

    Please Login or Register  to view this content.
    Lewis
    Last edited by LJMetzger; 04-26-2015 at 03:36 PM.

  6. #6
    Forum Contributor HerryMarkowitz's Avatar
    Join Date
    09-10-2012
    Location
    Istanbul / Turkey
    MS-Off Ver
    2013 - Win10 - 64bit
    Posts
    1,007

    Re: Need Loop to Search for cell containing two values

    Another way...
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    04-25-2015
    Location
    FL
    MS-Off Ver
    2010
    Posts
    6

    Re: Need Loop to Search for cell containing two values

    Thanks so much guys! I got it working with the following code:

    Please Login or Register  to view this content.
    Next I will take the row that is returned and loop through each cell on the row starting at column C and save the value of each cell to a variable array until an empty cell is found. This will give me each cells data in that row for the two search values. I had tried foundSite = sN & " " & dT but didn't know I could use the "*" symbol to do what I needed to do.

    thanks again!

  8. #8
    Registered User
    Join Date
    04-25-2015
    Location
    FL
    MS-Off Ver
    2010
    Posts
    6

    Re: Need Loop to Search for cell containing two values

    One more question:

    If I put a Do Until loop to search through the discovered row, how do I save each cell to an array value? If I don't use an array, I can get it to save the last cells value. Everytime I put it an array, it leaves each value as empty.

    Please Login or Register  to view this content.
    Last edited by theOctonaut; 04-27-2015 at 12:16 AM.

  9. #9
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Need Loop to Search for cell containing two values

    Hi Octo,

    Thank you very much for the rep points.

    I think you are missing the .FindNext call to find further values. See the following example code (included in the atttached file) which may help you. It is combines two solutions in one:
    a. Use of a Dynamic Array
    b. Use of a Dynamic Array of a Structure
    Please Login or Register  to view this content.

    Key Points:
    a. Option Explicit - To prevent typos from ruining days and weeks of work 'Option Explicit' is NEEDED at the top of each code module. This prevents errors caused by missspellings and FORCES every variable to be DECLARED (e.g. Dim i as Integer). http://www.cpearson.com/excel/DeclaringVariables.aspx
    b. Structure Definition - It should go after 'Option Explicit' and before the first routine (Sub or Function).
    Please Login or Register  to view this content.
    c. Creation of Dynamic Array and Dynamic Structure Array - Notice that there are no dimensions inside.
    Please Login or Register  to view this content.
    d. Initialization of the items created in c. above. The method I use is probably not the best way to do things, but it has always worked for me without problems. Redim expands (or contracts) the array size and removes any existing data. Please note that the Redim command can be tricky when using multi-dimensional arrays
    Please Login or Register  to view this content.
    e. Putting data in the arrays. Redim Preserve expands (or contracts) the array size and keeps any existing data.
    f. Offset command. The syntax is Offset(number of rows, number of columns). r.Offset(0,1) means take the address of r and move it 1 column to the right.
    Please Login or Register  to view this content.
    ----------------
    Debugger Secrets:
    a. Press 'F8' to single step (goes into subroutines and functions).
    b. Press SHIFT 'F8' to single step OVER subroutines and functions.
    c. Press CTRL 'F8' to stop at the line where the cursor is.
    d. 'Left Click' the margin to the left of a line to set (or clear) a BREAKPOINT.
    e. Press CTRL 'G' to open the IMMEDIATE WINDOW. 'debug.print' statements send their
    output to the IMMEDIATE WINDOW.
    f. Select View > Locals to see all variables while debugging.
    g. To automatically set a BREAKPOINT at a certain location put in the line:
    'Debug.Assert False'
    h. To conditionally set a BREAKPOINT at a certain location put in lines similar to:
    if i >= 20 and xTV20 > 99.56 then
    Debug.Assert False
    endif
    i. A variable value will be displayed by putting the cursor over the variable name.

    I hope this helps.

    Lewis
    Last edited by LJMetzger; 04-27-2015 at 04:42 PM.

+ 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] Creating a VBA loop to search for values, stop when a cell value is 0
    By jfgay in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-30-2013, 10:30 AM
  2. search string of test in a cell and use loop to define the category
    By megaman7 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-20-2013, 10:01 PM
  3. search cell values based on list of values in other sheet and add color to row
    By darkbraids in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-10-2012, 08:35 AM
  4. [SOLVED] Macro to loop through cell values in a column and format multiple cell values
    By Roop in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-07-2012, 05:39 PM
  5. loop through values in listbox and search for them in excel sheet
    By longhorn23 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-29-2010, 08:32 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