vba - 如何使用vba查找上次使用的列的地址

标签 vba excel

Possible Duplicate:
Last not empty cell in row; Excel VBA
Finding the number of non-blank columns in an Excel sheet using VBA

您好,我编写了一个用于获取所选单元格(事件单元格)地址的 vba 代码。但我想要最后使用的列地址的地址,这是我写的代码

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

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

最佳答案

像这样吗?

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

关于vba - 如何使用vba查找上次使用的列的地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12706325/

相关文章:

VBA Find 函数忽略百分比 -> 只查找小数

excel - VBA 自定义保存

c# - 如何在C#中的数字前用零制作Excel

csv - 是否可以使用宏将 csv 批量转换为 xls?

excel - 如何在 VBA 中获取标签的内部文本,不包括嵌套标签中的文本?

database - Access ,选择 Case ActiveControl.Name?

excel - 将 Illustrator 文件转换/导入 Excel

excel - 如何对包含公式的单元格求和

vba - 从列中的单元格中删除多余的空格

VBA:用于数组的 For 循环给出错误