Gliori

New Member
Joined
Jun 15, 2015
Messages
32
Hello, as the title states I want to run a macro through all worksheets in a workbook.

Here is my code:
Code:
Sub test1()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
Columns("M:M").ColumnWidth = 11.5
Next ws
End Sub

But it does only apply to the active worksheet. Can anyone see what's wrong with it?

Thank you in advance!

Cheers!
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Try This:
Code:
Sub test1()
Dim i As Integer
For i = 1 To Sheets.Count
Sheets(i).Columns("M:M").ColumnWidth = 11.5
Next
End Sub
 
Last edited:
Upvote 0
You could just activate the worksheet

Sub test1()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ws.activate
Columns("M:M").ColumnWidth = 11.5
Next ws
End Sub
 
Upvote 0
Gliori,

Here is another macro solution for you to consider, that will do what you have requested without activating each worksheet.

Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Sub test1_V2()
' hiker95, 07/01/2015, ME865189
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
  ws.Columns("M:M").ColumnWidth = 11.5
Next ws
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the test1_V2 macro.
 
Upvote 0
Alright! Thank you very much Trevor and hiker for taking your time and helping me. I'm not at the office right now but I'll try the codes as soon as I get there and get back to you with the results.

Kind regards!
 
Upvote 0
Gliori,

Thanks for the feedback.

You are very welcome. Glad we could help.

And, come back anytime.
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,746
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top