Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Excel - Selecting columns by index

Posted on 2003-10-28
9
Medium Priority
?
205 Views
Last Modified: 2010-05-01
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?
0
Comment
Question by:MarkNethercott
  • 5
  • 4
9 Comments
 
LVL 29

Expert Comment

by:leonstryker
ID: 9636476


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

Author Comment

by:MarkNethercott
ID: 9636517
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?
0
 
LVL 29

Accepted Solution

by:
leonstryker earned 450 total points
ID: 9636565
For this type of an operation it is the quickest way.

You can also select a range in those columns and do EntireColumn.AutoFit

Sub AutoFitCol()
Dim i as integer, j as Integer
    i = 1
    j = 10
    Range(Cells(1, i), Cells(1, j)).EntireColumn.AutoFit
End Sub
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 29

Expert Comment

by:leonstryker
ID: 9636973
Why B?

:(
0
 

Author Comment

by:MarkNethercott
ID: 9637279
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?
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 9637505
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
0
 

Author Comment

by:MarkNethercott
ID: 9637597
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.
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 9637859
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
0
 

Author Comment

by:MarkNethercott
ID: 9640569
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
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question