excel - 从用户输入设置打印区域的宏
问题描述
这是我现在正在使用的代码。
Sub PrintArea()
Dim arange
Rows = InputBox("How many Rows do you want to print (Between 1 and 48)?", "Row Selection")
.PageSetup.PrintArea = .Range(A, B, C).Rows
End Sub
当我尝试运行宏时,我得到“编译错误:无效或不合格的引用”,它突出显示了我的代码中的 .Range。
我希望打印区域始终为 AC 列,但行将在 2-48 之间变化,具体取决于我要打印的内容。
编辑:单步执行代码,它停在这一行,
ActiveSheet.PageSetup.PrintArea = .Range(A:C, "arange")
并给我一个语法错误。
解决方案
它应该是这样的:
Option Explicit
Sub SetMyPrintArea()
Dim UserInput As Variant
Do
UserInput = Application.InputBox(Prompt:="How many Rows do you want to print (Between 1 and 48)?", Title:="Row Selection", Type:=1)
If VarType(UserInput) = vbBoolean And UserInput = False Then Exit Sub
DoEvents
Loop While UserInput < 2 Or UserInput > 48
Worksheets("Sheet1").PageSetup.PrintArea = "A1:C" & UserInput
End Sub
请注意,
Do … Loop While UserInput < 2 Or UserInput > 48
强制输入框再次出现,直到用户输入介于 2 … 48 之间的值。这
If VarType(UserInput) = vbBoolean And UserInput = False Then Exit Sub
是为了检查用户是否按下了取消按钮然后中止。不要命名您的过程
PrintArea
,因为 Excel 已经使用了它PageSetup.PrintArea
并且很容易混淆。
推荐阅读
- android - 在android中使用同步()
- python - 具有自定义字段的 AbstractUser 模型显示 myapp.class.None
- sql - 像我五岁一样解释:SQL(关系)和 NOSQL(非关系)数据库
- vim - 在 vim 中使用引用/变量设置文件类型
- html - 网格图像在 Firefox(开发版)中正确显示,但在 Chrome 或 Safari 中不正确
- javascript - 为什么表格有效?
- azure - Azure 与 REST API 服务集成
- r - MCA 使用 FactorMineR 库在 R 中输出多少个主成分?
- scala - Call by Name 或 Call by Value 在大 O 表示法中是否有效?
- azure-devops - 是否可以每天跟踪 Azure DevOps 任务中的更改?