Sunday, 18 November 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...