Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop with Letters instead of numbers 1

Status
Not open for further replies.

vba317

Programmer
Mar 5, 2009
708
US
I an trying to reformat my spreadsheet. I want to write a for next loop based on letters instead of numbers is this possible? I get a compile error on my current loop.

Code:
Dim C1 As String

For C1 = "A" To "R"
 Columns("C1").Select
   With Selection
   .Columns.AutoFit
   .VerticalAlignment = xlCenter
   .HorizontalAlignment = xlRight
  End With
                                    
Next C1
 



hi.
Code:
For C1 = cells(1, "A").column To cells(1, "R").column
  with Columns(C1)
   .Columns.AutoFit
   .VerticalAlignment = xlCenter
   .HorizontalAlignment = xlRight
  End With
                                    
Next C1
or better yet...
Code:
  with Range(cells(1, "A").column To cells(1, "R")).entirecolumn
    .Columns.AutoFit
   .VerticalAlignment = xlCenter
   .HorizontalAlignment = xlRight
  End With
                                    
Next C1


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 



oops or better yet (no to)
Code:
  with Range(cells(1, "A").column, cells(1, "R")).entirecolumn
    .Columns.AutoFit
   .VerticalAlignment = xlCenter
   .HorizontalAlignment = xlRight
  End With


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Does this code eliminate the need for the for next loop? Or does this code go within the for next loop?

Tom
 
Skip,

I used the following code

Code:
 With Range(Cells(1, "A").Column, Cells(1, "R")).EntireColumn
                                    .Columns.AutoFit
                                    .VerticalAlignment = xlCenter
                                    .HorizontalAlignment = xlRight
                                     End With

I got a runtime error 1004
Method range of object global failed
 

Code:
 With [red]Range(cells(1, "A").column, cells(1, "R")).entirecolumn[/red]

This part loops from A to R, so it dosnt need a FOR/NEXT loop.

"Knowing that you know is the greatest sign of stupidity, knowing that you are ignorant is the best proof of intelligence.
 


Code:
With Range(Cells(1, "A"), Cells(1, "R")).EntireColumn
                                    .Columns.AutoFit
                                    .VerticalAlignment = xlCenter
                                    .HorizontalAlignment = xlRight
                                     End With

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Once again I thank you Skip for all your help. I am starting to see an end to my project. I thank you for your patience.

Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top