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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12706325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 17:57:18  来源:igfitidea点击:

how to find the address of last used column using vba

vbaexcel-vbaexcel

提问by M_S_SAJJAN

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

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

Hello I have written a vba code for getting the address of selected cell(active cell). But I want the address of last used columns address,here is the code what i wrote

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

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

it is working correctly but i want the address of last used columns. like i have the values upto "AB" columns I want get that address using vba code.

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

回答by Siddharth Rout

Like this?

像这样?

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