我正在设置一个迷你程序,以便操作员扫描条形码,然后在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中有这个代码。我得到的错误信息是
运行时错误''' 对象不支持此属性或方法
它强调If Len位是问题。
有人可以帮忙吗?
答案 0 :(得分:1)
通常,当您使用扫描仪扫描条形码时,它会发送数字并发送"输入"符号或回车,所以我会使用它作为触发器(Worksheet_SelectionChange)而不是工作表上的任何更改......
但无论错误如何,您似乎都没有正确引用该单元
If Len(Sheet1!A2) = 10 Then
应该是
if Len(Sheets("Sheet1").Range("A2").Value) = 10 Then
答案 1 :(得分:0)
如果要在特定的单元格上更改它,请使用以下代码:
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