电子表格中的问号导致 Excel VBA 出现问题
当我将包含 ?
的单元格的值与变量进行比较时,它始终返回 true。有什么办法可以防止这种情况发生吗?这是我当前的代码:
'Option Explicit
Dim hws As Worksheet
Set hws = ActiveSheet
Dim rng As Range, rng2 As Range
Dim letters(2, 2)
alpha = Range("CipherTable").Value
For x = 1 To 7
For y = 1 To 7
If alpha(x, y) = rng.Cells(i, j + 1).Value Then
letters(2, 1) = x
letters(2, 2) = y
End If
Next y
Next x
顺便说一下,alpha 看起来像这样:
A B C D E F G
H I J K L M N
O P Q R S T U
V W X Y Z 1 2
3 4 5 6 7 8 9
0 ; : ' " . ,
( ) _ - + ? !
它总是返回 A
,它位于 alpha(1,1) 中。想想看,既然他们都去了七,我不知道为什么它没有返回!
。我怎样才能解决这个问题并使其仅在实际匹配时才返回 true ?
When I compare the value of a cell that contains ?
to a variable, it always returns true. Is there any way I can prevent this? Here is my current code:
'Option Explicit
Dim hws As Worksheet
Set hws = ActiveSheet
Dim rng As Range, rng2 As Range
Dim letters(2, 2)
alpha = Range("CipherTable").Value
For x = 1 To 7
For y = 1 To 7
If alpha(x, y) = rng.Cells(i, j + 1).Value Then
letters(2, 1) = x
letters(2, 2) = y
End If
Next y
Next x
alpha, by the way, looks like this:
A B C D E F G
H I J K L M N
O P Q R S T U
V W X Y Z 1 2
3 4 5 6 7 8 9
0 ; : ' " . ,
( ) _ - + ? !
This always returns A
, which is in alpha(1,1). Come to think of it, since they each go to seven, I don't know why it don't come back with !
. How can I get around this and make it return true only when it actually matches?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我了解,你想创建一个替换算法。如果没有特定原因使用二维密码表,我宁愿使用一维方法,如下所示:
调用此函数,
结果为
THEQUICKBROWN(?)FOX123+-
(因为我们不允许Model
或Subst
中存在空白)现在将
Subst
更改为结果为
4,_73.+'?6910GBF)9ZWVUCD
如果将上述内容输入密码函数,最终会再次得到
THEQUICKBROWN(?)FOX123+-
,正如您对对称替换所期望的那样。As far as I understand you want to create a substitution algorithm. If there is no specific reason to use a two dimensional cipher table I would rather use a one dimensional approach like the following:
calling this function with
results in
THEQUICKBROWN(?)FOX123+-
(because we don't allow blanks inModel
orSubst
)Now change
Subst
toresult is
4,_73.+'?6910GBF)9ZWVUCD
if you feed the above into the cipher function, you end up again with
THEQUICKBROWN(?)FOX123+-
as you would expect from a symetrical substitution.我尝试了以下操作,并得到了预期的结果(它能够找到问号):(
1)在工作表中创建 CipherTable 范围,如上所述;
(2) 创建了一个与上面代码类似的函数QM;
(3) 输入=QM(cell-ref)样式的公式。
效果很好。 Function QM:
====
我还尝试了更直接的方法,并得到了预期的响应:
I tried the following, and got the expected result (it was able to find the question mark):
(1) Created CipherTable range in worksheet, as above;
(2) Created a function QM similar to code above;
(3) Entered a formula in the style of =QM(cell-ref).
It worked fine. Function QM:
====
I also tried something more direct, and got the response expected: