Compare length of two rows

L

Legacy 108787

Guest
In a large sheet I have several pivottables, using a dynamic range of sourcedata. I want one empty column between two tables.

Due to the dynamic range and some conditions for (self genererated) tabletitles, the cell for the next table to start can be different every time around. I therefore have to check which of two rows is longest, to have the next table exactly two cells further, leaving one blank column.

I have the following code:
Code:
Sub Last_Cell()
    Dim One As Range         'title (string) row
    Dim Two As Range         'content of table row
    Dim Fin As Range
    Set One = Range("IV5").End(xlToLeft).Offset(0, 2)
    Set Two = Range("IV11").End(xlToLeft).Offset(0, 2)
    If One > Two Then
        Set Fin = One
    Else
        Set Fin = Two
    End If
    Fin.Select
End Sub
This obviously doesn't work as One and Two are non-comparable, Row-wise.
I tried adding .Row, or [R], Address... but I'm stuck. I can't find anthing on here nor on Google.
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Code:
Sub Last_Cell()
    Dim One As Range
    Dim Two As Range
    Dim Fin As Range
    Set One = Range("A5", Range("IV5").End(xlToLeft))
    Set Two = Range("A11", Range("IV11").End(xlToLeft))
    If One.Columns.Count > Two.Columns.Count Then
        Set Fin = Range("IV5").End(xlToLeft).Offset(0, 2)
    Else
        Set Fin = Range("IV11").End(xlToLeft).Offset(0, 2)
    End If
    Fin.Select
End Sub

This does the trick!
But is seems overcomplicated to me, comparing ranges instead of two single cells.
Alas, I have my code now. Thanks for the help, Yard!
 
Upvote 0
Looking back, I think you actually want the number of the rightmost column, not to count the rows!

Code:
One.Column
 
Upvote 0
Looking back, I think you actually want the number of the rightmost column, not to count the rows!

Code:
One.Column
That's it! :)
Thanks again!


Though: quicky question:
If I keep this as one Sub, how can I call this Sub from another so that the Dim values are valid in the last Sub?
 
Last edited by a moderator:
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