Sunday, November 18, 2018

excel - When I'm selecting a range how do I refer to the first selected cell of it and the last selected cell of it on VBA?

This would seem to be an ideal time when Selection can provide the information you require.



sub FirstLastFull()

with selection

debug.print "first cell is: " & .cells(1, 1).address(0,0)
debug.print "last cell is: " & .cells(.rows.count, .columns.count).address(0,0)
debug.print "full range is: " & .address(0,0)


end with

end sub


The above will work on a contiguous range of selected cells. If you want the same information from a non-contiguous range, the Areas property must be considered.



sub FirstLastFull()

with selection


debug.print "first cell is: " & .cells(1, 1).address(0,0)
with .areas(.areas.count)
debug.print "last cell is: " & .cells(.rows.count, .columns.count).address(0,0)
end with
debug.print "full range is: " & .address(0,0)

end with

end sub

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...