View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
OssieMac OssieMac is offline
external usenet poster
 
Posts: 2,510
Default Case Statement with multiple checks

Hi Newman,

I am not really sure what you are trying to do but see if the following
example will help you to work out what you want.

You need to realise that the case only performs the first true evaluation
even if other true evaluations follow. If none are true then it goes to Else
Case but it is not necessary to include Else Case unless you need it.

Note the method I have used to evaluate an empty cell. I find it the most
reliable because Null and Empty and also a cell with "" are all different and
can be a pain deciding which one to use. However, the one I use sees a zero
and will not evaluate that to empty.

If my example does not help then feel free to get back to me and maybe you
can include a small example of what you require.

Sub Test_Case()
Dim rng1 As Range
Dim c As Range
Dim colBvalue As Variant

'Edit following range to suit the your range
Set rng1 = Sheets("Sheet1").Range("A1:A20")

For Each c In rng1

'Test for no data in cells in column A first
If Len(Trim(c)) = 0 Then
MsgBox "Empty cell address is " & _
c.Address & Chr(13) & "Processing terminated"
End 'This terminates the procedure
End If

'Using case to test values in column B
'Set a variable to = the value in column B
colBvalue = Cells(c.Row, c.Column).Offset(0, 1).Value
Select Case colBvalue
Case 10 'Value in col B is 10
MsgBox "Found value " & 10 & _
" Opposite cell " & c.Address
Case 5 'Value in col B is 5
MsgBox "Found value " & 5 & _
" Opposite cell " & c.Address
Case Else 'Value in column B is none of the above
MsgBox "Opposite cell " & c.Address & _
" Did not meet any case condition"
End Select
Next c

End Sub

Regards,

OssieMac

"Newman Emanouel" wrote:

Dear All

I am in need of your expert help

I am trying to write a macro that checks two columns in a case statement.
What I want it to do is check that column "A" in not null and check that
column "B" has a particular value.

If Column a is null then exit the macro if not continue with the checking. I
just cannot figure it out and need help

Thanks

Regards

Newman