[英]Question mark in spreadsheet causes problems in Excel VBA
When I compare the value of a cell that contains ?
当我比较包含
?
的单元格的值时 to a variable, it always returns true. 到变量,它总是返回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: 顺便说一下,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 ; : ' " . ,
( ) _ - + ? !
This always returns A
, which is in alpha(1,1). 这总是返回
A
,它位于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?
如何解决这个问题,并使其仅在实际匹配时才返回true?
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:
如果没有特定的原因使用二维密码表,我宁愿使用一维方法,如下所示:
Function Cipher(Argument As String) As String
Dim Model As String
Dim Subst As String
Dim Idx As Integer
Dim MyPos As Integer
Cipher = ""
' note double quotation mark within string
Model = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890;:'"".,()_-+?!"
Subst = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890;:'"".,()_-+?!"
For Idx = 1 To Len(Argument)
' get position from Model
MyPos = InStr(1, Model, UCase(Mid(Argument, Idx, 1)))
' return character from substitution pattern
If MyPos <> 0 Then Cipher = Cipher & Mid(Subst, MyPos, 1)
Next Idx
End Function
calling this function with 用
Sub Test()
Debug.Print Cipher("The quick brown (?) fox 123 +-")
End Sub
results in THEQUICKBROWN(?)FOX123+-
(because we don't allow blanks in Model
or Subst
) 结果
THEQUICKBROWN(?)FOX123+-
因为我们不允许在空白Model
或Subst
)
Now change Subst
to 现在将
Subst
更改为
Subst = "!?+-_)(,.""':;0987654321ZYXWVUTSRQPONMLKJIHGFEDCBA"
result is 4,_73.+'?6910GBF)9ZWVUCD
结果是
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. 如果将以上内容提供给密码函数,
THEQUICKBROWN(?)FOX123+-
再次得到THEQUICKBROWN(?)FOX123+-
,这与对称替换所期望的一样。
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; (1)如上在工作表中创建CipherTable范围;
(2) Created a function QM similar to code above; (2)创建了与上述代码相似的功能QM;
(3) Entered a formula in the style of =QM(cell-ref). (3)输入= QM(cell-ref)样式的公式。
It worked fine. 工作正常。 Function QM:
功能质量管理:
Public Function QM(theChar)
Dim CipherTable
Dim x As Integer
Dim y As Integer
CipherTable = Range("CipherTable").Value
For x = 1 To 7
For y = 1 To 7
If CipherTable(x, y) = theChar Then
QM = "X" & x & "Y" & y
Exit Function
End If
Next y
Next x
QM = ""
End Function
==== ====
I also tried something more direct, and got the response expected: 我还尝试了一些更直接的方法,并获得了预期的响应:
Public Sub QM2()
Dim questMark As Range
Dim someChar As String
Set questMark = Range("CipherTable").Cells(7, 6)
someChar = "A"
Debug.Print questMark = someChar
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.