To avoid reprimand for cross posting this is the original post that I’m diverging from:
Copy MessageBox returned value to a cell
For benefit of future hits on this, key elements are highlighted red.
Sorry for over kill of data/code & dialog, been stung in past with a lack of both. But also spent too long testing code found online that as a complete numpty/novic couldn't decipher due to a lack of detail/knowlege/understanding . Hope this will be of assistance to at least a few people. "Like" it if you find it helpfull, I'd be chuffed.
Can someone suggest a way to overcome/eliminate potential INACCURATE results?
Essentially this is a matter of having found a specific lastrow "T:T “Cash Paid” to then use “End(xlUp).Row” on THAT row to find the ACTUAL LASTED USED cell in Col S.
Have come up with (what I believe to be a convoluted solution) to return the range address that I want to use in my main code.
It is still a bit flawed because I'm using " End(xlDown).Row", it leaves it open to INACCURATE results WHEN/IF there is a blank cell somewhere in Col S BEFORE “lastrow Range("T:T")” “Cash Paid”
So by using lastrow "T:T “Cash Paid" and " End(xlUP).Row" it will would overcome INACCURATE results
The fundamental starting point is the code below; it establishes the first & last rows of the range in question.
My complete code in use is:-
Copy MessageBox returned value to a cell
For benefit of future hits on this, key elements are highlighted red.
Sorry for over kill of data/code & dialog, been stung in past with a lack of both. But also spent too long testing code found online that as a complete numpty/novic couldn't decipher due to a lack of detail/knowlege/understanding . Hope this will be of assistance to at least a few people. "Like" it if you find it helpfull, I'd be chuffed.
Can someone suggest a way to overcome/eliminate potential INACCURATE results?
Essentially this is a matter of having found a specific lastrow "T:T “Cash Paid” to then use “End(xlUp).Row” on THAT row to find the ACTUAL LASTED USED cell in Col S.
Have come up with (what I believe to be a convoluted solution) to return the range address that I want to use in my main code.
It is still a bit flawed because I'm using " End(xlDown).Row", it leaves it open to INACCURATE results WHEN/IF there is a blank cell somewhere in Col S BEFORE “lastrow Range("T:T")” “Cash Paid”
So by using lastrow "T:T “Cash Paid" and " End(xlUP).Row" it will would overcome INACCURATE results
The fundamental starting point is the code below; it establishes the first & last rows of the range in question.
Excel Formula:
firstrow = .Range("s:s").Find(what:="CF rules", LookIn:=xlValues, LookAt:=xlWhole).Row '=S7
lastrow = .Range("T:T").Find(what:="Cash Paid", LookIn:=xlValues, LookAt:=xlWhole).Row '= T342
VBA Code:
Sub MeUsedRangetest()
Dim sht As Worksheet
Dim lastrow As Long, firstrow As Long
Dim LstUsedRow As Variant
Dim FstUsedCell As Variant
With ActiveSheet
firstrow = .Range("s:s").Find(what:="CF rules", LookIn:=xlValues, LookAt:=xlWhole).Row '=$A$7
lastrow = .Range("T:T").Find(what:="Cash Paid", LookIn:=xlValues, LookAt:=xlWhole).Row '=$S$342
'---------------------------------
' Ignore Next section, as is just about returning test results to the ActiveSheet (relevant in testing process, simply left in to show the process I used to get to final result )
.Range("ar340") = .Range("S" & firstrow + 3 & ":T" & lastrow - 1).CurrentRegion.Address ‘=$A$6:$S$337
.Range("as340") = .Range("S" & firstrow + 3 & ":s" & lastrow - 1).CurrentRegion.Address ‘=$A$6:$S$337
.Range("at340") = .Range("S" & firstrow + 3 & ":T" & lastrow - 1).Address ‘=$A$6:$S$337
.Range("au340") = .Range("s" & firstrow + 3 & ":s" & lastrow - 1).Address ‘=$S$10:$S$341
.Range("av340") = Range("s" & LstUsedRow).Address '= "$S$337"
.Range("ay340") = .Range("S" & firstrow + 3).Address ‘=$S$10
.Range("az340") = .Range("S" & lastrow).Address ‘=$S$342
.Range("ba340") = .Range("S" & lastrow - 1).Address ‘=$S$341
.Range("bb340") = FstUsedCell '= "$S$10:$S$337"
'--------------------------------------
'This next section returns the "ACTUAL USED RANGE" of Col S, between "firstrow" and the dynamic "lastrow"
LstUsedRow = .Range("s10").End(xlDown).Row 'This works, needs "Dim LstUsedRow As Variant"
.Range("av340") = Range("s" & LstUsedRow).Address ' = "$S$337"
FstUsedCell = Range("S" & firstrow + 3 & ":s" & LstUsedRow).Address 'This works, needs "Dim FstUsedCell As Variant"
.Range("BB340") = FstUsedCell '= "$S$10:$S$337"
End With
'---------------------------------------
'For testing purposes only
Debug.Print firstrow '= 7
Debug.Print lastrow '= 342
Debug.Print Range("s" & firstrow + 3 & ":s" & lastrow - 1).Address '= $S$10:$S$341
Debug.Print LstUsedRow '=337
Debug.Print FstUusedCell '= $S$10:$S$337 (is the firstrow + 3 (7+3) to ACTUAL LstUsedRow row of Col S
End Sub