clattenburg cake
clattenburg cake

Reputation: 1222

Getting Messagebox To Mention Specific Column

I've written a simple code to highlight the last used row in an Excel column. Thing is, I want to get the messagebox to mention the column in question- is it possible to do this? For instance I have used column A here, and would like the messagebox to display "The last non-used row in column A is ", likewise if I changed LR to column B, C etc.


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

Upvotes: 0

Views: 951

Answers (2)

Ripster
Ripster

Reputation: 3585

To get the last row or column on a sheet you can always do Sheet.UsedRange.Rows.Count or Sheet.UsedRange.Columns.Count

You can this to get the column of a certain cell or range:

Split(Columns(Cells(1, 30).Column).Address(False, False), ":")

Column of a cell:

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

Column of a range:

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

Last column in a sheet:

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

-Edit-

Last unused column:

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

Last unused row:

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

Upvotes: 1

user2299169
user2299169

Reputation:

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

Upvotes: 0

Related Questions