我想选择最后一个单元格(最后一行,最后一列)。
Range(ColumnName & Rows.Count).End(xlUp).Select
ColumnName是按字母顺序排列的最后一列(例如:D),声明为字符串。
这不起作用。但是这是的:
Range("D" & Rows.Count).End(xlUp).Select
编辑:
'colNum as integer (e.g: 4)
'this code transform number to alphabetical
Public Function ColumnName(colNum As Integer) As String
Dim d As Integer
Dim m As Integer
Dim name As String
d = colNum
name = ""
Do While (d > 0)
m = (d - 1) Mod 26
name = Chr(65 + m) + name
d = Int((d - m) / 26)
Loop
ColumnName = name
'If i do "MsgBox ColumnName", and "MsgBox CStr(Len(ColumnName))" I obtain D, and 1.
End Function
答案 0 :(得分:0)
"D"
与值为"D"
的字符串变量之间应该存在无差异,因此我建议你可能认为ColumnName
是{ {1}}可能不正确。请仔细检查。
您可以使用以下内容执行此操作:
"D"
并确保您获得MsgBox ColumnName
MsgBox CStr(Len(ColumnName))
和D
作为结果。
答案 1 :(得分:0)
要找到最后一个Cell,您可以试试这个。这不完全是对您的代码的修改,但下面的结果将帮助您选择当前表格中的最后一个单元格如果我从第一句话中正确理解的是您的要求。
Dim LCol As Long
LCol = Cells.Find(What:="*", After:=Cells(1, 1), LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False).Column
Cells(Rows.Count, LCol).End(xlUp).Select
答案 2 :(得分:0)
您必须将ColumnName声明为变量。
Sub Button1_Click()
Dim ColumnName As String
ColumnName = "D"
Range(ColumnName & Rows.Count).End(xlUp).Select
End Sub
这是一种更流行的方式。
Sub SelectLastCellInColumnOption()
Dim Rws As Long
Rws = Cells(Rows.Count, "D").End(xlUp).Row
Cells(Rws, "D").Select
End Sub