我正在尝试在 Excel 中制作具有特定范围的打印按钮。
列永远不会改变,但行会改变。
我的目标是用户可以点击 PrintButton。之后,提示询问他想要使用的行。
这是我到目前为止得到的,但我被卡住了=/
Option Explicit
Private Sub CommandButton1_Click()
Dim myValue As Variant
myValue = InputBox("Please enter the row you're working on.", "Enter Row", "")
ActiveSheet.PageSetup.PrintArea = Range("N" & myValue, "O" & myValue)
ActiveSheet.PrintOut
ActiveSheet.PageSetup.PrintArea = False
End Sub
任何提示如何进行?
谢谢,祝你有美好的一天! =)
编辑:
经过一番研究,我想出了以下代码:
Option Explicit
Private Sub CommandButton1_Click()
Sheets("HARDWARE-INVENTORY").Select
Dim myValue As Variant
myValue = InputBox("Please enter the row you're working on.", "Enter Row", "")
Dim myRange As Range
Set myRange = Range("N" & myValue & ":O" & myValue)
myRange.PrintOut
End Sub
现在我只需要弄清楚如何排除卡住的标题行。
最佳答案
像这样使用 Application.InputBox
让用户选择一个范围8
最后一个参数告诉 Excel 用户应该返回一个范围
Dim rng1 As Range
Set rng1 = Application.InputBox("Select row", "Enter Row", ActiveCell.EntireRow.Address, , , , , 8)
rng1.PrintOut
带有错误处理的更长版本
Sub A2()
Dim rng1 As Range
On Error Resume Next
Set rng1 = Application.InputBox("Select row", "Enter Row", ActiveCell.EntireRow.Address, , , , , 8)
On erorr GoTo 0
'no range
If rng1 Is Nothing Then Exit Sub
If rng1.Rows.Count = 1 Then
rng1.PrintOut
Else
MsgBox "too many rows", vbCritical
End If
End Sub
关于带有用户提示的 Excel VBA 打印按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28474198/