我找到了这个用于查找包含最后数据的行的方法:
ws.Range("A65536").End(xlUp).row
是否有类似的方法可以找到一个工作表中包含数据的最后一列?
有很多方法可以做到这一点。最可靠的方法是使用find。
Dim rLastCell As Range
Set rLastCell = ws.Cells.Find(What:="*", After:=ws.Cells(1, 1), LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
MsgBox ("The last used column is: " & rLastCell.Column)
如果你想要找到特定行中使用的最后一列,可以使用以下代码:
Dim lColumn As Long
lColumn = ws.Cells(1, Columns.Count).End(xlToLeft).Column
使用已用范围(不太可靠):
Dim lColumn As Long
lColumn = ws.UsedRange.Columns.Count
如果在A列中没有数据,使用“已用范围”将不起作用。查看此处以了解有关“已用范围”的另一个问题:
有关重置“已用范围”的信息,请参见此处。
Application.CountA
与 .Find
一起使用,以避免出现错误。请参考此链接:https://dev59.com/tmfWa4cB1Zd3GeqPdhYt#11883425 - Siddharth Rout我知道这篇文章比较旧,但是我已经进行了多种测试,它仍然没有让我失望,除非有人能告诉我另外一种情况。
行号
Row = ws.Cells.Find(What:="*", After:=[A1] , SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
列字母
ColumnLetter = Split(ws.Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Cells.Address(1, 0), "$")(0)
列号
ColumnNumber = ws.Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
UsedRange
代码,即使起始列为空或不存在,也能够获取最后使用的列。lColumn = ws.UsedRange.Columns.Count
修改为lColumn = ws.UsedRange.Column + ws.UsedRange.Columns.Count - 1
,将始终给出可靠的结果。
?Sheet1.UsedRange.Column + Sheet1.UsedRange.Columns.Count - 1
9
。在激活表格后尝试使用此代码:
Dim J as integer
J = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row
Cells.SpecialCells(xlCellTypeLastCell).Row
,问题将是除非进行“保存文件”操作,否则xlCellTypeLastCell
信息不会得到更新。但使用UsedRange
将始终实时更新信息。这里有一些可能有用的东西。基于包含数据的行选择整个列,在这种情况下我使用第5行:
Dim lColumn As Long
lColumn = ActiveSheet.Cells(5, Columns.Count).End(xlToLeft).Column
MsgBox ("The last used column is: " & lColumn)
如果你的数据从第一行开始,这里有一个简单的选项。
MsgBox "Last Row: " + CStr(Application.WorksheetFunction.CountA(ActiveSheet.Cells(1).EntireRow))
CountA
来计算整行中具有数据的列数。我已经使用@Reafidy method/answer很长时间了,但今天我遇到了一个问题,即顶行从A1合并到N1,我的函数返回的“最后一列”是1而不是14。
这是我修改后的函数,现在考虑可能合并的单元格:
Public Function Get_lRow(WS As Worksheet) As Integer
On Error Resume Next
If Not IsWorksheetEmpty(WS) Then
Get_lRow = WS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Dim Cell As Range
For Each Cell In WS.UsedRange
If Cell.MergeCells Then
With Cell.MergeArea
If .Cells(.Cells.Count).Row > Get_lRow Then Get_lRow = .Cells(.Cells.Count).Row
End With
End If
Next Cell
Else
Get_lRow = 1
End If
End Function
Public Function Get_lCol(WS As Worksheet) As Integer
On Error Resume Next
If Not IsWorksheetEmpty(WS) Then
Get_lCol = WS.Cells.Find(What:="*", after:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
Dim Cell As Range
For Each Cell In WS.UsedRange
If Cell.MergeCells Then
With Cell.MergeArea
If .Cells(.Cells.Count).Column > Get_lCol Then Get_lCol = .Cells(.Cells.Count).Column
End With
End If
Next Cell
Else
Get_lCol = 1
End If
End Function
ws.UsedRange
代替。 - ikhws.Cells(Rows.Count, "A").End(xlUp).row
。 - Reafidy