0

我编写了一个简单的代码来突出显示 Excel 列中最后使用的行。问题是,我想让消息框提及有问题的列 - 可以这样做吗?例如,我在这里使用了 A 列,并且希望消息框显示“A 列中最后一个未使用的行是”,同样如果我将 LR 更改为 B、C 列等。


Sub lastrowcolumn()
Dim LR As Integer
   LR = Cells(Rows.Count, "A").End(xlUp).Row
   Outcome = MsgBox("The last non-used row in column is" & " " & LR)
End Sub
4

2 回答 2

1

要获取工作表上的最后一行或最后一列,您始终可以执行 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
于 2013-06-14T12:48:57.727 回答
0
Option Explicit
Option Base 0

Private Const c_lVeryLastRow As Long = 1048577

'Worksheet_SelectionChange event
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim lFirstUnusedRow As Long

    lFirstUnusedRow = ActiveSheet.Range(Target.Address).End(xlDown).Row + 1
    If lFirstUnusedRow = c_lVeryLastRow Then
        If (Target.Value = "") Then
            lFirstUnusedRow = ActiveSheet.Range(Target.Address).End(xlUp).Row + 1 'Target.Row
        Else
            lFirstUnusedRow = Target.Row + 1
        End If
    End If
    Call MsgBox(CStr(lFirstUnusedRow))
End Sub
于 2013-06-14T12:48:21.350 回答