vba - 获取 Excel 工作表的实际打印区域(通过 VBA)

标签 vba excel

如何在 Excel 中获取实际使用范围(包括图表)?

问题是,用户不会定义任何打印区域。但他应该在剪贴板中获得所有使用范围(包括图表)的图片:

如果我喜欢,Sheet1.PageLayout.PrintArea它给了我一个空白字符串。

如果我喜欢,Sheet1.UsedRange.Address ,
它不考虑底部的图表。

请提出解决方法。

谢谢。

最佳答案

你不能用吗?左上角单元 右下角单元 图表对象 并检查某些图表是否在 UsedRange 之外?像这样:

Sub test()
    Dim usedRangeEx As Range
    Set usedRangeEx = GetUsedRangeIncludingCharts(Worksheets("Sheet1"))
    usedRangeEx.Activate
    Debug.Print usedRangeEx.Address
End Sub

Private Function GetUsedRangeIncludingCharts(target As Worksheet) As Range
    Dim firstRow As Long
    Dim firstColumn As Integer
    Dim lastRow As Long
    Dim lastColumn As Integer
    Dim oneChart As ChartObject

    With target
        firstRow = .UsedRange.Cells(1).Row
        firstColumn = .UsedRange.Cells(1).Column
        lastRow = .UsedRange.Cells(.UsedRange.Cells.Count).Row
        lastColumn = .UsedRange(.UsedRange.Cells.Count).Column

        For Each oneChart In .ChartObjects
            If oneChart.TopLeftCell.Row < firstRow Then _
                firstRow = oneChart.TopLeftCell.Row
            If oneChart.TopLeftCell.Column < firstColumn Then _
                firstColumn = oneChart.TopLeftCell.Column
            If oneChart.BottomRightCell.Row > lastRow Then _
                lastRow = oneChart.BottomRightCell.Row
            If oneChart.BottomRightCell.Column > lastColumn Then _
                lastColumn = oneChart.BottomRightCell.Column
        Next oneChart

        Set GetUsedRangeIncludingCharts = .Range(.Cells(firstRow, firstColumn), _
                                                 .Cells(lastRow, lastColumn))
    End With

End Function

关于vba - 获取 Excel 工作表的实际打印区域(通过 VBA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22879803/

相关文章:

excel - 仅当 A 列和 B 列符合我的条件时,如何在 C 列中返回值?

excel - 如何在执行之前调试 Excel 自动打开宏?

vba - PowerPoint 文本转语音宏

vba - 在 Access 中循环表行,使用或不使用 Private Const

python - 将 xlwings 模块导入 python 3.4

c++ - C++ Excel COM自动化-Workbook.SaveAs-找不到成员

vba - Excel VBA - 对象引用未设置为对象的实例

excel - 编辑 VBA 脚本用户窗体

vba - 使用 VBA 将 Access 查询复制到 Excel 工作表失败

vba - Excel VBA,如何在一个工作表上的下拉列表中使用选择格式(颜色)另一个工作表上的一系列单元格