英文:
How Do I Correct a ByRef Mismatch Error in VBA for Worksheet_Change Sub in Excel?
问题
我正在尝试自动化一个过程,该过程目前涉及点击名为“Fix UPC”的Excel工作表上的按钮。该按钮查看文本格式的字符串,以查看其是否为12个字符长,如果是,则会删除最后一个数字,然后将数字格式设置为“0-00000-00000”。这个工作表将处理的绝大多数UPC都遵循UPC-A标准,但像许多系统一样,我们忽略了最后的检查数字,因此需要将其截断为11位,以便VLOOKUP能够正常工作。还有一些遵循13位EAN标准的UPC,这些UPC需要保持不变。
现在,我在名为Helpers
的模块中有以下函数:
Public Function deleteCheckDigit(theUPC) As String
If Len(theUPC) = 12 Then
deleteCheckDigit = Left(theUPC, 11)
Else
deleteCheckDigit = theUPC
End If
End Function
工作表上的Private Sub如下:
Private Sub Worksheet_Change(ByVal Target As Range)
theCol = Helpers.GetColumnFromAddress(Target.Address)
theValue = Range(Target.Address).Value
If theCol = "A" Then
If Len(theValue) = 12 Then
Target.Value = Helpers.deleteCheckDigit(theValue)
Range(Target.Address).NumberFormat = "0-00000-00000"
End If
End If
End Sub
当我输入一个UPC并按回车键时,Excel会抛出一个编译错误,指出“ByRef参数类型不匹配”。我测试了VarType(theValue)
,它返回8
,表示它是一个字符串,所以我不明白是什么导致了这个错误。
注意:我设置了一个If/Then
块来检查列字母,因为对不同的列进行了一些其他修改。
英文:
I am trying to automate a process that currently involves clicking a button on an Excel Sheet called "Fix UPC". This button looks at a text-formatted string to see if it is 12 characters long, if so, it will delete the last digit and then set the number format to "0-00000-00000". The vast majority of the UPCs this sheet will deal with follow the UPC-A standard, but like a lot of systems, ours ignores the last check digit, hence the need to trim it to 11 so a VLOOKUP will work properly. A few follow the 13-digit EAN standard so those need to be left intact.
Right now I have the following function located in a module named Helpers
:
Public Function deleteCheckDigit(theUPC) As String
If Len(theUPC) = 12 Then
deleteCheckDigit = Left(theUPC, 11)
Else
deleteCheckDigit = theUPC
End If
End Function
The Private Sub on the Worksheet is:
Private Sub Worksheet_Change(ByVal Target As Range)
theCol = Helpers.GetColumnFromAddress(Target.Address)
theValue = Range(Target.Address).Value
If theCol = "A" Then
If Len(theValue) = 12 Then
Target.Value = Helpers.deleteCheckDigit(theValue)
Range(Target.Address).NumberFormat = "0-00000-00000"
End If
End If
End Sub
When I enter in a UPC and hit Enter, Excel throws a Compile Error stating ByRef argument type mismatch
. I tested VarType(theValue)
and it returns 8
meaning it is a string, so I am not seeing what is causing this error.
Note: I have this set up with an If/Then
block to check the column letter because there is some other modifications being done to different columns.
答案1
得分: 1
Len
返回字符串的长度,但 Len
返回数字的长度不同...
所以,最好声明函数参数为 deleteCheckDigit(theUPC As String)
,然后像这样进行参数转换:deleteCheckDigit(CStr(theValue))
,以确保将一个字符串传递给函数。
此外,始终声明所有已使用的变量,分别如下:
Dim theCol As String
Dim theValue As String
这样做并在模块顶部使用 Option Explicit
,如果出现拼写错误,你会得到警告。例如,theVal
。在这种情况下,你可以像这样调用函数:deleteCheckDigit(theValue)
,theValue
已经转换为 String
。
关于使用列字母或数字的争议,这取决于人们调试代码的技能。它们相似,我也更喜欢使用列数字,但对于不太熟练的操作者来说,使用列字母可能更有表现力,有助于理解和调试代码...
英文:
Len
of a string returns what you need. But Len
of a number returns differently...
So, it would be good to declare the function parameter deleteCheckDigit(theUPC As String)
and and call it casting the argument
like deleteCheckDigit(CStr(theValue))
, to be sure that a String
is sent to the function.
It will also be good to ALWAYS declare all used variables, respectively:
Dim theCol As Sring
Dim theValue As String
Doing it and using Option Explicit
on top of the module, you are warned if a typo will appear, by mistake. Something as theVal
. In such a case you can call the function like deleteCheckDigit(theValue)
, theValue
being already converted to String
.
About the dispute regarding using of the column letter or number, it depends on the people skills to debug the code. They are similar and I also prefer using the column number, but using its letter may be more eloquent for a less skilled operator trying to understand and debug the code...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论