2

可能重复:
行中最后一个非空单元格;Excel VBA使用 VBA
查找 Excel 工作表中非空白列的数量

您好,我已经编写了一个 vba 代码来获取选定单元格(活动单元格)的地址。但我想要最后使用的列地址的地址,这是我写的代码

Dim a As String
a = Split(ActiveCell.Address, "$")(1)
MsgBox a

它工作正常,但我想要最后使用的列的地址。就像我有高达“AB”列的值一样,我想使用 vba 代码获取该地址。

4

1 回答 1

3

像这样?

Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim a As String
    Dim LastCol As Long

    '~~> Set this to the relevant sheet
    Set ws = ThisWorkbook.Sheets("Sheet1")

    '~~> Get the last used Column
    LastCol = LastColumn(ws)

    '~~> Return Column Name from Column Number
    a = Split(ws.Cells(, LastCol).Address, "$")(1)

    MsgBox a
End Sub

Public Function LastColumn(Optional wks As Worksheet) As Long
    If wks Is Nothing Then Set wks = ActiveSheet
    LastColumn = wks.Cells.Find(What:="*", _
                After:=wks.Range("A1"), _
                Lookat:=xlPart, _
                LookIn:=xlFormulas, _
                SearchOrder:=xlByColumns, _
                SearchDirection:=xlPrevious, _
                MatchCase:=False).Column
End Function
于 2012-10-03T10:17:40.103 回答