将行的行高设置为721px

时间:2016-06-14 08:57:40

标签: excel vba excel-vba

我在PPT-Look中有一张Excel表格,它动态地填充了另一张表格中的数据。行1到7和30到36的行高是固定的(ppt的页眉和页脚)。在第8行到第26行的范围内,空行时隐藏行(我在某些行中输入白色“X”以显示它们)。

enter image description here

导出时,我需要为第2行到第36行获得721px的总高度以适合我的PPT模板。

是否有可能动态调整“白色”行的行高(例如第29行),以获得定义范围(第2行到第36行)的总计721px?

1 个答案:

答案 0 :(得分:0)

你可以如下:

Option Explicit

Sub main()
    Dim extraRowHeight As Double, totalRowsHeight As Double
    Dim myRowsRange As Range
    Const maxRowHeight As Double = 409 '<--| maximum allowed row height

    totalRowsHeight = 721# '<--| set the total rows height you need for the range you are to specifiy right below
    With Worksheets("MyTableSheet") '<--| change "MyTableSheet" with your actual sheet name
        extraRowHeight = totalRowsHeight - .Rows("2:36").Height '<--| calculate the extra height needed for the wanted rows range
        With .Rows(29) '<--| consider the "reservoir" row
            If .RowHeight + extraRowHeight <= maxRowHeight Then '<--| if needed extra height leads to an allowable resulting "reservoir" row height ...
                .RowHeight = .RowHeight + extraRowHeight '<--| ... then adjust "reservoir" row height
            Else
                .RowHeight = maxRowHeight '<--| ... otherwise adjust "reservoir" row to maximum allowable height ...
                MsgBox "you still need to resize some other rows height for " & totalRowsHeight - .Rows("2:36").Height & " pts more" '<--|... and inform how much there still is to go
            End If
        End With
    End With
End Sub