Setting cell height & widths to match master sheet

claudehollett

Board Regular
Joined
Dec 11, 2003
Messages
89
The following code should loop through each worksheet and (1) set colmn width in col 1-52 to match master sheet, (2) set each row heigh in row 1-3 to match master sheet, and (3) set margins to match master. (1) & (3) work fine but (2) works only on the active sheet. Can someone tell me why and how to fix it. Thanks, Claude.

Sub SetToMaster()

'This routine adjusts all Worksheets in ThisWorkbook to match Master Sheet
Dim Master As Worksheet
Set Master = ThisWorkbook.Worksheets("Master")
Dim ws As Worksheet
Application.ScreenUpdating = False

For Each ws In ThisWorkbook.Worksheets

If ws.Name <> "Master" Then
For c = 1 To 52
ActiveSheet.Columns(c).ColumnWidth = Master.Columns(c).ColumnWidth
Next

For r = 1 To 3
ActiveSheet.Rows(r).RowHeight = Master.Rows(r).RowHeight
Next

With ws.PageSetup
.LeftMargin = Master.PageSetup.LeftMargin
.RightMargin = Master.PageSetup.RightMargin
.TopMargin = Master.PageSetup.TopMargin
.BottomMargin = Master.PageSetup.BottomMargin
End With

End If
Next
Application.ScreenUpdating = True
End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi Claude

I cannot understand how (1) works(?). In both (1) and (2) you are always referring to the ActiveSheet:

Code:
ActiveSheet.Columns(c).ColumnWidth = Master.Columns(c).ColumnWidth 

...

ActiveSheet.Rows(r).RowHeight = Master.Rows(r).RowHeight

You must refer to the worksheet loop variable ws:

Code:
ws.Columns(c).ColumnWidth = Master.Columns(c).ColumnWidth 

...

ws.Rows(r).RowHeight = Master.Rows(r).RowHeight

Please try it.

Hope this helps
PGC
 
Upvote 0

Forum statistics

Threads
1,214,661
Messages
6,120,791
Members
448,994
Latest member
rohitsomani

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