Solved

Excel - Selecting columns by index

Posted on 2003-10-28
9
202 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

751 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