Results 1 to 5 of 5

Thread: Code to search through a range?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    69

    Code to search through a range?

    Hi,

    I would like to know the code to search through a range. If a single cell’s content in the range is the same as a cell within a range on another workbook, highlight the cell in green, and add one to an integer and display the total when the loop has finished.

    Can anyone help with that?

    Thanks
    Im here until I get good at this,.....the more help I get, the less silly questions you'll get! Thanks

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Code to search through a range?

    You'd need to pass it the two ranges (assuming it is only two ranges).
    then loop through the cells grabbing the one value and then using
    1) a range then .Find on that range object
    2) a range specified and a manual loop through columns and rows of cells comparing each

    Have you considered using a pivot table on the secondary list to provide a column and a count of occurances, then a vlookup or code to match and return this?

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    69

    Re: Code to search through a range?

    How would I do all of the above. I am a novice
    Im here until I get good at this,.....the more help I get, the less silly questions you'll get! Thanks

  4. #4
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Code to search through a range?

    First lets take a normal range.

    Highlight a range on a new workbook.
    Tools>Macro>VB Editor
    Press Ctrl+G
    Type the following (first line then the second):
    Code:
    ?selection.address
    
    ?selection.address(true,true)
    Any information wanted on the selection object or the address method (?) put the editing cursor in them and press f1.


    Say you wanted to loop through the range (just a simple range for now).
    It would probably be best to get an object of the range and hold a current row/col pointers in variables.
    Code:
    public sub testme()
        Dim rng as Range
        dim lngRow as long, lngCol as long
    
    '    On Error Resume Next 'Read up on error handling
    
        set rng = selection.range
    
    '---- Clean up (these are remarks to help you understand how you do things
    '----   They are useful so write them out!!)
        set rng = nothing
    end sub
    That is the start. You then need to look at how to get the start and end columns and rows so you know how much to loop. There is a function to change the address to R<num>C<num>:R<num>C<num> then you can use text manipulation to get the start and end cols and rows.

    Have an experiment first and use the help files. Also, when you have objects defined properly, the intellisense can be useful.

    Alternatively, use Tools>Macros>Record Macro, and see what it outputs as you go around the sheets editting.

    Post up once you've tried something.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    69

    Re: Code to search through a range?

    Sorry, I should have put beginner!

    This is what I was thinking. Do I have the right concept?

    'For Each luCell in sheet1
    'search through a range.


    'If a single cell’s content in sheet1 (Overall sheet) range

    'is the same as a cell within a range on another sheet (other sheet),

    ' THEN highlight the cell in green (on the Overall sheet),
    'ActiveCell.Interior.Color = RGB(?,?,?)


    'Next luCell
    Im here until I get good at this,.....the more help I get, the less silly questions you'll get! Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width