20180428 xlVBA automatically sets the row height of the score bar

'Automatically set row height
Sub AutoSetRowHeight()
    Dim BreakRow As Range 'Horizontal page break position
    Dim Wb As Workbook
    Dim Sht As Worksheet
    Dim SumHeight As Double 'Cumulative first page row height
    Dim AverageHeight As Double
    Dim FirstPageLastRow As Long 'The last row number of the first page
    Dim i As Long 'line number
    Set Wb = Application.ThisWorkbook
    Set Sht = Wb.ActiveSheet
    With Sht
        'Get the cell where the page break between the first page and the second page is located
        Set BreakRow = Sht.HPageBreaks(1).Location
        Debug.Print "The line number of the first page break: "; BreakRow.Row
        'Accumulate the height of all rows on the first page
        i = 1
        Do While i < BreakRow.Row
            SumHeight = SumHeight + .Rows(i).RowHeight
            i = i + 1
        Loop
        'Get the blank line number at the end of the last transcript on the first page
        i = BreakRow.Row
        Do While .Cells(i, 2).Value <> ""
            i = i - 1
        Loop
        Debug.Print "First page last transcript cutoff line number: "; i
        'Calculate the average row height
        If i <> 0 Then
            AverageHeight = SumHeight / i
        Else
            MsgBox "Division by zero error"
            Exit Sub
        End If
        'Set the row height of the used area
        .UsedRange.Rows.RowHeight = AverageHeight
    End With
    'freed
    Set Wb = Nothing
    Set Sht = Nothing
    Set BreakRow = Nothing
End Sub

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325039353&siteId=291194637