Solved

Excel - Selecting columns by index

Posted on 2003-10-28
9
199 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

831 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