【问题标题】:Print Range User Selected用户选择的打印范围
【发布时间】:2013-01-20 15:10:49
【问题描述】:

我希望创建一个基本上可以像这样运行的模块:

  1. 定义 4 或 5 个打印范围;
  2. 提示用户输入框;
  3. 允许用户从该输入框中的下拉菜单中选择他们希望打印的范围;
  4. 选择范围后,他们点击确定,并提示“您确定吗?”框以防止误点击。

我对此很迷茫,老实说,我觉得我一直在编写的代码将比仅仅阐明问题所提供的帮助更少。

我已经让用户定义了范围(手动选择他们希望打印的列),但这不是我想要的。

更进一步,是否可以进一步自定义打印格式(横向与纵向,以及纸张类型)?

非常感谢您提前提供的帮助,我会尽力回答问题并提供我上面引用的代码示例(只是一个提示,允许您选择列。我需要它是一个定义的范围,按名称,range1=a2:c14 或类似名称,因为最终用户不是优秀的 excel 用户。

见下文:

Sub SelectPrintArea()
Dim PrintThis As Range
ActiveSheet.PageSetup.PrintArea = ""
Set PrintThis = Application.InputBox _
(Prompt:="Select the Print Range", Title:="Select", Type:=8)
PrintThis.Select
Selection.Name = "NewPrint"
ActiveSheet.PageSetup.PrintArea = "NewPrint"
ActiveSheet.PrintPreview
End Sub

作为后续:

假设文档有隐藏部分,如果这些部分是用户定义范围的一部分(比如它是分组的一部分),是否能够取消隐藏这些部分。这适用于受保护的文档吗?

【问题讨论】:

    标签: excel vba printing range


    【解决方案1】:

    为了向用户显示名称列表,您需要一个类似于此的 UserForm:

    该表单背后的代码如下所示。我使用打印预览来支持“你确定”消息,因为它是一个更优雅的用户体验。

    Option Explicit
    
    Private Sub UserForm_Initialize()
    
      With Me.cboPrintAreas
        .MatchRequired = True
        'Add named ranges to the listbox
        .AddItem "Report_1"
        .AddItem "Report_2"
        .AddItem "Report_3"
        .AddItem "Report_4"
        .AddItem "Report_5"
    
        'Set the default report
        .Value = "Report_1"
      End With
    
    End Sub
    
    Private Sub btnCancel_Click()
      Unload Me
    End Sub
    
    Private Sub btnPrint_Click()
    
        Dim rng As Range
    
        Set rng = Range(Me.cboPrintAreas.Value)
    
        With rng.Worksheet
          'Do a crude assignment of paper orientation
          If rng.Height > rng.Width Then
          .PageSetup.Orientation = xlPortrait
          Else
            .PageSetup.Orientation = xlLandscape
          End If
          .PageSetup.PrintArea = rng.Address
          Me.Hide
          .PrintOut Preview:=True, IgnorePrintAreas:=False
          Unload Me
        End With
    
    End Sub
    

    你会显示来自标准模块的表单,代码如下:

    Sub test()
    
      UserForm1.Show
    
    End Sub
    

    如果您想取消隐藏隐藏的行/列,您需要确保该范围的工作表适当地不受保护。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-11
      • 2011-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-11
      • 1970-01-01
      相关资源
      最近更新 更多