Link to home
Start Free TrialLog in
Avatar of MarkNethercott
MarkNethercott

asked on

Excel - Selecting columns by index

I would like to select the 'i'th to 'j'th column in a worksheet to autofit the contents.

How do you create a range that selects all the rows in columns i to j?

I know that if i = 1 and j =6, I could use the statement ...columns("A:F").AutoFit to get the result, but I don't knopw how to get from the numerical i & j reference to a A1 type reference?
Avatar of leonstryker
leonstryker
Flag of United States of America image



Sub AutoFitCol()
Dim I As Integer
    For I = 1 To 10
        Columns(I).AutoFit
    Next
End Sub
Avatar of MarkNethercott
MarkNethercott

ASKER

Ok, I see - do it one columns at a time.  Should I conclude that there isn't a simple way of selecting a range of columns?
ASKER CERTIFIED SOLUTION
Avatar of leonstryker
leonstryker
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Why B?

:(
It was the EntireColumn property that I hadn't spotted/understood and it seemed to be a the most/more elegant way to code the result - one line that's easy to read rather than a 3 line loop that's a (bit) harder to interpret.  

Is the first answer more efficient from a processing point of view?

Is there something else I've not understood?
It depends on how you look at it.  The first answer manipulates the column object directly and can be easier to use if the columns are not continuous, but uses a loop which is somewhat slower.

The second solution is faster, but is more difficult to read by relying on the Range object.

Leon
Ok, thanks.

I must apologise for being a bit slow (it's 10:30pm here)

In your original question I thought you were asking why I'd accepted the 'B' (or second) answer, but you really wanted to know why I'd only rated your answer as 'B' = Good !

On the one hand, I did have a reason, but - on reflection - it's rather long winded and would seem picky if I wrote it all out. In essence, it's always hard/subjective to grade an answer.  I never really know whether an  answer should be rated in the context that it's given (easy or hard question) or in relation to other good or excellent answers.  For example in some instancesa 'yes' or 'no' answers the question, but is this a good or excellent answer. I suppose I'd argue that it is only good because the answer is missing some extra qualification or example.  And how do you rate the one word answer against other answers that have taken a long time to prepare?

I suppose in this case, it took 2 goes to get to the answer that I was looking for  - which was great (and I appreciate the assistance),  but (to me) an excellent answer would have had the two alternatives in the first answer.

I apologise if my reasoning seems flawed.  Thanks again for your help.
The grading here at EE is not like that in school. A does not corespond to Excelent, B to good, and C to acceptable but rather:

A: The Expert(s) either provided you with a thorough answer or they provided you with a link to information that thoroughly answered your question. An "A" can also be given to any answer that you found informative or enlightening beyond the direct question that you asked.

B: The Expert(s) provided an acceptable solution, or a link to an acceptable solution, that you were able to use, although you may have needed a bit more information to complete the task.

For further information please look at http://beta.experts-exchange.com/help/closing.jsp
Thanks for the information and the link, I'll use these from now on - though in my defense, this isn't the impression that the EE acceptance form gives.  It says;

A - Excellent
B - Good