Comparing cells within a column

hightide
05-04-2010, 02:00 AM
Hi, im a relative noob to VBA. I am trying to compare two or more cells within a column. For example is any cell in Column A < 7 and is any of the other cells in Column A > 10.
If so I'd like to print a message. The thing is I have a code to do this but it is very long:


e.g

If Range("H5").Value < 10.5 And Range("H5").Value > 6 And Range("H6").Value > 15 And Range("H6").Value < 20 Then
Sheet1.Cells(5, 9) = "matching"
ElseIf Range("H5").Value < 10.5 And Range("H5").Value > 6 And Range("H7").Value > 15 And Range("H7").Value < 20 Then
Sheet1.Cells(5, 9) = "matching"
ElseIf Range("H5").Value < 10.5 And Range("H5").Value > 6 And Range("H8").Value > 15 And Range("H8").Value < 20 Then
Sheet1.Cells(5, 9) = "matching"
ElseIf Range("H5").Value < 10.5 And Range("H5").Value > 6 And Range("H9").Value > 15 And Range("H9").Value < 20 Then
Sheet1.Cells(5, 9) = "matching"
ElseIf Range("H5").Value < 10.5 And Range("H5").Value > 6 And Range("H10").Value > 15 And Range("H10").Value < 20 Then
Sheet1.Cells(5, 9) = "matching"
Else
Sheet1.Cells(5, 9) = ""
End If

If Range("H6").Value < 10.5 And Range("H6").Value > 6 And Range("H5").Value > 15 And Range("H5").Value < 20 Then
Sheet1.Cells(6, 9) = "matching"
ElseIf Range("H6").Value < 10.5 And Range("H6").Value > 6 And Range("H7").Value >15 And Range("H7").Value < 20 Then
Sheet1.Cells(6, 9) = "matching"
ElseIf Range("H6").Value < 10.5 And Range("H6").Value > 6 And Range("H8").Value >15 And Range("H8").Value < 20 Then
Sheet1.Cells(6, 9) = "matching"
ElseIf Range("H6").Value < 10.5 And Range("H6").Value > 6 And Range("H9").Value > 15 And Range("H9").Value < 20 Then
Sheet1.Cells(6, 9) = "matching"
ElseIf Range("H6").Value < 10.5 And Range("H6").Value > 6 And Range("H10").Value > 15 And Range("H10").Value < 20 Then
Sheet1.Cells(6, 9) = "matching"
Else
Sheet1.Cells(6, 9) = ""
End If

If Range("H7").Value < 10.5 And Range("H7").Value > 6 And Range("H5").Value > 15 And Range("H5").Value < 20 Then
Sheet1.Cells(7, 9) = "matching"
ElseIf Range("H7").Value < 10.5 And Range("H7").Value > 6 And Range("H6").Value > 15 And Range("H6").Value < 20 Then
Sheet1.Cells(7, 9) = "matching"
ElseIf Range("H7").Value < 10.5 And Range("H7").Value > 6 And Range("H8").Value > 15 And Range("H8").Value < 20 Then
Sheet1.Cells(7, 9) = "matching"
ElseIf Range("H7").Value < 10.5 And Range("H7").Value > 6 And Range("H9").Value > 15 And Range("H9").Value < 20 Then
Sheet1.Cells(7, 9) = "matching"
ElseIf Range("H7").Value < 10.5 And Range("H7").Value > 6 And Range("H10").Value > 15 And Range("H10").Value < 20 Then
Sheet1.Cells(7, 9) = "matching"
Else
Sheet1.Cells(7, 9) = ""
End If

If Range("H8").Value < 10.5 And Range("H8").Value > 6 And Range("H5").Value > 15 And Range("H5").Value < 20 Then
Sheet1.Cells(8, 9) = "matching"
ElseIf Range("H8").Value < 10.5 And Range("H8").Value > 6 And Range("H6").Value > 15 And Range("H6").Value < 20 Then
Sheet1.Cells(8, 9) = "matching"
ElseIf Range("H8").Value < 10.5 And Range("H8").Value > 6 And Range("H7").Value >15 And Range("H7").Value < 20 Then
Sheet1.Cells(8, 9) = "matching"
ElseIf Range("H8").Value < 10.5 And Range("H8").Value > 6 And Range("H9").Value > 15 And Range("H9").Value < 20 Then
Sheet1.Cells(8, 9) = "matching"
ElseIf Range("H8").Value < 10.5 And Range("H8").Value > 6 And Range("H10").Value > 15 And Range("H10").Value < 20 Then
Sheet1.Cells(8, 9) = "matching"
Else
Sheet1.Cells(8, 9) = ""
End If





but as you can see it can be very long when there are lots of cells in the column to compare. What I would like is a way of doing the same calculations but with a much shorter code.
Any help would be much appreciated.

zebulon72
05-05-2010, 01:36 AM
there is many different ways to do that. E.g there are plenty of threads describing the "FIND"-method in the forum.
Here is an example using a loop (or two)
I'm guessing your column looks something like this?
1 a
2 s
3 d
4 f
5 q
6 w
7 e
8
9
10 r
11 t
12 y
13 a
14 s
15 d
16 f
17 d
18 g
19 v
20 v
21 h
22 h

if so we could use something like this. a for-loop would also do the trick.
myvar equals you "matching" i think?


Dim myvar, lookvar As String
Dim i, j As Integer
i = 1
j = 10

Do Until Cells(i, 8) = ""

myvar = Cells(i, 8)
lookvar = Cells(j, 8)

Do Until Cells(j, 8) = ""

If myvar = lookvar Then
MsgBox "Double found on row " & j
End If
j = j + 1
lookvar = Cells(j, 8)

Loop
i = i + 1
j = 10
Loop


Sure you only want a message on you findings?

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum