1

Is there a way to find the last cell in a colum that contains a specific format?

Currently I use a for loop, below

LastRowNumber = Range("A:A").Find(What:="", after:=Range("A1"), searchdirection:=xlPrevious).Row

For i = 1 To 10000


            If Cells(LastRowNumber - i, 1).NumberFormat = "[h]:mm:ss" Then

                LastRowNumber = LastRowNumber - i

            End If


Next i

This loop find the last cell in the column then search the cell above to see if its in the format of [h]:mm:ss however this just loops through until it fails when i = LastRowNumber is there a better way to solve this?

Vityata
  • 42,633
  • 8
  • 55
  • 100
CptGoodar
  • 303
  • 2
  • 15

1 Answers1

1

If you need the last cell containing the specific format, start to loop from the last used cell in the column:

For i = 10000 To 1 Step -1
    If Cells(i, 1).NumberFormat = "[h]:mm:ss" Then
        lastRowNumber = i
        Exit For
    End If
Next i

Make sure to make 10000 a variable, based on the last used cell in a given column, if this is applicable - Excel VBA- Finding the last column with data

Vityata
  • 42,633
  • 8
  • 55
  • 100