Columns by numbers

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I cannot work out how to refer to a group of columns by numbers.
this doesn't work or any varaition using Columns intead of Range:

Dim startCol as Byte, endCol as Byte

Range(Columns(startCol), Columns(startCol + 1), Columns(endCol)).AutoFit

Listing each column separately works but how can I group them on 1 line?

Columns(startCol).AutoFit
Columns(startCol +1).AutoFit
Columns(endCol).AutoFit

Could someone please help?

Geoff
 
How about using a function to convert the number to a letter? For example,
here's a function I used when working with the first 26 (A-Z) columns:
-----
Function ConvertValue2Column(iValue As Integer) As String
'Takes a number and converts it into a letter to be used for cell column
calcs.

Dim Result As String, ASCIIVal As Integer

ASCIIVal = 64 'Asc("@")
If (ASCIIVal + iValue) > 90 Then
MsgBox "Table too long!", vbExclamation, "Error!"
Exit Function
End If
Result = Chr(ASCIIVal + iValue)
ConvertValue2Column = Result
End Function
-----

So, using the above function:
Range(Columns(ConvertValue2Column(startCol)),
Columns(ConvertValue2Column(startCol) + 1),
Columns(ConvertValue2Column(endCol))).AutoFit
 
Hi Toby
Yes, I have used a similar algorthm to convert letters to numbers. Just
seems a shame that a requirement so simple should require other code. If my
columns were contiguous then this would suffice:

Range(Columns(startCol), Columns(endCol)).AutoFit

.... but they need not be contiguous.

Seems like I need the sledgehammer after all <g>

Thanks

Geoff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top