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/