I want to extract the row number of a range and add +1
to it in VBA
.
Therefore, I tried to go with this VBA
:
Sub Extract_Row ()
Sheet1.Range("A1").Value = Split(Sheet1.Range("L28").Address, "$")(2) + 1
Sheet1.Range("A2").Value = Split(Sheet1.Range("L28:AE28").Address, "$")(2) + 1
End Sub
It works for the first option where I only select one cell L28
.
For the second option where I select a range L28:AE28
I get runtime error 13
.
What do I need to change in the second option to make it work as well?
Use UBound()
to extract last item of array. Try below sub...
Sub Extract_Row()
Sheet1.Range("A2").Value = Split(Sheet1.Range("L28:AE28").Address, "$")(UBound(Split(Sheet1.Range("L28:AE28").Address, "$"))) + 1
End Sub
As per my comment, no need to Split()
, use:
Sheet1.Range("A1").Value = Sheet1.Range("L28:AE28").Row + 1
Where .Row
would return the row index of the top left cell in a Range
object.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.