1

I have a worksheet which contains a data matrix like the sample below:

example

This array is dynamically generated, so..I know the coordinates of the top left (non empty) cell, I want to find the coordinates of the top right (non empty) Cell

The first solution is to use the .offset and check each cell to the right if it's not null but I find this way bad. I would like find a better way, more optimized.

Does it exist?

2 Answers 2

2

In your case you can use CurrentRegion property:

Sub test()
    With Range("B5").CurrentRegion            
        MsgBox .Address 'address of entire matrix            
        MsgBox .Cells(.Rows.Count, .Columns.Count).Address 'bottom right cell
    End With
End Sub
Sign up to request clarification or add additional context in comments.

2 Comments

Really thx Simoco and Gary =) Last question, is there a "custom .currentRegion" that allows to define the region type ? I mean by example, if my array was filled with Integers and around it there was String cell. Is there a way that I only select the "Integer region" ?
No, unfortunatelly there is not. There should be some additional logic in code (e.g. loop through values of currentRegion in one row to determine in what column string values starts and ends)
1

If the data is as you posted (an isolated block starting at B5). Then:

Sub dural()
    Dim r As Range
    Set r = Range("B5").CurrentRegion
    nLastColumn = r.Columns.Count + r.Column - 1
End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.