繁体   English   中英

VBA - 打印多个范围

[英]VBA - Printing multiple ranges

下面给出了一个在线找到的 VBA 代码,它有助于从 Excel 打印所需的范围,但不确定如何改进它以满足以下条件。 执行时,它会将多个选择打印到一页中,但不适合它们。 复制第一个选择后,第二个进入该页面的后续行,依此类推。 例如,如果随机选择两个包含 10 个值的列(A 和 C)作为要打印的所需范围,则第二个选定列 (C) 将位于第一列 (A) 下方。 是否可以修改它以便将第二个(和下一个选择)定位到第一个选择/列右侧的新列中?

'VBA Code
Sub printOutRange()
Dim xRng1 As Range
Dim xRng2 As Range
Dim xNewWs As Worksheet
Dim xWs As Worksheet
Dim xIndex As Long
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set xWs = ActiveSheet
Set xNewWs = Worksheets.Add
xWs.Select
xIndex = 1
For Each xRng2 In Selection.Areas
    xRng2.Copy
    Set xRng1 = xNewWs.Cells(xIndex, 1)
    xRng1.PasteSpecial xlPasteValues
    xRng1.PasteSpecial xlPasteFormats
    xIndex = xIndex + xRng2.Rows.Count
Next
xNewWs.Columns.AutoFit
xNewWs.PrintPreview
xNewWs.Delete
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

这应该这样做:

Dim xRng1 As Range
Dim xRng2 As Range
Dim xNewWs As Worksheet
Dim xWs As Worksheet
Dim xIndex As Long
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set xWs = ActiveSheet
Set xNewWs = Worksheets.Add
xWs.Select
xIndex = 1
For Each xRng2 In Selection.Areas
    xRng2.Copy
    Set xRng1 = xNewWs.Cells(1, xIndex)
    xRng1.PasteSpecial xlPasteValues
    xRng1.PasteSpecial xlPasteFormats
    xIndex = xIndex + 1
Next
xNewWs.Columns.AutoFit
xNewWs.PrintPreview
xNewWs.Delete
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM