VBA for Last Cell in Formula Range

  • Thread starter Thread starter dhockin
  • Start date Start date
D

dhockin

Using VBA
Trying to indentify the last cell on sheet that has a formula.

code fragment below

have tried
Set rge = Sh.UsedRange.SpecialCells(xlCellTypeFormulas,
xlCellTypeLastCell)
msgbox rge.Address

However I this returns the address of all formula cells

Any suggestions to get the address of the last cell with a formula ?

ie used range a1: j 100 , and last formula may be j 89
 
One way:

On Error Resume Next
With Sh.Cells.SpecialCells(xlCellTypeFormulas)
With .Areas(.Areas.Count)
Set rge = .Cells(.Count)
End With
End With
On Error GoTo 0
If Not rge Is Nothing Then MsgBox rge.Address
 
I think it depends on what you mean.

If I have a formula in A1:A100 and another in X1, what's the address of that
lastcell with a formula--is it A100 or X1?

But maybe...

Option Explicit
Sub testm()

Dim myRng As Range
Set myRng = Nothing
On Error Resume Next
Set myRng = Worksheets("Sheet1").Cells.SpecialCells(xlCellTypeFormulas)
On Error GoTo 0

If myRng Is Nothing Then
MsgBox "hey, no formulas"
Else
With myRng
'last area of this multiarea range
With .Areas(.Areas.Count)
'last cell in this last area
MsgBox .Cells(.Cells.Count).Address
End With
End With
End If

End Sub

But I wouldn't trust this kind of thing--who knows how excel will order those
areas.

If I know what I want, I'd look through rows or columns trying to find that last
formula.
 
just pick the last item from the range:

Sub last_formula()
Set rge = ActiveSheet.UsedRange.SpecialCells(xlCellTypeFormulas,
xlCellTypeLastCell)
f = Split(rge.Address, ",")
u = UBound(f)
MsgBox (f(u))
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Excel VBA 1
Excel VBA setting Ranges 2
COUNTIF Cells in Range? 4
Range to Last Cell 1
VBA set thick borders around non empty cells in range 0
Date and amount of last payment 3
Excel Need Countifs Formula Help 0
Formula help 1

Back
Top