2013-03-12 75 views
4

我有一個自動篩選範圍的工作表,它以單元格B3開頭。列A包含一些宏按鈕,但實際上是空白的。前兩行包含有關主範圍內數據的信息。VBA LastRow計算不起作用

在VBA中,我使用了我認爲是確定一個工作表中的最後一排的標準方法(在這種情況下,我不能靠.End方法對單個列):

LastRow = Activesheet.Cells.Find("*",SearchOrder:=xlByRows,SearchDirection:=xlPrevious).Row 

然而,有時這會返回值1,即使我有數千行數據。它似乎只有在設置了過濾器的情況下才會執行此操作(但仍然有可見的行中包含數據),但即使這樣也不會總是發生,並且我無法看到它的模式。

我知道有其他的解決方案 - 我已經改變爲UsedRange技術代替,但它是非常令人沮喪的是,因爲它本來是一個最有效的在這種情況下,這個特殊的一個失敗。

有誰知道爲什麼會發生這種情況?

+0

我發現Find方法有時候很奇怪。你有沒有試過在你的'Find'調用中指定'Lookin:= xlValues'(或者是否所有的公式都切換到'xlFormulas')? – CuberChase 2013-03-13 06:45:34

回答

1

你有沒有想過使用Greg的答案,但環找到最高所有列的排?例如:

LastRow = 1 
With ActiveSheet 
    For i = 1 to .UsedRange.Columns.Count 
     If .Cells(.Rows.Count, i).End(xlUp).Row > LastRow Then 
     LastRow = .Cells(.Rows.Count, i).End(xlUp).Row 
     EndIf 
    Next i 
End With 

此解決方案將允許隨機填充底部行中的空白值。 UsedRange非常棘手,因爲它會返回曾經被編輯過的最遠離的行/列(即使它現在是空白的)。根據我的經驗,如果在工作表中按下Ctrl-Up,則Range.End(xlUp)的行爲與您所期望的相同。這有點更可預測。

如果您使用.Find設置嘗試查看After:= [A1]參數。我沒有探索過這個功能的特質,但那會是我開始解決這個問題的地方。

1

試試這個...

Dim LastRow as long 

With ActiveSheet 
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row 
End With 

這將讓A列最後一行

+0

謝謝,但我已經意識到其他的解決方案,在這種情況下我不能依賴單列。如果它在這種情況下起作用,那麼.find方法將是最有效的。我已經相應地更新了這個問題。 – BBaxter 2013-03-12 20:21:37

0

試試下面的代碼:

Sub GetColA_LastRow() 
    Dim ws As Worksheet 
    Dim lRow As Long 

    Set ws = ThisWorkbook.Sheets("Sheet1") 

    With ws 
     lRow = .Range("A" & .Rows.Count).End(xlUp).Row 
    End With 

    MsgBox "The last row which has data in Col A of Sheet1 is " & lRow 
End Sub 

OR

sub getLastRow() 
dim lastRow as long 
lastRow = Sheets("sheet1").Range("A65000").End(xlUp).Row 

end sub 

你也可訪問鏈接以獲取更多詳情 http://www.siddharthrout.com/2012/10/02/find-last-row-in-an-excel-sheetvbavb-net/

更新註釋後的代碼:

Sub getLastRow() 

    Dim rng As Range, lastRow As Long 
    Set rng = Cells.Find("mango") ' here you enter whatever you want to find 

    If Not rng Is Nothing Then 
     lastRow = Sheets("sheet1").Cells(65000, rng.Column).End(xlUp).Row 
    End If 

End Sub 
+0

謝謝,但我已經意識到其他解決方案,在這種情況下我不能依賴單列。如果它在這種情況下起作用,那麼.find方法將是最有效的。我已經相應地更新了這個問題。 – BBaxter 2013-03-12 20:21:03

+0

看看我的解決方案,然後使用UsedRange對象。 – 2013-03-12 21:46:44

+0

@BBaxter我已經更新了代碼。 – 2013-03-13 05:26:41

0

怎麼樣:

with activesheet.usedrange 
    LastRow = .rows.count 
end with 

心連心 菲利普

+0

精湛............. – 2013-03-13 05:27:26

1

建議您嘗試使用Find指定查找XlFormulas,與XlValues不同,將使用此參數檢測隱藏的單元格(但未篩選的單元格)。

Sub Test() 
Dim rng1 As Range 
Set rng1 = ActiveSheet.Cells.Find("*", [a1], xlFormulas, , xlByRows, xlPrevious) 
If Not rng1 Is Nothing Then MsgBox rng1.Row 
End Sub 
0

我的類似問題是最後一行和列使用了什麼,不管之前是否使用空單元格,使用或不使用過濾。我從我能找到的零碎中拼湊出來,至少對於填充了數據的單元格,我認爲它們符合我和你的要求。

Function FindLastUsedRowAndCol(ByVal ws As Worksheet) As Variant() 

Dim LastColRange As Range 
Dim LastCol As Integer 
Dim LastRow As Long 
Dim LastRowTmp As Long 
Dim RowXCol(2) As Variant 


Set LastColRange = ws.Cells.Find(What:="*", After:=ws.Cells(1, 1), LookIn:=xlFormulas, LookAt:= _ 
    xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False) 
LastCol = LastColRange.Column 

LastRow = 1 

For i = 1 To LastCol Step 1 
    If ws.FilterMode Then 
     LastRow = ws.AutoFilter.Range.Rows.Count 
     LastRowTmp = Cells(ws.Rows.Count, i).End(xlUp).row 
     If LastRowTmp > LastRow Then LastRow = LastRowTmp 
    Else 
     LastRowTmp = Cells(ws.Rows.Count, i).End(xlUp).row 
     If LastRowTmp > LastRow Then LastRow = LastRowTmp 
    End If 
Next i 

RowXCol(1) = LastRow 
RowXCol(2) = LastCol 
FindLastUsedRowAndCol = RowXCol 

End Function 

,並測試:

Sub testit() 
Dim ws As Worksheet 
Set ws = Application.Worksheets("Sheet1") 
cr = FindLastUsedRowAndCol(ws) 
MsgBox "row: " & cr(1) & " col: " & cr(2) 
End Sub 
+0

從頭開始。它不適用於所有情況 – JoeWalt 2015-03-08 20:35:28

1

我今天早上遇到的確切同樣的問題。

起初,我確信「.find」功能是不穩定的。

但是,在擺弄了一段時間之後,我發現一個非空單元格在我的表格中行號太深,認爲它是1,000或10,000或相似。我刪除它,「.find」再次運作。可能,某些內部VBA變量的限制不夠大。

這樣做:

1)按CTRL + END

2)確定的非空單元格(或多個),假設這是在不經意間填寫,並將其刪除。