VBA to Auto Resize Excel Row Heights

MrMaker

Board Regular
Joined
Jun 7, 2018
Messages
53
Office Version
  1. 365
Platform
  1. Windows
Afternoon all,

Hopefully a very quick one, I have some working code which (when run) auto resizes the row heights on 4 named sheets (Sheet1/2/3/4) to '45'.

Is there a way that I can resize sheet 4' to a different height to the others?

Thanks

Private Sub Workbook_Open()
Dim Ws As Worksheet
Dim Crit As String
Application.ScreenUpdating = False
Crit = Sheets("Selector").Range("A1").Value
For Each Ws In Worksheets
Select Case Ws.Name
Case "Sheet1", "Sheet2", "Sheet3", "Sheet4"
Ws.UsedRange.AutoFilter 1, "<>" & Crit
Ws.AutoFilter.Range.Offset(1).EntireRow.Delete
Ws.AutoFilterMode = False
Ws.Range("A2:A" & ws.Rows.Count).RowHeight = 45
End Select
Next Ws
With Sheets("Selector")
.Range("A1").Value = Crit
.Rows.RowHeight = 45
.Visible = xlHidden
End With
End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Can you please report your code using the VBA code tags, rather than putting it in a table.
 
Upvote 0
You should not put the code in a table, just copy it from the VB Editor & paste it to the board between the code tags.
That way it makes it easier for any potential helper.
 
Upvote 0
To have sheet 4 use a different row height, you can just add another case to the select case statement for sheet4 & remove sheet4 from your existing case.
 
Upvote 0
Ah ok, let's try this:
I inherited the code so I'm not too clear how I'd do that, any ideas?
Thanks in advance

VBA Code:
Private Sub Workbook_Open()
   Dim Ws As Worksheet
   Dim Crit As String
   
   Application.ScreenUpdating = False
   Crit = Sheets("Selector").Range("A1").Value
   For Each Ws In Worksheets
      Select Case Ws.Name
         Case "Sheet1", "Sheet2", "Sheet3", "Sheet4"
            Ws.UsedRange.AutoFilter 1, "<>" & Crit
            Ws.AutoFilter.Range.Offset(1).EntireRow.Delete
            Ws.AutoFilterMode = False
            Ws.Range("A2:A" & Ws.Rows.Count).RowHeight = 45
      End Select
   Next Ws
   With Sheets("Selector")
      .Range("A1").Value = Crit
      .Rows.RowHeight = 45
      .Visible = xlHidden
   End With
End Sub
 
Upvote 0
Thanks for that, it's a lot better.
How about
VBA Code:
Private Sub Workbook_Open()
   Dim Ws As Worksheet
   Dim Crit As String
   
   Application.ScreenUpdating = False
   Crit = Sheets("Selector").Range("A1").Value
   For Each Ws In Worksheets
      Select Case Ws.Name
         Case "Sheet1", "Sheet2", "Sheet3"
            Ws.UsedRange.AutoFilter 1, "<>" & Crit
            Ws.AutoFilter.Range.Offset(1).EntireRow.Delete
            Ws.AutoFilterMode = False
            Ws.Range("A2:A" & Ws.Rows.Count).RowHeight = 45
         Case "Sheet4"
            Ws.UsedRange.AutoFilter 1, "<>" & Crit
            Ws.AutoFilter.Range.Offset(1).EntireRow.Delete
            Ws.AutoFilterMode = False
            Ws.Range("A2:A" & Ws.Rows.Count).RowHeight = 20
      End Select
   Next Ws
   With Sheets("Selector")
      .Range("A1").Value = Crit
      .Rows.RowHeight = 45
      .Visible = xlHidden
   End With
End Sub
Just change the row height for sheet 4 from 20 to what ever it needs to be.
 
Upvote 0
Works brilliantly, thank you very much!

I'll try and remember the code thing for next time ?
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,951
Messages
6,122,442
Members
449,083
Latest member
Ava19

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