要获取工作表上的最后一行或最后一列,您始终可以执行 Sheet.UsedRange.Rows.Count 或 Sheet.UsedRange.Columns.Count
您可以这样来获取某个单元格或范围的列:
Split(Columns(Cells(1, 30).Column).Address(False, False), ":")
单元格的列:
Sub Example()
Dim LR As Long
Dim Col() As String
LR = Cells(Rows.Count, "A").End(xlUp).Row
Col = Split(Columns(Cells(1, 30).Column).Address(False, False), ":")
MsgBox ("The last non-used cell is in column " & Col(0) & " row " & LR)
End Sub
范围的列:
Sub Example()
Dim LR As Long
Dim Col() As String
LR = Cells(Rows.Count, "A").End(xlUp).Row
Col = Split(Columns(Range("A:C").Columns.Count).Address(False, False), ":")
MsgBox ("The last non-used cell is in column " & Col(0) & " row " & LR)
End Sub
工作表中的最后一列:
Sub Example()
Dim LR As Long
Dim Col() As String
LR = Cells(Rows.Count, "A").End(xlUp).Row
Col = Split(Columns(ActiveSheet.UsedRange.Columns.Count).Address(False, False), ":")
MsgBox ("The last non-used cell is in column " & Col(0) & " row " & LR)
End Sub
-编辑-
最后未使用的列:
Sub Example()
Dim LR As Long
Dim Col() As String
LR = Cells(Rows.Count, "A").End(xlUp).Row
'Col = Split(Columns(ActiveSheet.UsedRange.Columns.Count + 1).Address(False, False), ":")
'Alternative method to get column number
Col = Split(Columns(ActiveSheet.Columns.Count).End(xlToLeft).Address(False, False), ":")
MsgBox ("The last non-used cell is in column " & Col(0) & " row " & LR)
End Sub
最后未使用的行:
Sub Example()
Dim LR As Long
Dim Col() As String
LR = ActiveSheet.UsedRange.Rows.Count + 1
Col = Split(Columns(ActiveSheet.UsedRange.Columns.Count + 1).Address(False, False), ":")
MsgBox ("The last non-used cell is in column " & Col(0) & " row " & LR)
End Sub