excel VBA 代码错误

2025-02-03 01:05:26
推荐回答(4个)
回答1:

if语句的括号不匹配
if len(cells(x,5))<>len(application.WorksheetFunction.Substitute(cells(x,5)," ","")) then ...

另外,前面几位都没说错,你的代码还是用的工作表函数,不如直接在工作表中输入公式:
=IF(LEN(E1)<>LEN(SUBSTITUTE(E1, " ", "")), "YES", "NO")

VBA检测是否包含空格,可以用INSTR函数:
if instr(cells(x,5), " ")>0 then ...

回答2:

比较长度是多余的。改成这样试试
Sub test1()
Dim x As Integer
For x = 1 To 700
If Cells(x, 5) <> Application.WorksheetFunction.Substitute(Cells(x, 5), " ", "") Then
Cells(x, 6) = "Yes"
Else
Cells(x, 6) = IIf(Cells(x, 5) = "", "", "No")
End If
Next x
End Sub

回答3:

cells(行,列) 你弄反了吧?
if instr(range("E" & x).text," ")>0 then
range("F" & x).value ="Yes"
else
range("F" & x).value ="No"
end if

回答4:

不需要宏也可以啊
=LEN(E1)-LEN(SUBSTITUTE(F1," ",))
然后往下拖,复制公式