20180429 xlVBA套打单据自适应列宽

Sub TestAutoAdjustColumnWidthBaseOnModel()
    Set ModelSheet = ThisWorkbook.Worksheets("单据模板")
    Set PrintSheet = ThisWorkbook.Worksheets("批量打印")
    AutoAdjustColumnWidthBaseOnModel ModelSheet, PrintSheet
End Sub
Sub AutoAdjustColumnWidthBaseOnModel(ByVal ModelSheet As Worksheet, ByVal PrintSheet As Worksheet, Optional modelCountInOnePage As Variant)
    Dim ModelRng As Range ‘模板单元格
    Dim modelColumnWidth() As Double ‘模板列宽数据
    Dim modelColumnCount As Long ‘模板行数
    Dim sumModelColumnWidth As Double ‘模板累计列宽
    Dim adjustScale As Double ‘调整比例
    ‘Dim modelCountInOnePage As Long ‘一页打印几个单据模板
    Dim BreakColumn As Long ‘垂直分页符位置
    Dim FirstPageSumColumnWidth As Double ‘累计首页列宽
    Dim ColumnsInOnePage  As Long  ‘每页打印多少行
    Dim i As Long, m As Long ‘行号

    With ModelSheet
        If Application.WorksheetFunction.Count(.Cells) > 0 Then
            ‘计数防止计算行号发生错误
            EndRow = .Cells.Find("*", .Cells(1, 1), xlValues, xlWhole, xlByRows, xlPrevious).Row + 1
            EndCol = .Cells.Find("*", .Cells(1, 1), xlValues, xlWhole, xlByColumns, xlPrevious).Column
            ‘获取单据模板单元格区域
            Set ModelRng = .Range(.Cells(1, 1), .Cells(EndRow, EndCol))
            Debug.Print ModelRng.Address
            ‘获取模板单元格行数和累计列宽
            modelColumnCount = ModelRng.Columns.Count
            ReDim modelColumnWidth(1 To modelColumnCount)
            sumModelColumnWidth = 0
            For i = 1 To modelColumnCount
                modelColumnWidth(i) = ModelRng.Columns(i).ColumnWidth
                sumModelColumnWidth = sumModelColumnWidth + ModelRng.Columns(i).ColumnWidth
            Next i
            Debug.Print sumModelColumnWidth
            ‘记录列宽
        End If
    End With
    ‘

    With PrintSheet
        Debug.Print "垂直分页符个数:"; .VPageBreaks.Count
        ‘先判断是否有垂直分页符,如果没有则退出
        If .VPageBreaks.Count > 0 Then
            ‘获取第一页与第二页分页符所在的单元格
            BreakColumn = .VPageBreaks(1).Location.Column
            Debug.Print "首页分页符所在的行号:"; BreakColumn
            ‘累计第一页所有行的高度
            i = 1
            Do While i < BreakColumn
                FirstPageSumColumnWidth = FirstPageSumColumnWidth + .Columns(i).ColumnWidth
                i = i + 1
            Loop

            ‘Stop

            Debug.Print FirstPageSumColumnWidth
            ‘获取第一页最后一个成绩单末尾的空白行行号
            If IsMissing(modelCountInOnePage) Then
                ColumnsInOnePage = BreakColumn
                Do While Application.WorksheetFunction.Count(.Columns(ColumnsInOnePage)) > 0
                    ColumnsInOnePage = ColumnsInOnePage - 1
                Loop
                Debug.Print "首页最后一个成绩单截止行号1:"; ColumnsInOnePage
                ColumnsInOnePage = Application.WorksheetFunction.Max(BreakColumn, modelColumnCount)
                Debug.Print "首页最后一个成绩单截止行号2:"; ColumnsInOnePage
                modelCountInOnePage = ColumnsInOnePage / modelColumnCount
                Debug.Print "每一页放置多少个单据:"; modelCountInOnePage
            End If

            ‘计算调整比例
            adjustScale = FirstPageSumColumnWidth / (sumModelColumnWidth * modelCountInOnePage)
            Debug.Print adjustScale

            ‘调整
            EndCol = .Cells.Find("*", .Cells(1, 1), xlValues, xlWhole, xlByColumns, xlPrevious).Column

            m = 0
            For i = 1 To EndCol
                m = m + 1
                .Columns(i).ColumnWidth = modelColumnWidth(m) * adjustScale
                If m = modelColumnCount Then m = 0 ‘逐个单据调整
            Next i

        End If
    End With

End Sub

  

原文地址:https://www.cnblogs.com/nextseven/p/8970698.html

时间: 2024-10-13 15:41:45

20180429 xlVBA套打单据自适应列宽的相关文章

设置tablewidget自适应列宽和设置自动等宽

  在网上很容易知道自适应列宽,100%不留空显示,这里还是提下: /*设置表格是否充满,即行末不留空*/ ui->tableWidget->horizontalHeader()->setStretchLastSection(true);   设置了100%不留空后的效果:    在QT5之前,只要通过下面的程序就可以实现自动等宽: /*QT5版本之前设置自动等宽*/ ui->tableWidget->horizontalHeader()->setResizeMode(

devpress grid表格自适应列宽的问题

1 /// <summary> 2 /// 自适应列宽,显示横向滚轴,只有当所有列都已经在界面上加载完成之后才能生效 3 /// </summary> 4 public void setAutoCloumnWidth() 5 { 6 //自适应列宽 7 this.gridView.BestFitColumns(); 8 this.gridView.HorzScrollVisibility = DevExpress.XtraGrid.Views.Base.ScrollVisibili

Asp.net 设置GridView自适应列宽不变形

动态绑定的GridView由于列数不固定,而列又太多,这样设置GridView固定宽度就不能满足需求了.为此整理了两种方法来达到GridView自适应列宽不变形的效果. //在GridView的行数据绑定完的事件中设置 protected void gvObjectList_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow || e.Row.R

Dev gridView中设置自适应列宽和日期显示格式、金额的显示格式

在Dev GridView控件中,数据库中表数据日期都是长日期格式(yyyy-MM-dd HH:mm:ss),但显示在控件变成短日期格式(yyyy-MM-dd),金额显示要显示精确的数值, 比如80.00,90.15等,但在 dev gridView中只是显示80,90 1.解决日期显示问题的代码: 设置日期的displayformat,editFormat,EditMask /// <summary>        /// 设置时间显示格式        /// </summary&g

[转载]UltraWinGrid自适应列宽/行高

说几点: DisplayLayout.AutoFitStyle 用这个属性起不到我要的效果 以下的代码可以起到效果 [c-sharp] view plaincopyprint? foreach (UltraGridColumn ugc in grid.DisplayLayout.Bands[0].Columns) ugc.PerformAutoResize(); 更好的一种写法: [c-sharp] view plaincopyprint? grid.DisplayLayout.Bands[0]

XtraReport交叉表自适应行高及最佳列宽

1.自适应行头的行高,绑定CustomRowHeight事件,代码如下: private Graphics gr = Graphics.FromHwnd(IntPtr.Zero); private void pivotGrid1_CustomRowHeight(object sender, HuanSi.XtraReports.UI.PivotGrid.PivotCustomRowHeightEventArgs e) { e.RowHeight = 20; var grid=sender as

C# dataGridView根据数据调整列宽

1 //自适应列宽 2 3 this.dgvBaoming.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.AllCells; //平铺均分列宽 this.dgvBaoming.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fills;

XtraReport交叉表自适应行高及最佳列宽(转)

1.自适应行头的行高,绑定CustomRowHeight事件,代码如下: private Graphics gr = Graphics.FromHwnd(IntPtr.Zero); private void pivotGrid1_CustomRowHeight(object sender, HuanSi.XtraReports.UI.PivotGrid.PivotCustomRowHeightEventArgs e) { e.RowHeight = 20; var grid=sender as

TListView 列宽自适应大小

看图说话,直接看效果. 设置前: 设置后: 我使用的设置: listview1.Columns[1].Width:=-2; //Columns[1]设置SubItems 扩展: a. ListView.Column[0].Width := -1;//列宽根据列内容自适应,此时保证列内容都可见. b. ListView.Column[0].Width := -2;//列宽根据列标题自适应,此时保证列标题可见. 实践是检验真理的唯一标准..Let's do it together! 版权声明:本文为