VBA,在整个 sheet 中找到最后使用的列
VBA, find last used column in the whole sheet
我用谷歌搜索了很多,发现了很多不同的解决方案,但我需要改进我现在使用的解决方案。
我想使用查找方法在 sheet 中找到最后使用的列,而不考虑删除的单元格。
我只想获取使用的最后一列,包括起始单元格行中的那一列。在下图中,如果我使用我的代码,它将给出最后一列 = 4,因为在第二行中,数据停在第 4 列。为什么结果不给出 5(header 列)?
谢谢!!
With ActiveSheet
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
findlastcol = .Cells.Find(What:="*", _
After:=.Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
Else
findlastcol = 1
End If
End With
例子Tablescreenshot
+---------+---------+---------+---------+---------+
| Header1 | Header2 | Header3 | Header4 | Header5 |
+---------+---------+---------+---------+---------+
| Data | Data | Data | Data | |
+---------+---------+---------+---------+---------+
您可以试试下面的代码:
Sub FindLastColumn()
Dim iLastCol As Integer
ActiveSheet.UsedRange 'Refreshing used range (may need to save wb also)
iLastCol = ActiveSheet.UsedRange.Columns(ActiveSheet.UsedRange.Columns.Count).Column
End Sub
或者,您可以尝试:
findlastcol = Selection.SpecialCells(xlCellTypeLastCell).Column
AutoFilter 启动查找方法
- 使用
xlFormulas
的 Find
方法几乎 'bullet proof',除非您的情况涉及过滤器。
- 以下示例显示了如何通过关闭
AutoFilter
来实现这一点,这并不是人们想要的。它还显示了如何存在三个不需要的参数。此外,这是一种不同的方法,不需要 CountA
.
- 一个合适的解决方案是将当前过滤器复制到Filter object and then apply it later back. Here is an example如何做。
代码
Sub testBulletProof()
Dim LastCol As Long
Dim rng As Range
With ActiveSheet
If .AutoFilterMode Then
.AutoFilterMode = False
End If
Set rng = .Cells.Find(What:="*", _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious)
End With
If Not rng Is Nothing Then
LastCol = rng.Column
Else
LastCol = 1
End If
Debug.Print LastCol
End Sub
- 由于您可能知道 header 所在的行,并且数据的列数不会比 header 多,因此您可以使用:
代码
Sub testFindInRow()
Dim LastCol As Long
Dim rng As Range
With ActiveSheet
Set rng = .Rows(1).Find(What:="*", _
LookIn:=xlFormulas, _
SearchDirection:=xlPrevious)
End With
If Not rng Is Nothing Then
LastCol = rng.Column
Else
LastCol = 1
End If
Debug.Print LastCol
End Sub
我用谷歌搜索了很多,发现了很多不同的解决方案,但我需要改进我现在使用的解决方案。
我想使用查找方法在 sheet 中找到最后使用的列,而不考虑删除的单元格。
我只想获取使用的最后一列,包括起始单元格行中的那一列。在下图中,如果我使用我的代码,它将给出最后一列 = 4,因为在第二行中,数据停在第 4 列。为什么结果不给出 5(header 列)?
谢谢!!
With ActiveSheet
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
findlastcol = .Cells.Find(What:="*", _
After:=.Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
Else
findlastcol = 1
End If
End With
例子Tablescreenshot
+---------+---------+---------+---------+---------+
| Header1 | Header2 | Header3 | Header4 | Header5 |
+---------+---------+---------+---------+---------+
| Data | Data | Data | Data | |
+---------+---------+---------+---------+---------+
您可以试试下面的代码:
Sub FindLastColumn()
Dim iLastCol As Integer
ActiveSheet.UsedRange 'Refreshing used range (may need to save wb also)
iLastCol = ActiveSheet.UsedRange.Columns(ActiveSheet.UsedRange.Columns.Count).Column
End Sub
或者,您可以尝试:
findlastcol = Selection.SpecialCells(xlCellTypeLastCell).Column
AutoFilter 启动查找方法
- 使用
xlFormulas
的Find
方法几乎 'bullet proof',除非您的情况涉及过滤器。 - 以下示例显示了如何通过关闭
AutoFilter
来实现这一点,这并不是人们想要的。它还显示了如何存在三个不需要的参数。此外,这是一种不同的方法,不需要CountA
. - 一个合适的解决方案是将当前过滤器复制到Filter object and then apply it later back. Here is an example如何做。
代码
Sub testBulletProof()
Dim LastCol As Long
Dim rng As Range
With ActiveSheet
If .AutoFilterMode Then
.AutoFilterMode = False
End If
Set rng = .Cells.Find(What:="*", _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious)
End With
If Not rng Is Nothing Then
LastCol = rng.Column
Else
LastCol = 1
End If
Debug.Print LastCol
End Sub
- 由于您可能知道 header 所在的行,并且数据的列数不会比 header 多,因此您可以使用:
代码
Sub testFindInRow()
Dim LastCol As Long
Dim rng As Range
With ActiveSheet
Set rng = .Rows(1).Find(What:="*", _
LookIn:=xlFormulas, _
SearchDirection:=xlPrevious)
End With
If Not rng Is Nothing Then
LastCol = rng.Column
Else
LastCol = 1
End If
Debug.Print LastCol
End Sub