Solved

Excel - Increse columns in range by 5

Posted on 2016-11-21
6
63 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
[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
  • 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 52

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

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

628 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