ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Need help with Procedure to return selected column numbers (https://www.excelbanter.com/excel-programming/427115-re-need-help-procedure-return-selected-column-numbers.html)

JLGWhiz[_2_]

Need help with Procedure to return selected column numbers
 
I don't believe you will have much luck with that. Here is an excerpt from
the VBA help file:

When applied to a Range object that's a multiple-area selection, this
property returns columns from only the first area of the range. For example,
if the Range object has two areas- A1:B2 and C3:D4- Selection.Columns.Count
returns 2, not 4. To use this property on a range that may contain a
multiple-area selection, test Areas.Count to determine whether the range
contains more than one area. If it does, loop over each area in the range.

You would have to use Columns.Count for the selected range, then add that
result -1 to the starting column number to get the column range. i.e. If
you select D4:J4, then Selection.Column.Count would yield a count of 7. You
know you are in coluimn D or Columns(4), so if you add 7 - 1 or 6, it gives
you 10, therefor, your range is columns 4:10.





"Dan Thompson" wrote in message
...
I am having trouble with this macro

Sub ReturnColumnNumbers()
Dim ColNum As Integer
ColNum = Selection.Column
MsgBox ("The Selected Column Numbers are" & Chr(13) & ColNum)
End Sub

The problem is it works when I select just one column but if I select more
than one column it does not return the other column numbers ?

Any thoughts ?

Dan Thompson





All times are GMT +1. The time now is 12:03 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com