Excelinput框要打印多less份以最大数量

我需要一个input框来询问可以input的最大号码打印多less份。

我有下面的代码,但工程,但如果我input超过“1”触发
If NumberOfCopies >= "11" Then ,如果我点击取消button它也触发相同的事情

 'ASKS HOW MANY COPIES TO PRINT PRINTS: Dim NumberOfCopies As String NumberOfCopies = Application.InputBox("How many copies do you want to print? Must enter 0-10", Type:=2) If NumberOfCopies >= "11" Then MsgBox "Max to print is 10 copies" GoTo PRINTS End If If NumberOfCopies = "0" Or NumberOfCopies = "" Then 'do not print Else ActiveSheet.PrintOut Copies:=NumberOfCopies End If 

仍然需要testing用户击中取消…这在我点击取消,或input0,1或11时为我工作

 Prints: Dim NumberOfCopies As String NumberOfCopies = Application.InputBox("How many copies do you want to print? Must enter 0-10", Type:=2) If NumberOfCopies = "False" Or NumberOfCopies = "0" Then 'If user hits Cancel, NumberofCopies will be "False" Else If NumberOfCopies >= "11" Then MsgBox "Max to print is 10 copies" GoTo Prints Else ActiveSheet.PrintOut Copies:=NumberOfCopies End If End If 

将数字放在引号中会将它们变成一个string。 由于您希望用户input一个数字,您应该进行以下更改:

  Dim NumberOfCopies As Int NumberOfCopies = Application.InputBox("How many copies do you want to print Must enter 0-10", Type:=1) If NumberofCopies >= 11 Then ... If NumberOfCopies = 0 or NumberOfCopies = "" Then ... 

Type:=2实际上是文本types。 你正在使用数字,所以你应该把它设置为Type:=1 。 因此,如果用户input的不是数字,它将自动popup“数字无效”的消息(不需要脚本)。

您应该尽可能避免使用位置标签。 您的代码可以很容易地用Do...While循环编写脚本。

正如已经指出的那样,在使用数字时,不要将它们用双引号括起来,否则VBA会把它们当作String(这不是你想要的)。

@Rdster为用户点击“取消”button带来了一个好点。 当发生这种情况时,variables的值将是“False”(一个string),所以你应该寻找那个。

所有这一切说,我相信你的代码会更好地像这样工作:

 'ASKS HOW MANY COPIES TO PRINT Dim NumberOfCopies As String Do NumberOfCopies = Application.InputBox("How many copies do you want to print? Must enter 0-10", Type:=1) If NumberOfCopies = "False" Then Exit Sub ' If user clicks on Cancel button If NumberOfCopies > 10 Then MsgBox "Max to print is 10 copies" End If Loop While NumberOfCopies > 10 'although producing the same result, it is cleaner to compare with 10, than to >=11 'Avoid leaving blank conditions. Work with what you have. 'Everything that doesn't match your conditions will be skipped anyways. If NumberOfCopies > 0 And NumberOfCopies < 10 Then ActiveSheet.PrintOut copies:=NumberOfCopies End If