Change column width

Hatye

Board Regular
Joined
Jan 11, 2010
Messages
143
Hi,
I want to change column width on the following columns in my worksheet: O, S, W, AA, AE, AI from 2 to 1.
What I have is this:

Code:
    Range("O:O,S:S,W:W,AA:AA,AE:AE,AI:AI").Select
    Selection.ColumnWidth = 1
The problem is that it is selecting all columns from B to AI. I have some merged cells whitin the range, so I guess that that has something to do about it, but I can't figure out what to do. Can anyone help me with this? :)
However, it does work when I do it manually (when recording the macro).
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Do you have a row that doesn't have any merged cells? Maybe you should be using the horizontal alignment 'Center across selection' instead of merged cells.
 

Hatye

Board Regular
Joined
Jan 11, 2010
Messages
143
Hi Andrew Poulsom and than you for the reply,

No, I don't have any whole columns that don't contain merged cells. It is unfortunately a lot of work to not use merged cells. This is a report template, which is specially adapted to different purposes. All columns width are 2, and it uses merged cells actively to get the correct layout for the report.
Now there is a report which needs to be adjusted, so that the columns mentioned in the first post must have the width 1.

Since it works when I do it manually, shouldn't it work with when I run it as a macro?
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
I asked about rows not columns. Surely you haven't merged the cells in every row of the columns whose width you want to change?
 

Hatye

Board Regular
Joined
Jan 11, 2010
Messages
143
Oh, I'm sorry, I misread; my fault!

Yes, I have several rows that doesn't conatain any merged cells. E.g. row 1, 6, 9, 40.
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
This worked for me:

Code:
Range("O1:O1,S1:S1,W1:W1,AA1:AA1,AE1:AE1,AI1:AI1").ColumnWidth = 1
 

Forum statistics

Threads
1,084,733
Messages
5,379,498
Members
401,607
Latest member
Zemexi

Some videos you may like

This Week's Hot Topics

  • VBA code giving errors and stopping Excel
    Hello Experts, I have this code being used to loop through files in a file path, and copy specific data to another sheet. It is giving me several...
  • Disable MsgBox message
    Morning, I have a userform where if i leave a ComboBox empty i see a MsgBox warning me that i must enter an invoice number. It is this MsgBox i...
  • Macro Recorder into VBA, Copy Paste Data Filled Cells
    Hi Everyone, I have a macro recorder file that takes a selection of data, copies, then pastes into a new sheet on ("A2:B2") The issue is my...
  • Number format changes while pasting into a cell
    Hi, I am trying to paste a number 180204524303 from an email to an excel cell, however, whenever i try to do so , the the paste value appears as...
  • Collating data
    Hello all. Could someone please help. I am trying to pull all column data from multiple sheets (24 I total so far) into 1 master sheet without...
  • Sum Multiple Columns Based on Multiple Criteria
    I am trying to consolidate data by summing columns G through M based on material, plant, vendor, and fiscal year being identical. The period does...
Top