selecting cells with same value in a column

haumoana

New Member
Joined
Jul 6, 2007
Messages
11
Hi,

I want to select all cells in column B, where the cell in column B has a specified value and the cell in column C has a second specified value without looping.

I was thinking of using the autoFilter combined with specialCells to select the cells, but it didn't seem to work.

thanks.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
not clear why autofilter does not work. can you post a small extract of your sheet and the specific values.
 
Upvote 0
The Autofilter works fine, but when I come to loop the specialcells has selected everything in the column not the filtered values as I expected it to.

As the stuff I have is commercially sensitive I have made a spreadsheet with dummy datea.

Code:
ID	Day	Weather
1	Thursday	Sunny 
2	Thursday	Raining
3	Thursday	Sunny 
4	Friday	Windy
5	Friday	Raining
6	Thursday	Windy

Code:
    Selection.AutoFilter Field:=2, Criteria1:="Thursday"
    Selection.AutoFilter Field:=3, Criteria1:="Sunny"
    Columns("B:B").SpecialCells(xlCellTypeConstants, 2).Select

    For Each c In Selection
        'do stuff
    Next c
 
Upvote 0
copy the filtered data sufficiently below the original database and then do your looping(If the no. of rows are large then preferably copy in next sheet from A1). and then take the second column and do your looping stuff. I have added c.select in the looping to test the sub.

the revised macro is given which is to be modified to suit you.

Code:
Sub test()
Dim c As Range
Range("a1").Select

Selection.AutoFilter Field:=2, Criteria1:="Thursday"
    Selection.AutoFilter Field:=3, Criteria1:="Sunny"
    'Columns("B:B").SpecialCells(xlCellTypeConstants, 2).Select
Range(Range("a1"), Range("a1").End(xlDown).End(xlToRight)).Copy
Range("a15").PasteSpecial
Range(Range("B16"), Range("B16").End(xlDown)).Select
    For Each c In Selection
    c.Select
        'do stuff
    Next c
Selection.AutoFilter

End Sub
 
Upvote 0
Thank you for your solution. It did not quite suit my purposes but did inspire me to a solution that worked.

Code:
    Selection.AutoFilter Field:=2, Criteria1:="Thursday"
    Selection.AutoFilter Field:=3, Criteria1:="Sunny"
    
    Range(Range("b1"), Range("b1").End(xlDown)).SpecialCells(xlCellTypeVisible).Select
    
    For Each cel In Selection
         ' do stuff
    Next cel

:p
 
Upvote 0

Forum statistics

Threads
1,214,810
Messages
6,121,690
Members
449,048
Latest member
81jamesacct

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