0

ワークブックに印刷するシートごとに 1 回呼び出される Excel VBA 印刷関数があります。

VBA でシートを循環し、この関数を呼び出します。

Sub PrintSheet
  With ActiveSheet

    'lastRow is worked out here....

    'Autofit YTD and SLY
    .Columns("J:J").AutoFit
    .Columns("K:K").AutoFit

    'Autofit email column
    .Columns("F:F").AutoFit

    With .PageSetup
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = False
        .Orientation = xlLandscape
        .LeftMargin = Application.InchesToPoints(0.4)
        .RightMargin = Application.InchesToPoints(1)
        .TopMargin = Application.InchesToPoints(0.5)
        .BottomMargin = Application.InchesToPoints(0.5)
        .RightFooter = "&P of &N"

        .PrintArea = "$A$1:$O$" & CStr(lastRow)
    End With

    .PrintOut
 End With
End Sub

私が抱えている問題は、時々 (ランダムな動作で、常に同じシートであるとは限りません)、自動調整している列が非常に広く伸びて、他の列がページから外れることがあります。これは、印刷ルーチンを再度実行できるため、データ関連ではありません。以前は列をうまく引き延ばしていた同じシートを印刷します。最初にVBAコードで入力されるため、シート内のすべての列の値をトリミングしています

伸びる列は自動調整される列であると言ったことを除けば、この動作のパターンを見つけることができませんでした。

何かご意見は?

4

1 に答える 1

4

autofit;を使用しているときにこれを何度も経験しました。だから今はなるべく使わないようにしています。

できること:

1.セット幅を使用します。たとえば、列Jが常に47幅で問題ないことがわかっている場合は、Columns("D:D").ColumnWidth = 47
2を使用します。次のように、サブルーチンの最後にいくつかのテストを追加できます。

Sub PrintSheet()

With Excel.ActiveSheet

    'lastRow is worked out here....
    lastRow = .Cells(.Rows.Count, 1).End(Excel.xlUp).Row

    'Autofit YTD / SLY / email
    .Columns("J:J").AutoFit
    .Columns("K:K").AutoFit
    .Columns("F:F").AutoFit

    With .PageSetup
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = False
        .Orientation = Excel.xlLandscape
        .LeftMargin = Application.InchesToPoints(0.4)
        .RightMargin = Application.InchesToPoints(1)
        .TopMargin = Application.InchesToPoints(0.5)
        .BottomMargin = Application.InchesToPoints(0.5)
        .RightFooter = "&P of &N"
        .PrintArea = "$A$1:$O$" & lastRow 'CStr(lastRow) <=== & does an implicit conversion
    End With

    'add three checks here <====
    '<==I've picked 100 out of thin air but you need to change this to a value which indicates that it has behaved badly
    If .Columns("J:J").ColumnWidth > 100 Then
        .Columns("J:J").ColumnWidth = 30  '<== you might want to amend 30
    End If
    If .Columns("k:k").ColumnWidth > 100 Then
        .Columns("k:k").ColumnWidth = 30
    End If
    If .Columns("f:f").ColumnWidth > 100 Then
        .Columns("f:f").ColumnWidth = 30
    End If

    .PrintOut

End With
End Sub
于 2012-12-29T12:31:51.447 に答える