【问题标题】:Autoprint when cell is 10 characters long Excel单元格长度为 10 个字符时自动打印 Excel
【发布时间】:2014-11-29 01:39:37
【问题描述】:

我正在设置一个小程序,以便操作员扫描条形码,然后在 Excel 中创建一个并在标签打印机上打印。

我需要它在他被扫描到 10 位数代码后立即自动打印。

到目前为止,我所拥有的是 -

选项显式

Private Sub Worksheet_Change(ByVal Target As Range)
If Len(Sheet1!A2) = 10 Then
ActiveWorkbook.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False
Sheets("Sheet1").Range("A2").ClearContents
End If
End Sub

但这似乎不起作用。我在 Sheet1 中有这个代码。我得到的错误信息是

运行时错误“438” 对象不支持该属性或方法

它突出了 If Len 位作为问题。

有人可以帮忙吗?

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    通常,当您使用扫描仪扫描条形码时,它会发送数字并发送“输入”符号或回车符,因此我会将其用作触发器(Worksheet_SelectionChange)而不是工作表上的任何更改...

    但不管错误似乎你没有正确引用单元格

    If Len(Sheet1!A2) = 10 Then
    

    应该是

    if Len(Sheets("Sheet1").Range("A2").Value) = 10 Then
    

    【讨论】:

      【解决方案2】:

      如果您要让它在特定单元格更改时触发,请使用以下代码:

          Option Explicit
          Private Sub Worksheet_Change(ByVal Target As Range)
              'Checks if A2 was the changed cell
              If Intersect(Target, Sheets("Sheet1").Range("A2")) Is Nothing Then Exit Sub
                  Else
              Application.EnableEvents = False 'to prevent endless loop
                  If Len(Target.Value) = 10 Then
                      ActiveWorkbook.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False
                      Target.ClearContents
                  Else
                  End If
              End If
              Application.EnableEvents = True
          End Sub
      

      在这里使用彼得的代码:excel VBA run macro automatically whenever a cell is changed

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多