Function to alter row height automatically

krissz

Board Regular
Joined
Feb 21, 2010
Messages
95
I regularly have to manually review large volumes of excel data, one column being the date by which the data is ordered. For visual convenience, every time the month changes (e.g. from August to September), I increase the row height so as to visually group the data. The data is provided by SQL queries.
.
Is there any way I can automate the process, namely:
1. Import the data - done
2. Sort the data by date - Done
3. Increase the row height when the month changes - ?????
.
I have googled for ideas & made several attempts; no luck so far
I am running Excel 2010
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
In your macro to sort the data, you could add a line:
Code:
UsedRange.Rows.Autofit

But there is a maximum of 409 points in row height. Appx 90 percent of the vertical space.
 
Last edited:
Upvote 0
How about this? Put this in This Workbook, in the Workbook_Open sub:
Code:
Private Sub Workbook_Open()
Dim Lastrow As Integer
Dim i As Integer
Dim Thismonth As Integer


Dim x As Date
    Lastrow = ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count).Row
Rows("1:" & Lastrow).RowHeight = 12
'//resets row hts
Thismonth = Month(Date) '//MsgBox Thismonth


For i = 2 To Lastrow
    Range("A" & i).Select
        With Selection
            If Month(Selection) = Thismonth Then
                .RowHeight = 25
                .EntireRow.Font.Bold = True '//OPTIONAL. This makes matching rows have bold font as well
'/////////(bold font also allows user to filter based on that factor, too, which they can't do on row ht alone)
            End If
        End With
Next i


End Sub
 
Upvote 0
Thank you. Based on your suggestion, my final solution is:

Code:
Option Explicit

Private Sub Worksheet_Activate()
    
Dim LastRow As Integer
Dim FirstRow As Integer
Dim i As Integer
Dim ThisMonth As Integer
   
    Application.ScreenUpdating = False
'   Find first active row
    For i = 2 To 25
        If InStr(1, ActiveSheet.Range("B" & i).Value, "Date") > 0 Then
            FirstRow = i + 1
            Exit For
        End If
    Next i
    
'   Find Last row (Cells in column B must be occupied)
    Selection.End(xlDown).Select
    LastRow = ActiveCell.Row
    
'   Set default height
    Rows(FirstRow & ":" & LastRow).RowHeight = 14

'   Increase row height when month changes
    ThisMonth = 0
    For i = FirstRow To LastRow
        Range("B" & i).Select
        With Selection
            If ThisMonth > 0 Then
                If Month(Selection) <> ThisMonth Then
                    .RowHeight = 25
                End If
            End If
            ThisMonth = Month(Selection)
        End With
    Next i
    Range("B" & FirstRow).Select
    Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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