VBA Help - Set Lastrow based on specific text?

Johnny Thunder

Well-known Member
Joined
Apr 9, 2010
Messages
693
Office Version
  1. 2016
Platform
  1. MacOS
Hi guys - working on a project to set some formulas to the lastrow. normally I would use the "Cells(Rows.Count, 1).End(xlUp).Row" type method because it works great for me. But for this worksheet I am unable to use this method so I spotted a column that defines the last row based on a cells text "Grand Total".

Here is my code but it is not working - Not sure of a better way to do this?

Code:
Sub BluePivot()


Dim sht         As Worksheet
Dim lastR       As Long


Set sht = Sheets("Pivot Table")
lastR = Cells.Find(What:="Grandtotal", _
                    After:=Range("BD2"), _
                    LookAt:=xlPart, _
                    LookIn:=xlFormulas, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious, _
                    MatchCase:=False).Row




With sht


Range("BB4:BB4" & lastR & "").FormulaR1C1 = "=IF(RC[8]=TRUE,MAX(R2C:R[-1]C)+1,"""")"


Range("BC4:BC" & lastR & "").FormulaR1C1 = "=IF(R[-1]C="""", """",IF(R[-1]C[1]=""grandtotal"","""",IF(R[-1]C[1]=""title"",R[-1]C+9,R[-1]C)))"

End With

End sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
You haven't qualified your ranges, try
Code:
Sub BluePivot()

Dim lastR       As Long

With Sheets("Pivot Table")
   lastR = .Cells.find(What:="Grandtotal", _
                       After:=Range("BD2"), _
                       LookAt:=xlPart, _
                       lookIn:=xlFormulas, _
                       SearchOrder:=xlByRows, _
                       SearchDirection:=xlPrevious, _
                       MatchCase:=False).Row
   
   .Range("BB4:BB4" & lastR).FormulaR1C1 = "=IF(RC[8]=TRUE,MAX(R2C:R[-1]C)+1,"""")"
   
   .Range("BC4:BC" & lastR).FormulaR1C1 = "=IF(R[-1]C="""", """",IF(R[-1]C[1]=""grandtotal"","""",IF(R[-1]C[1]=""title"",R[-1]C+9,R[-1]C)))"
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

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