Solved

Excel - Increse columns in range by 5

Posted on 2016-11-21
6
52 Views
Last Modified: 2016-11-21
Hi Experts,
I need VBA to increase all COLUMNS in a selected range by  +3

Range("A:A,B:B,C:C,D:D,E:E,F:F,G:G,H:H,O:O,R:R,U:U").Select

I tried taking the sub for increasing rows, which works, and changed it to columns, but it does not work. It increases ALL columns in the sheet  not the desired columns.

Here is what I tried...................
Sub TEST()
' test

Dim column As Range
    For Each column In Range("A:A,B:B,C:C,D:D,E:E,F:F,G:G,H:H,O:O,R:R,U:U").Columns
        Columns.ColumnWidth = column.ColumnWidth + 5
    Next column
End Sub

Thanks so much
Chris
0
Comment
Question by:chris pike
  • 3
  • 2
6 Comments
 
LVL 33

Accepted Solution

by:
Rob Henson earned 500 total points
ID: 41896193
Try this:

Sub Increase_Column_Width()
    
For C1 = 1 To 6
    CW = Cells(1, C1).ColumnWidth
    Cells(1, C1).ColumnWidth = CW + 5
Next C1

For C2 = 15 To 21 Step 3
    CW = Cells(1, C2).ColumnWidth
    Cells(1, C2).ColumnWidth = CW + 5
Next C2

End Sub

Open in new window

0
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 41896213
Hi,

pls try
Sub TEST()


 Dim col As Range
     For Each col In Range("A:A,B:B,C:C,D:D,E:E,F:F,G:G,H:H,O:O,R:R,U:U").Columns
         col.ColumnWidth = col.ColumnWidth + 5
     Next col
 End Sub

Open in new window

regards
0
 
LVL 33

Expert Comment

by:Rob Henson
ID: 41896229
@Rgonzo - is the error in Chris's script because column is already a defined word so can't be used as a variable?

If not maybe the error is in the line:

Columns.ColumnWidth = column.ColumnWidth + 5

should be:

column.ColumnWidth = column.ColumnWidth + 5

note removal of "s" from column in first part.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Closing Comment

by:chris pike
ID: 41896262
Thanks,
That was an easy one.
0
 

Author Comment

by:chris pike
ID: 41896268
Rgonzo1971
Sorry I didn't even see your comment when I awarded Rob
I didn't refresh screen , so I didn't see any additional comments.

But thanks for helping.
0
 

Author Comment

by:chris pike
ID: 41896269
I do have a related question to this question.
Posting now
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

679 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