有没有办法获取第一个未隐藏列的列号?
因此,如果隐藏了A列,则返回2.
如果隐藏A列和B列,则返回3.
如果A和C被隐藏,则返回2等
答案 0 :(得分:4)
没有循环
更新:未处理隐藏最左侧列的情况。如果无细胞测试是多余的,则Tmdean的较短解决方案是优越的
Sub FirstNonHidden()
Dim rng1 As Range
Set rng1 = Cells.SpecialCells(xlCellTypeVisible)
If rng1.Column <> 1 Then
MsgBox rng1.Areas(1).Column
Else
MsgBox "No hidden cells"
End If
End Sub
答案 1 :(得分:3)
Sub test()
Dim ColCounter As Integer
ColCounter = 1
Do While ColCounter > 0 And ColCounter < 66536
If Sheet1.Columns(ColCounter).Hidden = False Then
MsgBox (ColCounter)
Exit Do
End If
ColCounter = ColCounter + 1
Loop
End Sub
答案 2 :(得分:3)
最简单的方法可能是
Dim column_num As Long
column_num = ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible).Cells(1).Column
使用循环会比bhuang3的解决方案更有效,但它可以更简单一点。
Dim cursor As Range, column_num As Long
Set cursor = Range("A1")
Do Until cursor.ColumnWidth > 0
Set cursor = cursor.Offset(0, 1)
Loop
column_num = cursor.Column
答案 3 :(得分:0)
试试这个
ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Offset(1, 0).End(xlToLeft).Column
修改
证明确实有效