I use the below VBA to select the first visible cell in a filtered range:
Sub Postioning_Option_01()
Sheet1.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, 2).Select
End Sub
Now, instead of only applying this VBA
to Sheet1
I want to loop through all sheets.
Therefore, I tried to go with this VBA
:
Sub Postioning_Option_02()
Dim b As Worksheet
For Each b In Worksheets
b.Select
b.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, 2).Select
Next b
End Sub
When I run this VBA
I get error Object variable or With block variable not set
.
What do I need to change in the VBA
to make it work?
This will skip sheets with no autofilter:
Sub Postioning_Option_02()
Dim b As Worksheet
For Each b In Worksheets
If b.AutoFilterMode Then
b.Select
b.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, 2).Select
End If
Next b
End Sub
Nothing special about the first 4 subs, but the last 2 look a little bit deeper, but might be out of context.
Option Explicit
Sub Positioning_Option_01()
If Sheet1.AutoFilterMode Then
Sheet1.AutoFilter.Range.Offset(1) _
.SpecialCells(xlCellTypeVisible).Cells(1, 2).Select
Else
End If
End Sub
Sub Postioning_Option_02()
Dim b As Worksheet
For Each b In ThisWorkbook.Worksheets
b.Activate
If b.AutoFilterMode Then
b.AutoFilter.Range.Offset(1) _
.SpecialCells(xlCellTypeVisible).Cells(1, 2).Select
Else
End If
Next
End Sub
Sub Positioning_Option_01_With()
With Sheet1
If .AutoFilterMode Then
With .AutoFilter.Range.Offset(1) _
.SpecialCells(xlCellTypeVisible).Cells(1, 2)
.Select
End With
Else
End If
End With
End Sub
Sub Postioning_Option_02_With()
Dim b As Worksheet
For Each b In ThisWorkbook.Worksheets
b.Activate
With b
If .AutoFilterMode Then
With .AutoFilter.Range.Offset(1) _
.SpecialCells(xlCellTypeVisible).Cells(1, 2)
.Select
End With
Else
End If
End With
Next
End Sub
Sub Pos_Offset()
With Sheet1
If .AutoFilterMode Then
' The following range refers to the range below headers containing
' data and one empty row range below data.
With .AutoFilter.Range.Offset(1)
' If no match in AutoFilter, after applying "SpecialCells"
' the cell in the row below data will be selected. You can't
' tell if it is contained in "AutoFilter.Range".
With .SpecialCells(xlCellTypeVisible).Cells(1, 2)
.Select
Debug.Print .Address, .Value
End With
End With
Else
' AutoFilterMode is False
MsgBox "AutoFilterMode is set to 'False'."
End If
End With
End Sub
Sub Pos_Actual()
With Sheet1
If .AutoFilterMode Then
' The range below headers containing data.
With .AutoFilter.Range
With .Rows(2).Resize(.Rows.Count - 1)
' If no match in AutoFilter, after applying SpecialCells
' in the following line, the following error occurs:
' "Run-time error '1004': No cells were found."
' Therefore:
On Error Resume Next
With .SpecialCells(xlCellTypeVisible).Cells(1, 2)
If Err.Number <> 0 Then
' The are no visible cells.
MsgBox "No visible cells."
Else
.Select
Debug.Print .Address, .Value
End If
On Error GoTo 0
End With
End With
End With
Else
' AutoFilterMode is False
MsgBox "AutoFilterMode is set to 'False'."
End If
End With
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.