打印范围用户选择的 VBA Excel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14711812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:28:59  来源:igfitidea点击:

Print Range User Selected VBA Excel

excelvbaexcel-vbaprintingrange

提问by mburke05

I was hoping to create a module that would basically operate like so:

我希望创建一个基本上可以这样操作的模块:

  1. Define 4 or 5 print ranges;
  2. Prompt a user an input box;
  3. Allow the user to select, from a drop down in that input box, the range they wish to print;
  4. After selecting the range, they hit OK, and are prompted by a "are you sure?" box to prevent mistaken clicks.
  1. 定义 4 或 5 个打印范围;
  2. 提示用户输入框;
  3. 允许用户从该输入框中的下拉列表中选择他们希望打印的范围;
  4. 选择范围后,他们点击确定,并提示“你确定吗?” 框以防止误点击。

I'm fairly lost on this and I honestly feel like the code I've been writing will be less help than just articulating the problem.

我对此相当迷茫,老实说,我觉得我一直在编写的代码不会比阐明问题更有用。

I have had it work by the user defining the range (manually selecting the columns they wish to print), but that's not what I'm looking for.

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

One step further, would it be possible to allow for the customization of the print format (landscape vs portrait, and paper type) even further?

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

Thanks so much for the help in advance, I'll do my best to answer questions and provide samples of the code I referenced above (just a prompt that allows you to select the columns. I need it to be a defined range, by name, range1=a2:c14or something like that, because the end user is not a great excel user.

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

See below:

见下文:

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

As a follow-up:

作为后续:

Assume the document has hidden sections, would it be able to unhide those sections if they are part of a user defined range (like if it was part of a grouping). Would this work on a protected document?

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

回答by ThunderFrame

In order to present a list of names to the user, you'll need a UserForm similar to this:

为了向用户显示姓名列表,您需要一个类似于以下内容的用户窗体:

enter image description here

在此处输入图片说明

The code behind that form would look like the below. I've used Print preview in favor of an "Are you sure" message, because it's a more elegant UX.

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

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

And you'd display the form from a Standard module with code like:

您可以使用以下代码显示来自标准模块的表单:

Sub test()

  UserForm1.Show

End Sub

If you want to unhide hidden rows/columns, you'll need to ensure you have the range's sheet suitably unprotected.

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