Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Excel VBA执行字符替换_Vba_Excel - Fatal编程技术网

使用Excel VBA执行字符替换

使用Excel VBA执行字符替换,vba,excel,Vba,Excel,假设您想设置一个非常简单的,其中a-->1,B-->2。。。等等 假设你想加密的单元格中有一个单词“Hello”。您可以通过每个单词设置一个非常简单的For循环: For i = 1 To Len("Hello") 'perform encryption here Next i 是否有一种快速简便的方法来映射预定义范围中的值?也就是说,我们知道A=1,或1+26,或1+2*(26)。。等等 与其编写IF语句来检查所有字母,我想知道是否有一种优雅的方法来获取:“8 5

假设您想设置一个非常简单的,其中a-->1,B-->2。。。等等

假设你想加密的单元格中有一个单词“Hello”。您可以通过每个单词设置一个非常简单的For循环:

For i = 1 To Len("Hello")
    'perform encryption here
Next i
是否有一种快速简便的方法来映射预定义范围中的值?也就是说,我们知道A=1,或1+26,或1+2*(26)。。等等


与其编写IF语句来检查所有字母,我想知道是否有一种优雅的方法来获取:“8 5 12 12 15”

将单元格的输出作为带字节数组的字符串:

Dim brr() As Byte, i As Long, k As String
brr() = StrConv(Cells(1, 3), vbFromUnicode)
然后根据较大的数组评估新数组中的每个字母:

dim arr as variant
arr = array("a", "b")
For i = 0 To UBound(brr) 'need to start at 0, lbound applies for std array, not byte
    'match brr value to arr value, output arr location
    'k will store the final string
    k = k + 'didn't look up the output for application.match(arr())
Next i

Edit1:多亏了JohnColeman,我可以将Asc()添加到上面,它不需要为A、B、C等添加额外的数组

Dim brr() As Byte, i As Long, k As String
brr() = StrConv(Cells(1, 3), vbFromUnicode)
for i = 0 To UBound(brr) 
    k = k & " " & Asc(brr(i)) 'something like that
next i

将单元格的输出获取为带有字节数组的字符串:

Dim brr() As Byte, i As Long, k As String
brr() = StrConv(Cells(1, 3), vbFromUnicode)
然后根据较大的数组评估新数组中的每个字母:

dim arr as variant
arr = array("a", "b")
For i = 0 To UBound(brr) 'need to start at 0, lbound applies for std array, not byte
    'match brr value to arr value, output arr location
    'k will store the final string
    k = k + 'didn't look up the output for application.match(arr())
Next i

Edit1:多亏了JohnColeman,我可以将Asc()添加到上面,它不需要为A、B、C等添加额外的数组

Dim brr() As Byte, i As Long, k As String
brr() = StrConv(Cells(1, 3), vbFromUnicode)
for i = 0 To UBound(brr) 
    k = k & " " & Asc(brr(i)) 'something like that
next i

这可能会让您开始:

Function StringToNums(s As String) As Variant
    'assumes that s is in the alphabet A, B, ..., Z
    Dim nums As Variant
    Dim i As Long, n As Long
    n = Len(s)
    ReDim nums(1 To n)
    For i = 1 To n
        nums(i) = Asc(Mid(s, i, 1)) - Asc("A") + 1
    Next i
    StringToNums = nums
End Function

Sub test()
    Debug.Print Join(StringToNums("HELLO"), "-") 'prints 8-5-12-12-15
End Sub

这可能会让您开始:

Function StringToNums(s As String) As Variant
    'assumes that s is in the alphabet A, B, ..., Z
    Dim nums As Variant
    Dim i As Long, n As Long
    n = Len(s)
    ReDim nums(1 To n)
    For i = 1 To n
        nums(i) = Asc(Mid(s, i, 1)) - Asc("A") + 1
    Next i
    StringToNums = nums
End Function

Sub test()
    Debug.Print Join(StringToNums("HELLO"), "-") 'prints 8-5-12-12-15
End Sub

使用字典路由,您可以构建一个字典,它是保存密码的键、值对列表。在您的情况下,
a
键将具有值
1
,而
b
键将具有值
2
,依此类推。然后,你可以将你的单词一个字母一个字母地与字典碰撞,以构建密码:

Function caesarCipher(word As String) As String

    'create an array of letters in their position for the cipher (a is 1st, b is 2nd)
    Dim arrCipher As Variant
    arrCipher = Split("a b c d e f g h i j k l m n o p q r s t u v x y z", " ")

    'Create a dictionary from the array with the key being the letter and the item being index + 1
    Dim dictCipher As Scripting.Dictionary
    Set dictCipher = New Dictionary
    For i = 0 To UBound(arrCipher)
        dictCipher.Add arrCipher(i), i + 1
    Next

    'Now loop through the word letter by letter
    For i = 1 To Len(word)
        'and build the cipher output
        caesarCipher = caesarCipher & IIf(Len(caesarCipher) = 0, "", " ") & dictCipher(LCase(Mid(word, i, 1)))
    Next

End Function

这是一个很好的方法,因为你可以改变你的密码为任何你想要的,你只需要你的字典猴子。在这里,我只是从一个数组构建一个字典,并使用数组的索引作为密码输出

使用字典路由,您可以构建一个字典,它是保存密码的键、值对列表。在您的情况下,
a
键将具有值
1
,而
b
键将具有值
2
,依此类推。然后,你可以将你的单词一个字母一个字母地与字典碰撞,以构建密码:

Function caesarCipher(word As String) As String

    'create an array of letters in their position for the cipher (a is 1st, b is 2nd)
    Dim arrCipher As Variant
    arrCipher = Split("a b c d e f g h i j k l m n o p q r s t u v x y z", " ")

    'Create a dictionary from the array with the key being the letter and the item being index + 1
    Dim dictCipher As Scripting.Dictionary
    Set dictCipher = New Dictionary
    For i = 0 To UBound(arrCipher)
        dictCipher.Add arrCipher(i), i + 1
    Next

    'Now loop through the word letter by letter
    For i = 1 To Len(word)
        'and build the cipher output
        caesarCipher = caesarCipher & IIf(Len(caesarCipher) = 0, "", " ") & dictCipher(LCase(Mid(word, i, 1)))
    Next

End Function

这是一个很好的方法,因为你可以改变你的密码为任何你想要的,你只需要你的字典猴子。在这里,我只是从一个数组构建一个字典,并使用数组的索引作为密码输出

所有答案都很好,但这就是你使用字典的方式,它更简单、更直接。我隐式地定义了字典,以便于启动,但最好通过在VBE中添加工具>引用中的
运行时脚本
来显式地定义字典

Sub Main()
    Dim i As Integer
    Dim ciphered As String, str As String
    Dim dict As Object

    Set dict = CreateObject("scripting.Dictionary")
    str = "Hello"

    For i = 65 To 122
        dict.Add Chr(i), i - 64
    Next i

    For i = 1 To Len(str)
        ciphered = ciphered & "-" & dict(Mid(UCase(str), i, 1))
    Next i
    ciphered = Right(ciphered, Len(ciphered) - 1)

    Debug.Print ciphered
End Sub
如果从字典中获取代码时删除
ucase
,则它将计为大小写,这意味着大写或小写将有不同的代码。您可以轻松地将其更改为函数,不要忘记删除
str=“Hello”
。现在它返回:

输出


8-5-12-12-15

所有的答案都很好,但这就是你使用字典的方式,它更简单、更直接。我隐式地定义了字典,以便于启动,但最好通过在VBE中添加工具>引用中的
运行时脚本
来显式地定义字典

Sub Main()
    Dim i As Integer
    Dim ciphered As String, str As String
    Dim dict As Object

    Set dict = CreateObject("scripting.Dictionary")
    str = "Hello"

    For i = 65 To 122
        dict.Add Chr(i), i - 64
    Next i

    For i = 1 To Len(str)
        ciphered = ciphered & "-" & dict(Mid(UCase(str), i, 1))
    Next i
    ciphered = Right(ciphered, Len(ciphered) - 1)

    Debug.Print ciphered
End Sub
如果从字典中获取代码时删除
ucase
,则它将计为大小写,这意味着大写或小写将有不同的代码。您可以轻松地将其更改为函数,不要忘记删除
str=“Hello”
。现在它返回:

输出


8-5-12-12-15

获取ascii值并进行一些运算。没有理由使用包含26个子句的if语句。您可以设置一个数组并在单元格中拆分字符串以读取每个字母。在arrayA中找到字母位置并输出该位置。我将使用dictionary@Cyril-您能演示一个非常简单和快速的示例吗?您知道函数
Asc()
?获取ascii值并进行一些运算。没有理由使用包含26个子句的if语句。您可以设置一个数组并在单元格中拆分字符串以读取每个字母。在arrayA中找到字母位置并输出该位置。我将使用dictionary@Cyril-您能演示一个非常简单快速的示例吗?您是否知道函数
Asc()
?根据您的评论更新了我的答案。感谢您提供有关Asc()的信息,因为在字节数组中逐字母分隔后,这使它变得更容易。更新了我的答案以回应您的评论。感谢您提供有关Asc()的信息,因为这使得在字节数组中逐字母分隔后变得更容易。
StrConv
似乎是一种自然的方式(+1),尽管由于某些原因,它与
Join()
StrConv
似乎是一种自然的方式(+1),尽管由于某些原因,它不能很好地与
Join()