View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.programming
John Keith John Keith is offline
external usenet poster
 
Posts: 172
Default VBA "Find" with multiple criterion

On Mon, 13 Aug 2012 15:48:25 +0000, KeriM
wrote:


I'm trying to do a somewhat complicated find in VBA.

I have a worksheet with column B having only two different values (eg. a
and b). Column C has values that repeat for each value for column B.
(eg. 1 and 2). The values that I want to return are in columns E and F.


So what I'm trying to do is this:

If columns B and C equal "a" and "1" (respectively), then subtract the
values in E and F (on that row where the columns equal) and return that
result on another worksheet.

Repeat for all the different combinations...a1, a2, b1, b2.

To illustrate:


Code:
--------------------


Column B | Column C Column E | Column F

a 1 70 50
a 2 80 30
b 1 60 20
b 2 40 20


--------------------


I want to find a1 and write "20" in another worksheet.

Sorry if this is an overly long explanation, I feel like it's confusing
to explain. Let me know if anything needs to be clarified. As always,
any help is appreciated!


How long is your list? Is the data contiguous? Do you want to store
the result in the same row on the other worksheet?

Setting those questions aside I'd suggest either a FOR/WHILE loop. I
suppose there are more elegant methods and most experts try to avoid
loops but they can be quite fast for a simple operation like this and
the code is easy to understand.

For i = first_row to last_row
if cells(i, "B") = "a" and cells(i, "C") = 1 then _
worksheets(othersheet).cells(i, "A")= cells(i, "E") - cells(i, "F")
Next i

This all assumes I understood your question and there are several
details to check to meet what you really have to work with.

John Keith