Solved

Excel - Selecting columns by index

Posted on 2003-10-28
9
197 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 150 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
 
LVL 29

Expert Comment

by:leonstryker
ID: 9636973
Why B?

:(
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now