0

All When I write the following excel VBA code, macro applies the columns width 10.86 to the entire sheet, and ignore the terms related to columns (A, B, C)., Could any one help?, thanks in advance

Columns("A:A").Select
Selection.ColumnWidth = 17.86

Columns("B:C").Select
Selection.ColumnWidth = 19.86

Columns("D:I").Select
Selection.ColumnWidth = 10.86
Amr Gaber
  • 11
  • 2
  • 2
    I can't reproduce this. What is on the sheet? Note that you don't need to `Select`: `Columns("A:A").ColumnWidth = 17.86`, and so on. – BigBen Jan 13 '20 at 14:10
  • As Ben stated it might be a selection issue and direct references might solve it. Also see if you don't have any Worksheet_Change events running at the same time which might mess with your selection. – Plutian Jan 13 '20 at 14:12

2 Answers2

0

unless there is some other code that changes the selection, this should not happen
try this code, it sets the width in the active sheet

Dim ws As Worksheet 
Set ws = ActiveSheet
With ws
    .Columns("A:A").ColumnWidth = 17.86
    .Columns("B:C").ColumnWidth = 19.86
    .Columns("D:I").ColumnWidth = 10.86
End With
Stachu
  • 1,614
  • 1
  • 5
  • 17
0

If you get rid of the Select statements it works properly. The below link is a good discussion about avoiding Select statements in VBA.

How to avoid using Select in Excel VBA

Columns("A:A").ColumnWidth = 17.86

Columns("B:C").ColumnWidth = 19.86

Columns("D:I").ColumnWidth = 10.86
roses56
  • 130
  • 9