0

当我比较包含?变量的单元格的值时,它总是返回 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?

4

2 回答 2

1

据我了解,您想创建一个替换算法。如果没有特定理由使用二维密码表,我宁愿使用如下一维方法:

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

调用这个函数

Sub Test()
    Debug.Print Cipher("The quick brown (?) fox 123 +-")
End Sub

结果THEQUICKBROWN(?)FOX123+-(因为我们不允许在Modelor中有空格Subst

现在Subst改为

Subst = "!?+-_)(,.""':;0987654321ZYXWVUTSRQPONMLKJIHGFEDCBA"

结果是4,_73.+'?6910GBF)9ZWVUCD

如果您将上述内容输入密码函数,您将再次THEQUICKBROWN(?)FOX123+-得到对称替换所期望的结果。

于 2011-01-11T08:56:06.537 回答
0

我尝试了以下,并得到了预期的结果(它能够找到问号):

(1) 在工作表中创建 CipherTable 范围,如上;

(2)创建了一个类似于上面代码的函数QM;

(3) 输入 =QM(cell-ref) 样式的公式。

它工作得很好。功能质量管理:

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

====

我还尝试了更直接的方法,并得到了预期的响应:

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
于 2011-01-14T15:32:09.090 回答