How to find the last visible column (not hidden) in sheet using vba?
I have used the code below:
Dim LastColumn As Integer
If WorksheetFunction.CountA(Cells) > 0 Then
'Search for any entry, by searching backwards by Columns.
LastColumn = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
MsgBox LastColumn
End If
But it gives me last column number. I want last visible column number. Any help?
Try the below. You can use xlCellTypeVisible to specify hidden vs non hidden cells.
Sub counthidden()
MsgBox ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible).Columns.Count
End Sub
You can use SpecialCells to help:
Dim rng1 As Range
Dim rng2 As Range
On Error Resume Next
Set rng1 = ActiveSheet.UsedRange.SpecialCells(xlVisible)
On Error GoTo 0
If Not rng1 Is Nothing Then
Set rng2 = rng1.Find(What:="*", After:=rng1.Cells(1), SearchOrder:=xlByColumns, SearchDirection:=xlPrevious)
MsgBox rng2.Column
End If
Surprisingly simple as it turns out. I was looking for a similar problem.
Sub lastVisibleColumn()
' This should select the last fully visible column on the active window
' This part selects the current top left visible cell regardless of zoom
ActiveWindow.VisibleRange.Select
ActiveCell.Offset(0, 0).Select
startColumn = ActiveCell.Column
' Large scroll right 1 page
ActiveWindow.LargeScroll ToRight:=1
ActiveWindow.VisibleRange.Select
' Select the top left cell again, -1 column to get to
'the previous scrollable page
ActiveCell.Offset(0, -1).Select
ActiveWindow.LargeScroll ToLeft:=1
lastColumn = ActiveCell.Column
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.