电子表格中的问号导致 Excel VBA 出现问题

发布于 2024-10-11 02:58:03 字数 765 浏览 7 评论 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 ?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱殇璃 2024-10-18 02:58:03

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

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+- (因为我们不允许 ModelSubst 中存在空白)

现在将 Subst 更改为

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

结果为 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:

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)

Now change Subst to

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

result 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.

坐在坟头思考人生 2024-10-18 02:58:03

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

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

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

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

效果很好。 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

====

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

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

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:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文