Select Case Statement in For Each Loop

clueless1

New Member
Joined
Apr 16, 2013
Messages
17
Hi there, will do my best to be brief:

I have the code below to loop through cells in column A and then place a result into column B based upon the value in column A. I also want the value of the cell to be put to Upper Case only for the purposes of comparison, i.e. I do not need it changed in the actual data.

Code:
Sub Animals()
Dim rng As Range
Dim cell As Range
Dim result As String
Dim animal As String
Set rng = Range("A2:A30")

    For Each cell In rng
    
        animal = UCase(cell.Value)
    
        Select Case animal
        
        Case animal = "CAT"
        result = "have whiskers"
        
        Case animal = "DOG"
        result = "can catch a ball"
        
        
        Case animal = "APE"
        result = "are a dangerous animal!"
        
        Case Else
        result = "Animal Not Recognised"
        
        End Select
                
        cell.Offset(0, 1).Value = result
    
    Next cell
End Sub

The variable 'animal' does not seem to be getting the value in the cell, as all results are the 'Case Else' statement i.e. "Animal Not Recognised".

Please can anyone advise, must be something minor?!?!
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Use Case "CAT" instead of Case animal = "CAT". The Select Case animal line signifies that you are selecting based on the animal variable.
 
Upvote 0
or you can try below macro
Code:
Sub Animals()
Dim rng As Range
Set rng = Range("A2:A30")
For Each cell In rng
If UCase(cell) = "DOG" Then
cell.Offset(0, 1).Value = "can catch a ball"
ElseIf UCase(cell) = "CAT" Then
cell.Offset(0, 1).Value = "have whiskers"
ElseIf UCase(cell) = "APE" Then
cell.Offset(0, 1).Value = "are a dangerous animal!"
Else
cell.Offset(0, 1).Value = "Animal Not Recognised"
End If
Next
End Sub
 
Upvote 0
ok then try below one
Code:
Sub Test()
For Each cell In Range("A2:A30")
Select Case UCase(cell)
Case "CAT"
cell.Offset(0, 1).Value = "have whiskers"
Case "DOG"
cell.Offset(0, 1).Value = "can catch a ball"
Case "APE"
cell.Offset(0, 1).Value = "are a dangerous animal!"
Case Else
cell.Offset(0, 1).Value = "Animal Not Recognised"
End Select
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,047
Members
448,940
Latest member
mdusw

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top