Select Range by Condition in Cell

mucah!t

Well-known Member
Joined
Jun 27, 2009
Messages
593
Hello all,

I need to select all ranges A:C where there is a "TRUE" mentioned in A
The following code only selects the first occurance [from the bottom]
Any ideas?

Code:
Sub test()
   Dim lastrow As Long, icell As Long
lastrow = Range("A" & Rows.Count).End(xlUp).Row
For icell = 1 To lastrow
    If Range("A" & icell) = True Then
        
        Range("A" & icell, "C" & icell).Select
    
    Else
    End If
Next icell
End Sub
 
Hi,
Please try the below code and revert.

Code:
    rowend = Range("A" & Rows.Count).End(xlUp).Row
    Range("A5:C" & rowend).Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$5:$C$" & rowend).AutoFilter Field:=1, Criteria1:="TRUE"
    Selection.SpecialCells(xlCellTypeVisible).Select
    Selection.AutoFilter

Hope this satisfies your requirement.
 
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
hello balajayan,

This one works almost.
the only problem with it is, that it also selects A5 [the first row] while it is "false" iso "true"
 
Upvote 0
I'd like cells A:C to be selected where the cell in A has a "TRUE"


So like this then

Code:
Sub test()
Dim lastrow As Long, icell As Long
Dim sRanges As String
lastrow = Range("A" & Rows.Count).End(xlUp).Row
For icell = 1 To lastrow
    If Range("A" & icell) = True Then
        If sRanges = "" Then
        sRanges = "A" & Trim(Str(icell)) & ":C" & Trim(Str(icell))
        Else
        sRanges = sRanges & ",A" & Trim(Str(icell)) & ":C" & Trim(Str(icell))
        End If
 
      '  Range("A" & icell, "C" & icell).Select
 
    End If
Next icell
If sranges <> "" Then
    Range(sRanges).Select
End If
 
End Sub
 
Last edited:
Upvote 0
maybe you need to state the activesheet.range(....

To test .."test" i use developer tab/ macros/run macro
works everytime.

So if this function is being called by other functions you will need to take account of the activesheet that it will act upon
 
Last edited:
Upvote 0
Hi,
Slightly modified, please try the below code:

Code:
    rowend = Range("A" & Rows.Count).End(xlUp).Row
    Range("A5:C" & rowend).Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$5:$C$" & rowend).AutoFilter Field:=1, Criteria1:="TRUE"
    If Range("A5").Value = False Then
        Range("A6:C" & rowend).Select
    End If
    Selection.SpecialCells(xlCellTypeVisible).Select
    Selection.AutoFilter
 
Upvote 0

Forum statistics

Threads
1,225,149
Messages
6,183,184
Members
453,151
Latest member
Lizamaison

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