How to get VBA to return a count of selected rows in a filtered range for processing.

Sej

New Member
Joined
Oct 17, 2020
Messages
17
Platform
  1. Windows
Hi,

Forgive me if I use non-programming terms - I am a self taught coder at a very basic level. And I just learn from forums such as this. But this has me stumped.

I have a code where depending on the number of rows selected (in a filtered non-contiguous rows ) - the macro should loop and run that many times.

Here is an extract of the macro:

answer = Selection.Columns(1).SpecialCells(xlCellTypeVisible).Count
For i = 1 To answer
Call FolderName
MsgBox "Should I continue?"
ActiveCell.Offset(1, 0).Activate
Do While ActiveCell.EntireRow.Hidden = True
ActiveCell.Offset(1, 0).Activate
Loop
Next i
End If


This code works perfectly - but only if multiple rows are selected - if only one row is selected - it causes an overflow error.

To bypass that, I have rewritten this to:

answer = MsgBox("Select 'Yes' for 1 and 'No' for more than 1", vbYesNoCancel)
If answer = vbYes Then
Call FolderName
MsgBox "Should I continue?"
End If

If answer = vbNo Then
counter = Selection.Columns(1).SpecialCells(xlCellTypeVisible).Count

For i = 1 To counter
Call FolderName
MsgBox "Should I continue?"
ActiveCell.Offset(1, 0).Activate
Do While ActiveCell.EntireRow.Hidden = True
ActiveCell.Offset(1, 0).Activate
Loop
Next i
End If
If answer = vbCancel Then
Exit Sub
End If


I cannot imagine, I need to jump through such hoops myself to try to make a simple count based loop for filtered non-contiguous rows.

What am I doing wrong? Can this be done better?
 
The header is actually in A4 - but will this code work with a filter on?

The code will work with filter on. Use this instead
VBA Code:
ActiveSheet.AutoFilter.Range.Rows.Count - 1
 
Upvote 0

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Sorry - forget that last one
I forgot that you wanted visible rows only
 
Upvote 0
If row 3 is empty
Rich (BB code):
answer = ActiveSheet.Range("A4").CurrentRegion.Resize(, 1).SpecialCells(xlCellTypeVisible).Count - 1

If rows 1 , 2 & 3 contain data adjacent to the table they are included in CurrentRegion
Rich (BB code):
answer = ActiveSheet.Range("A4").CurrentRegion.Resize(, 1).SpecialCells(xlCellTypeVisible).Count - 4
 
Upvote 0
Yongle, there may be unselected rows in the filtered rows.....

eg: If the filter has 35 rows, my selected rows may just be row number 4 and row number 6.

Wont your code take ALL the 35 rows visible?
 
Upvote 0
That was why I asked this questionin post#18 ... do you want to count every visible row except the header ?

Are you selecting manually?
- perhaps VBA could be used to filter the data to match your manual selection
- how are you choosing the rows that you are selecting?

OR re you selecting via VBA?
- please post the code
 
Upvote 0
Apologies, I missed that - no it is never ALL visible rows.

- I am selecting manually, but the business workflow is so complicated that VBA cannot match the selection.
- I am selecting filtered non-contiguous rows.

The macro HAS to run on selected rows.
 
Upvote 0
Thanks
I'm busy until tomorrow morning. I will update the thread then.

but the business workflow is so complicated that VBA cannot match the selection.
- wait and see ;)
 
Upvote 0
Explanation of the approach
- use VBA to colourfill cell in designated column in each selected row
- use that column as an additional filter

Simple Example

1
. Copy the data below into Sheet1 in a new workbook
Book1
ABCD
1
2
3
4NameTagRefDate
5Name01AR005007/10/2020
6Name02BR006401/10/2020
7Name03AR007701/10/2020
8Name04AR008608/10/2020
9Name05AR008802/10/2020
10Name06BR002705/10/2020
11Name07BR008829/09/2020
12Name08AR009605/10/2020
13Name09AR006028/09/2020
14Name10BR008507/10/2020
Raw Data


2. Select cell A4 and click on Data \ Filter

3
. Insert the code below in Sheet1 code window (NOT in a module)
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    With Target
        If .Row < 5 Or .Column <> 1 Then Exit Sub
        With .Interior
            If .ColorIndex = 43 Then .ColorIndex = -4142 Else .ColorIndex = 43
        End With
    End With
End Sub

4. In column Tag manually filter for Tag A
Book1
ABCD
1
2
3
4NameTagRefDate
5Name01AR005007/10/2020
7Name03AR007701/10/2020
8Name04AR008608/10/2020
9Name05AR008802/10/2020
12Name08AR009605/10/2020
13Name09AR006028/09/2020
Raw Data


5. Click on a few cells in column A - cells turn green when clicked (cancelled if clicked again)
Book1
ABCD
1
2
3
4NameTagRefDate
5Name01AR005007/10/2020
7Name03AR007701/10/2020
8Name04AR008608/10/2020
9Name05AR008802/10/2020
12Name08AR009605/10/2020
13Name09AR006028/09/2020
Raw Data


6. Click on dropdown in column A and filter by colour
Book1
ABCD
1
2
3
4NameTagRefDate
5Name01AR005007/10/2020
9Name05AR008802/10/2020
12Name08AR009605/10/2020
13Name09AR006028/09/2020
Raw Data



7. Next steps
- think about what we need to do to make this work for you
- which column do you want to click to make the selection?
- should a different column be used to for the colour?
- etc
- I have kept it VERY simple but we can make it more sophisticated to match your requirements
- the important target is that we need the data FILTERED and then VBA is not relying on selected cells which is what is causing your difficulties

Let me know if you require any help to develop it further
 
  • Like
Reactions: Sej
Upvote 0
Solution
Explanation of the approach
- use VBA to colourfill cell in designated column in each selected row
- use that column as an additional filter

Simple Example

1
. Copy the data below into Sheet1 in a new workbook
Book1
ABCD
1
2
3
4NameTagRefDate
5Name01AR005007/10/2020
6Name02BR006401/10/2020
7Name03AR007701/10/2020
8Name04AR008608/10/2020
9Name05AR008802/10/2020
10Name06BR002705/10/2020
11Name07BR008829/09/2020
12Name08AR009605/10/2020
13Name09AR006028/09/2020
14Name10BR008507/10/2020
Raw Data


2. Select cell A4 and click on Data \ Filter

3
. Insert the code below in Sheet1 code window (NOT in a module)
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    With Target
        If .Row < 5 Or .Column <> 1 Then Exit Sub
        With .Interior
            If .ColorIndex = 43 Then .ColorIndex = -4142 Else .ColorIndex = 43
        End With
    End With
End Sub

4. In column Tag manually filter for Tag A
Book1
ABCD
1
2
3
4NameTagRefDate
5Name01AR005007/10/2020
7Name03AR007701/10/2020
8Name04AR008608/10/2020
9Name05AR008802/10/2020
12Name08AR009605/10/2020
13Name09AR006028/09/2020
Raw Data


5. Click on a few cells in column A - cells turn green when clicked (cancelled if clicked again)
Book1
ABCD
1
2
3
4NameTagRefDate
5Name01AR005007/10/2020
7Name03AR007701/10/2020
8Name04AR008608/10/2020
9Name05AR008802/10/2020
12Name08AR009605/10/2020
13Name09AR006028/09/2020
Raw Data


6. Click on dropdown in column A and filter by colour
Book1
ABCD
1
2
3
4NameTagRefDate
5Name01AR005007/10/2020
9Name05AR008802/10/2020
12Name08AR009605/10/2020
13Name09AR006028/09/2020
Raw Data



7. Next steps
- think about what we need to do to make this work for you
- which column do you want to click to make the selection?
- should a different column be used to for the colour?
- etc
- I have kept it VERY simple but we can make it more sophisticated to match your requirements
- the important target is that we need the data FILTERED and then VBA is not relying on selected cells which is what is causing your difficulties

Let me know if you require any help to develop it further
Wow, ok let me figure this out at work on Monday and will post my findings. Thank you Yongle.
 
Upvote 0

Forum statistics

Threads
1,215,948
Messages
6,127,871
Members
449,410
Latest member
adunn_23

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