我正在使用Excel2016。我在使用VBA进行应用程序方面有一定的经验,并且在编程方面有一定的经验。
我正在尝试从条形码扫描仪获取输入,将其与电子表格中的一列进行比较,如果存在匹配项,请在一些单元格中添加一些字符和一个日期戳(初始值和日期,分别在单独的列中)。
这个问题具有非常相似的用例,并且包括一个代码示例。我尝试了代码示例,但无法正常工作。最初,阵列存在问题。最终,我发现您可以执行“ C2:C8”,尽管没有在任何地方进行文档记录(可能是基础课程/课程的一部分,但找不到),但它似乎可行。关于为Match()定义的子函数或函数存在错误,因此我在安全中心启用了求解器加载项。那没有解决它,所以我发现这个论坛帖子解释了Match不是VBA功能。
现在,单击按钮“运行时错误1004,无法获取WorksheetFunction类的Match属性”后,出现错误,单击“调试”将我带到同一行。
这是我想到的代码:
Private Sub CommandButton1_Click()
code = InputBox("Please scan a barcode and hit enter if you need to")
matchedCell = Application.WorksheetFunction.Match(code, Range("C2:C8"), 0)
matchedCell.Offset(0, 2) = Now
End Sub
这真令人沮丧,因为我认为这是一件简单的事情,并且已经解决了。似乎没有在解决问题和构建软件,而是在与语法和/或环境进行斗争。我究竟做错了什么?
两种可能性:
对象的使用Match()
功能Application
并将其返回值存储在Variant变量中,以检查是否有任何错误(如果未找到值)
Private Sub CommandButton1_Click()
Dim code As Variant
Dim matchedCell As Variant
code = InputBox("Please scan a barcode and hit enter if you need to")
matchedCell = Application.Match(code, Range("C2:C8"), 0)
If Not IsError(matchedCell) Then Range("C2:C8").Cells(matchedCell, 1).Offset(0, 2).Value = Now
End Sub
对象的使用Find()
功能Range
Private Sub CommandButton1_Click()
Dim code As Variant
Dim matchedCell As Range
code = InputBox("Please scan a barcode and hit enter if you need to")
Set matchedCell = Range("C2:C8").Find(what:=code, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=True)
If Not matchedCell Is Nothing Then matchedCell.Offset(0, 2).Value = Now
End Sub
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句