VBA - Finding first cell address with formula in a column

dellzy

Board Regular
Joined
Apr 24, 2013
Messages
146
Hi Experts,

I wish to know which cell in column M in Excel worksheet that has formula so that it will be the first range address when user selecting a range to be pasted as values. So far, what I've got seems to check the cell which is not blank only. So, it will still return the cell address even it contains values only, not formula. Also, I don't know how to stop the loop once it finds the first cell address with formula.

Below what I have so far. Appreciate some good help, please.

Code:
Function IsFormula(cell_ref As Range)
    IsFormula = cell_ref.HasFormula
End Function


Sub ShowCellWFormula()
Dim j As Integer
Dim DestRng As Range
j = 5
LRow = 8


For j = j To LRow
If Range("M" & j).HasFormula() = False Then
MsgBox "nope"
j = j + 1
End If
Set DestRng = Range("M" & j)
MsgBox DestRng.Address


Next j
End Sub


Thank you so much in advance.
 
Last edited:

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
This will find the first formula cell in Col M. You can adapt it to do whatever you want once the first cell is identified.
Code:
Sub ShowCellWFormula()
Dim DestRng As Range
LRow = Range("M" & Rows.Count).End(xlUp).Row
With Range("M1:M" & LRow)
       On Error Resume Next
       Set DestRng = .SpecialCells(xlCellTypeFormulas).Areas(1).Cells(1, 1)
       On Error GoTo 0
       If Not DestRng Is Nothing Then
              MsgBox "First formula cell in col M is cell " & DestRng.Address
       Else
              MsgBox "Col M doesn't have any formula"
       End If
End With
End Sub
 
Upvote 0
Wow! It works exactly how I want it to be! Tq soooo much JoeMo ! Your code brings so much benefit to my other function codes! :)

God Bless You.. ;)
 
Upvote 0
This will find the first formula cell in Col M. You can adapt it to do whatever you want once the first cell is identified.
Rich (BB code):
Sub ShowCellWFormula()
Dim DestRng As Range
LRow = Range("M" & Rows.Count).End(xlUp).Row
With Range("M1:M" & LRow)
       On Error Resume Next
       Set DestRng = .SpecialCells(xlCellTypeFormulas).Areas(1).Cells(1, 1)
       On Error GoTo 0
       If Not DestRng Is Nothing Then
              MsgBox "First formula cell in col M is cell " & DestRng.Address
       Else
              MsgBox "Col M doesn't have any formula"
       End If
End With
End Sub
You can shorten the highlighted line of code to this..

Code:
Set DestRng = .SpecialCells(xlCellTypeFormulas)(1)

Since LRow is used only once in the code and since the object of the With statement is referenced only once, it is possible to condense your code to this...

Code:
[SIZE=1]Sub ShowCellWFormula()
  Dim DestRng As Range
  On Error Resume Next
  Set DestRng = Range("M1", Range("M" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeFormulas)(1)
  On Error GoTo 0
  If Not DestRng Is Nothing Then
         MsgBox "First formula cell in col M is cell " & DestRng.Address
  Else
         MsgBox "Col M doesn't have any formula"
  End If
End Sub[/SIZE]

While in "real life", I would probably use the method you posted, I thought the readers of this thread might find this alternated code of interest in that it eliminates calling SpecialCells which, in turn, eliminates the need for the error handler, although I will admit it obfuscates the code just a "little bit":LOL:...

Code:
[SIZE=1]Sub test()
  Dim Rw As Long, ColM As Variant, DestRng As Range
  ColM = Range("M1", Cells(Rows.Count, "M").End(xlUp)).Formula
  ColM = Join(Application.Transpose(ColM), Chr(1))
  If InStr(Chr(1) & ColM, Chr(1) & "=") Then
    Set DestRng = Cells(UBound(Split(Chr(1) & Split(Chr(1) & ColM, Chr(1) & "=")(0), Chr(1))), "M")
    MsgBox "First formula cell in col M is cell " & DestRng.Address(0, 0)
  Else
    MsgBox "Col M doesn't have any formula"
  End If
End Sub[/SIZE]
 
Last edited:
Upvote 0
You can shorten the highlighted line of code to this..

Code:
Set DestRng = .SpecialCells(xlCellTypeFormulas)(1)

Since LRow is used only once in the code and since the object of the With statement is referenced only once, it is possible to condense your code to this...

Code:
[SIZE=1]Sub ShowCellWFormula()
  Dim DestRng As Range
  On Error Resume Next
  Set DestRng = Range("M1", Range("M" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeFormulas)(1)
  On Error GoTo 0
  If Not DestRng Is Nothing Then
         MsgBox "First formula cell in col M is cell " & DestRng.Address
  Else
         MsgBox "Col M doesn't have any formula"
  End If
End Sub[/SIZE]

While in "real life", I would probably use the method you posted, I thought the readers of this thread might find this alternated code of interest in that it eliminates calling SpecialCells which, in turn, eliminates the need for the error handler, although I will admit it obfuscates the code just a "little bit":LOL:...

Code:
[SIZE=1]Sub test()
  Dim Rw As Long, ColM As Variant, DestRng As Range
  ColM = Range("M1", Cells(Rows.Count, "M").End(xlUp)).Formula
  ColM = Join(Application.Transpose(ColM), Chr(1))
  If InStr(Chr(1) & ColM, Chr(1) & "=") Then
    Set DestRng = Cells(UBound(Split(Chr(1) & Split(Chr(1) & ColM, Chr(1) & "=")(0), Chr(1))), "M")
    MsgBox "First formula cell in col M is cell " & DestRng.Address(0, 0)
  Else
    MsgBox "Col M doesn't have any formula"
  End If
End Sub[/SIZE]

Well, tqvm for the detailed alternate codes to simplify it, Mr. Rick Rothstein.. Totally appreciate the time you spent and knowledge you transfer here. You guys are awesome! :)
 
Upvote 0

Forum statistics

Threads
1,216,112
Messages
6,128,901
Members
449,477
Latest member
panjongshing

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top