VBA Select Case Statement

jtbrolly

New Member
Joined
Aug 22, 2011
Messages
42
Trying to make a macro similar to the one you guys helped me with the other day, but I'm getting a Compile error: Statments and labels invalid between Select Case and first Case at the first match.Offset. Can anybody help me out?

Code:
Sub Trues()
 
    Dim match
    match = "TRUE"
For Each match In Range("l1:l")
Select Case match
match.Offset(0, -11) = "Matches - Good"
match.Offset(0, -11).Interior.ColorIndex = 4
Case Else
End Select
Next match
End Sub
 
Last edited:

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
If you want to loop though Range("A1:A11") looking from all the cells that contain true, the syntax would be

Code:
Dim oneCell As Range

For Each oneCell in Range("A1:A11")
    Select Case oneCell.Value
        Case "True"
            oneCell.Offset(0, 1) = "is good"
        Case Else
    End Selct
Next oneCell

What cell do you want green with "Matches - Good"?
Range("1:11").Offset(0, -11) is not clear, and might be to the left of column A.
 
Upvote 0
I'm looking for the Trues in Column L, hence the -11. I want the Matches - Good being written in Column A.
 
Upvote 0
Also, not sure if this affects it but the TRUE is a formula result.

I'm looking at the formula results in column L and if the result is a TRUE, I want to write Matches - Good in green in column A. And if it's not TRUE, I don't want it to write anything.
 
Upvote 0
This should do that.
Code:
Dim oneCell As Range

For Each oneCell in Range("L1:L11")
    Select Case oneCell.Value
        Case "True"
            oneCell.EntireRow.Range("A1") = "Match - good"
            oneCell.EntireRow.Range("A1").Interior.ColorIndex = 4
        Case Else
    End Select
Next oneCell
Note that the .Range is relative to oneCell.EntireRow, hense the .Range("A1")

SelectCase is slower than If..ElseIf..End If. And adding a With...End With gives this code:
Code:
Dim oneCell As Range

For Each oneCell in Range("L1:L11")
    With oneCell
        If CStr(.Value) = "True"
            With.EntireRow.Range("A1")
                .Value = "Match - good"
                .Interior.ColorIndex = 4
            End With
        End If
    End With
Next oneCell
The CStr is there to protect against there being an error value (e.g. #DIV/0) in the cell. (Never trust what the user might put in a cell.)
 
Upvote 0
The If-Then you provided didn't write anything. The Select Case is close:

Code:
Dim oneCell As Range
For Each oneCell In Range("L:L")
    Select Case oneCell.Value
        Case "True"
        On Error Resume Next
            oneCell.EntireRow.Range("A1") = "Matches - Good"
            oneCell.EntireRow.Range("A1").Interior.ColorIndex = 4
        Case Else
    End Select
Next oneCell
End Sub

The only problem I ran into is I'm trying to skip over the N/A errors. It skips the FALSE cells, but it's still writing Matches - Good for N/A, which I don't want it to do. I tried putting On Error Resume Next which let it ran without stopping on the N/As, but now it applies it for them.
 
Upvote 0
Do you know why the If Then isn't working? It's running without any errors, but nothing happens.

The SelectCase runs but when I tried On Error Resume Next it skipped the N/As but then wrote the Match - Good in the corresponding A cells as well.

Is it possible to skip the N/As without doing anything? I know this sounds bad, but we have recurring N/A errors which we look at after the TRUEs.
 
Upvote 0
Try changing this
Code:
If LCase(CStr(.Value)) = "true"

If you still want the Select Case, try
Code:
Select Case CStr(oneCell.Value)
 
Upvote 0

Forum statistics

Threads
1,224,568
Messages
6,179,595
Members
452,927
Latest member
whitfieldcraig

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