我想(动态)获取范围的最后一个值。因此,我有以下代码。
Parser Error Message: This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms.
但是这给了TRUE而不是我要找的东西(单元格的实际值)。我有什么想法才能获得价值?
答案 0 :(得分:2)
用这一行替换所有行:
MsgBox Cells(Rows.Count,3).End(xlUp).Value
它将返回C列中具有值的最后一个单元格的值。
答案 1 :(得分:1)
来自Ron DeBruin的网站。我经常用这个。这是获取范围中最后一行,列或单元格的更可靠方法。使用xldown到达最后一行是使用xldown,它将停在第一个空白单元格。 .usedrange将捕获工作表上的任何重影格式,并且可以提供不可预测的结果
Function Last(choice As Long, rng As Range)
'Ron de Bruin, 5 May 2008
' 1 = last row
' 2 = last column
' 3 = last cell
Dim lrw As Long
Dim lcol As Long
Select Case choice
Case 1:
On Error Resume Next
Last = rng.Find(What:="*", _
After:=rng.Cells(1), _
lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
Case 2:
On Error Resume Next
Last = rng.Find(What:="*", _
After:=rng.Cells(1), _
lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
Case 3:
On Error Resume Next
lrw = rng.Find(What:="*", _
After:=rng.Cells(1), _
lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
On Error Resume Next
lcol = rng.Find(What:="*", _
After:=rng.Cells(1), _
lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
On Error Resume Next
Last = rng.Parent.Cells(lrw, lcol).Address(False, False)
If Err.Number > 0 Then
Last = rng.Cells(1).Address(False, False)
Err.Clear
End If
On Error GoTo 0
End Select
End Function
然后这样称呼:
sub get_the_Last()
dim TheValue as string
dim myRange as range
set myRange = (assign your range here)
TheValue = range(Last(3, MyRange)).value
end sub
答案 2 :(得分:-2)
我就是这样做的
Sub Test()
Dim wb As Workbook
Dim ws As Worksheet
Dim x1 As Variant
Set wb = ActiveWorkbook
Set ws = wb.ActiveSheet
ws.Range("C13").Select
x1 = Selection.Rows(Selection.Rows.Count).Value
MsgBox (x1)
End Sub
所以Selection.Rows.Count返回所选范围内存在的行数。
可替换地:
Sub Test1()
Dim wb As Workbook
Dim ws As Worksheet
Dim x1 As Variant
Set wb = ActiveWorkbook
Set ws = wb.ActiveSheet
ws.Range("C13").Select
MsgBox Selection.Rows(Selection.Rows.Count).Value
End Sub