Jon EvanCook
New Member
- Joined
- Sep 30, 2019
- Messages
- 11
Excel 2007 VBA
I have a spreadsheet which currently uses the database functions and VBA to to select specific rows of the 'Database' area("A7:A48") and store them as a continuous block of data in the 'Extract' area("U7:AM48"). In this case the data is stored as values and I have no problems identifying the row containing the last data set using xlup etc.
I have now changed the spreadsheet to use formulae to extract the data rows. I am now having problems trying to find a simple method to find the last row of the extracted data set as instead of blank cells in the extract area, all the cells now contain formulae and xlup only finds the last formula. The last row is easily identified as each row of the data set contains a unique sequential number in increasing values(range Z8:Z48) so the last row always contains the maximum value.
Test routine - this differs slightly from my main procedure because the workbook contains several sheets which can call the same macro and which stores the sheet name in a string variable called 'mainsheet'.
I get a Run-time error '91' message saying 'Object variable or With block variable not set.' which I cannot identify. I'm sure the solution must be simple but...
The Target Range Z8:Z48 contains numbers in this case Z8 formula returns the value 1 and Z9 contains the result 2 and MaxVal is equal to 2 but it appears the line 'FinSeq = TgtRng.Row' finds no row to return.
Jon
I have a spreadsheet which currently uses the database functions and VBA to to select specific rows of the 'Database' area("A7:A48") and store them as a continuous block of data in the 'Extract' area("U7:AM48"). In this case the data is stored as values and I have no problems identifying the row containing the last data set using xlup etc.
I have now changed the spreadsheet to use formulae to extract the data rows. I am now having problems trying to find a simple method to find the last row of the extracted data set as instead of blank cells in the extract area, all the cells now contain formulae and xlup only finds the last formula. The last row is easily identified as each row of the data set contains a unique sequential number in increasing values(range Z8:Z48) so the last row always contains the maximum value.
Test routine - this differs slightly from my main procedure because the workbook contains several sheets which can call the same macro and which stores the sheet name in a string variable called 'mainsheet'.
Code:
Sub MaxValRow()
Dim MaxVal As Double
Dim Myrange As Range, TgtRng As Range
Dim FinSeq As Range
Set Myrange = ActiveWorkbook.Sheets("4").Range("Z8:Z48") 'selection
MaxVal = Application.Max(Myrange)
Set TgtRng = Myrange.Find(what:=MaxVal)
FinSeq = TgtRng.Row
MsgBox "Maximum is in Row " & FinSeq
End Sub
I get a Run-time error '91' message saying 'Object variable or With block variable not set.' which I cannot identify. I'm sure the solution must be simple but...
The Target Range Z8:Z48 contains numbers in this case Z8 formula returns the value 1 and Z9 contains the result 2 and MaxVal is equal to 2 but it appears the line 'FinSeq = TgtRng.Row' finds no row to return.
Jon