![Excel VBA 语法错误](https://linux22.com/image/1573393/Excel%20VBA%20%E8%AF%AD%E6%B3%95%E9%94%99%E8%AF%AF.png)
我正在尝试使用在网上找到的以下代码,但由于语法错误它停止了:见下文
Sub TestCNR()
Cnr 10, 4
End Sub
Sub Cnr(n, r)
i = 1
For j = 1 To r
Cells(i, j).Value = j
Next
Do Until Finished(n, r, i)
j = FindFirstSmall(n, r, i)
' The error occures in below line
For k = 1 To j – 1
Cells(i + 1, k).Value = Cells(i, k).Value
Next
Cells(i + 1, j).Value = Cells(i, j).Value + 1
For k = j + 1 To r
Cells(i + 1, k).Value = Cells(i + 1, k - 1).Value + 1
Next
i = i + 1
Loop
End Sub
Function Finished(n, r, i)
Temp = True
For j = r To 1 Step -1
If Cells(i, j).Value <> j + (n - r) Then
Temp = False
End If
Next
Finished = Temp
End Function
Function FindFirstSmall(n, r, i)
j = r
Do Until Cells(i, j).Value <> j + (n - r)
j = j - 1
Loop
FindFirstSmall = j
End Function
有人可以告知错误原因吗?
答案1
[...] 我在网上找到的代码
For k = 1 To j – 1
该–
字符不是-
。不要只是从互联网上复制粘贴代码 :)
Debug.Print Asc("–")
150
Debug.Print Asc("-")
45
Chr$(150)
不MINUS
被 VBA 的解析器识别为运算符:该标记无效,因此出现语法错误。
仅供参考,这些不合格的Cells
调用隐含地指的是任何东西ActiveSheet
;您可能希望使您的目标表比这更明确。