VBA to change row height to last row with data

Shadkng

Active Member
Joined
Oct 11, 2018
Messages
365
I would like some help to modify the code below to adjust row height and font size only down to the last row with data. Currently it adjusts the rows for the entire sheet which doesn't seem necessary. This code also hides both rows and columns based on various criteria. Is the code efficient the way it's written? Thanks

Sub CLUTCH_BUILD_HIDE_ALL()
Dim xRg As Long
Application.ScreenUpdating = False
With Sheets("CLUTCH BUILD")
ActiveSheet.UsedRange.Font.Size = 16
ActiveSheet.UsedRange.RowHeight = 30
Cells.EntireColumn.AutoFit
For i = 1 To ActiveSheet.UsedRange.Columns.Count
Columns(i).ColumnWidth = Columns(i).ColumnWidth + 3
Next i
.Rows("11:20").Hidden = False
For xRg = 11 To 20
.Rows(xRg).Hidden = .Cells(xRg, 2) = "" Or .Cells(xRg, 2) = "-"
Next xRg
End With
Dim c As Range
For Each c In Range("A24:N24").Cells
If c.Value = "HIDE" Then
c.EntireColumn.Hidden = True
End If
Next c
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Try this.
Code:
Sub CLUTCH_BUILD_HIDE_ALL()
Dim c As Range
Dim xRg As Long

    Application.ScreenUpdating = False

    With Sheets("CLUTCH BUILD")
        .UsedRange.Font.Size = 16
        .UsedRange.RowHeight = 30
        .Cells.EntireColumn.AutoFit
        
        For i = 1 To .UsedRange.Columns.Count
            .Columns(i).ColumnWidth = .Columns(i).ColumnWidth + 3
        Next i
        
        .Rows("11:20").Hidden = False
        
        For xRg = 11 To 20
            .Rows(xRg).Hidden = .Cells(xRg, 2) = "" Or .Cells(xRg, 2) = "-"
        Next xRg

        For Each c In .Range("A24:N24").Cells
            If c.Value = "HIDE" Then
                c.EntireColumn.Hidden = True
            End If
        Next c

    End With
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
It works but doesn't seem to do anything differently. What about changing row height and font size for just the rows that have data? Is it necessary to do that?
 
Upvote 0
What do you mean by 'works'?

Is it doing what you want it to?
 
Upvote 0
Yes it is and I see you cleaned up the code a bit.

My other request was to have the row height and font only change for rows that have data. Now it changes them for the entire sheet. Thanks
 
Upvote 0
This should change the height and font of only the range with data.
Code:
    With Sheets("CLUTCH BUILD")
        .UsedRange.Font.Size = 16
        .UsedRange.RowHeight = 30
Mind you, UsedRange can sometimes be unreliable especially if you've done a lot of formatting/copying/pasting etc.

See if this is any better.
Code:
Sub CLUTCH_BUILD_HIDE_ALL()
Dim c As Range
Dim xRg As Long
Dim I As Long

    Application.ScreenUpdating = False

    With Sheets("CLUTCH BUILD")
        .Range("A1").CurrentRegion.Font.Size = 16
        .Range("A1").CurrentRegion.RowHeight = 30
        .Cells.EntireColumn.AutoFit
        
        For i = 1 To .Range("A1").CurrentRegion.Columns.Count
            .Columns(i).ColumnWidth = .Columns(i).ColumnWidth + 3
        Next i
        
        .Rows("11:20").Hidden = False
        
        For xRg = 11 To 20
            .Rows(xRg).Hidden = .Cells(xRg, 2) = "" Or .Cells(xRg, 2) = "-"
        Next xRg

        For Each c In .Range("A24:N24").Cells
            If c.Value = "HIDE" Then
                c.EntireColumn.Hidden = True
            End If
        Next c

    End With
    
    Application.ScreenUpdating = True
    
End Sub
 
Last edited:
Upvote 0
That didn't work either. Actually the rows and fonts didn't change at all. Isn't there a code to simulate cont/shift down arrow which hi-lights rows down until a blank row?
 
Upvote 0
Where does the data on 'CLUTCH BUILD' start?
 
Upvote 0

Forum statistics

Threads
1,216,081
Messages
6,128,695
Members
449,464
Latest member
againofsoul

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