VBA 中的无效过程调用或参数错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8064958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 14:25:07  来源:igfitidea点击:

Invalid procedure call or argument error in VBA

excelvbaerror-handling

提问by Andrei Ion

I get this error and I really don't know why.

我收到此错误,我真的不知道为什么。

I have the following piece of code:

我有以下一段代码:

Rand = 2
LRand = ws.Cells(Rows.Count, 6).End(xlUp).Row
Do While Rand <> LRand + 1
    If ws.Cells(Rand, 4).Value = "" Then
        desch = InStr(ws.Cells(Rand, 5).Value, "(")
        inch = InStr(ws.Cells(Rand, 5).Value, ")")
        ws.Cells(Rand, 4).Value = Mid(ws.Cells(Rand, 5).Value, desch + 1, inch - desch - 1) 'here is the error
    End If
Rand = Rand + 1
Loop

Do you guys have any idea why I get the invalid procedure call or argument? Thanks a lot!

你们知道为什么我得到无效的过程调用或参数吗?非常感谢!

回答by niko

it is possible

有可能的

 if desch = 0
      or 
 if inch = 0

 desch = instr('this may return zero')
 inch  = instr('when it doesnt find the substring')

 so putting them in mid functions results like these

 mid(string,0,0) results error  
 mid(string(0,2) results error
 mid(string(2,0) results error

So check those values at first

所以首先检查这些值

回答by GTG

This code assumes that the contents of the cell being processed, ws.Cells(Rand, 5).Value, are on the form "(some text here)". If that assumption is wrong, the above error will occur. This will happen if the cell is empty, either ( or ) is missing or if ) comes before (.

此代码假定正在处理的单元格的内容 ws.Cells(Rand, 5).Value 位于表单“(此处为某些文本)”上。如果这个假设是错误的,就会出现上面的错误。如果单元格为空、 ( 或 ) 丢失或如果 ) 出现在 (.