2016-05-09 4 views
0

Excel vbaで最後の行を検索する方法について問い合わせがあります。私は現在、Annex 1Aと呼ばれるこの特定のワークシートの最後の行を見つける必要があるプロジェクトに取り組んでいます。Excel VBAを使用した最終行の検索

ワークシートのスニップの画像を以下に示す:例えば

Click Here For Image

、上記画像から、Row 32Row 33は、空の値を含み、私が中である行の合計を導出したいです中古。

方法LastRow値は常に31代わりに33を返す2

LastRow = Sheets("Annex 1A").Range("B" & Sheets("Annex1A").Rows.Count).End(xlUp).Row 

1つの

LastRow = Sheets("Annex 1A").Cells.Find(What:="*", _ 
        After:=Sheets("Annex 1A").Range("B1"), _ 
        Lookat:=xlPart, _ 
        LookIn:=xlFormulas, _ 
        SearchOrder:=xlByRows, _ 
        SearchDirection:=xlPrevious, _ 
        MatchCase:=False).Row 

方法:

私はこの次の方法を試みました。 33の値を導き出す他の方法はありますか?

+0

[この記事では、あなたの質問に答えること](http://stackoverflow.com/questions/11169445/error-in-finding-last-used-cell-in-vba) –

+0

は、私が見てきたを通じて@ForwardEdこの記事は参考になりましたが、それでも私が必要とする値を得ることはできません:(しかし、提案に感謝! – MysteryGirl

答えて

-1

この方法は、私がそれを必要な回数の90%のように私のために働いている:

Lastrow = Sheets("Annex 1A").UsedRange.SpecialCells(xlCellTypeLastCell).row 
+0

ありがとう@ gizlmeier!私は最終的にurメソッドを使って33として値を派生させました:)! – MysteryGirl

-1

私はあなたが既に答えを受け入れてきました知っているが、これは他の人に役に立つかもしれません。

UsedRangeは信頼できない可能性があるため、しばしば避けられます。しかし、この場合、それは場所があり、あなたのためのトリックを行う可能性があります。

しかし、UsedRangeの完全な制御を取り戻す場合は、UsedRangeといくつかのセルのチェックを組み合わせた方法が答えになる可能性があります。あなたの場合の例は、空でないセルまたは陰影をつけたセルのいずれかを探すものです。

Public Function BespokeFindLastRow() As Long 
    Dim rng As Range 
    Dim i As Long 

    'Use the intersect function to find column B 
    'as column references of UsedRange are relative 
    'so if nothing is in column "A", then 
    'column(1) or ("A") in your UsedRange would 
    'actually be column "B". 
    With ThisWorkbook.Worksheets("Sheet1") 
     Set rng = Intersect(.UsedRange, .Columns("B")) 
    End With 

    'Return -1 if no cells are found 
    If rng Is Nothing Then 
     BespokeFindLastRow = -1 
     Exit Function 
    End If 

    'Loop backwards through the cells in the column range 
    'to find either a non-empty cell or a coloured cell. 
    For i = rng.Cells.Count To 1 Step -1 
     With rng.Cells 
      If Not IsEmpty(.Item(i)) Or .Interior.ColorIndex <> xlNone Then 
       BespokeFindLastRow = .Row 
       Exit Function 
      End If 
     End With 
    Next 

    'Nothing was found in the column so return -1 
    BespokeFindLastRow = -1 
End Function 
関連する問題