替换 vba 中的特定字母

  • 本文关键字:vba 替换 vba
  • 更新时间 :
  • 英文 :

dict = {}
dict['one'] = "This is one"
dict[2]     = "This is two"
tinydict = {'name': 'john','code':6734, 'dept': 'sales'}

print dict['one']       # Prints value for 'one' key
print dict[2]           # Prints value for 2 key
print tinydict          # Prints complete dictionary
print tinydict.keys()   # Prints all the keys
print tinydict.values() # Prints all the values

我试图将括号添加到 python 代码的内容中,并且必须将"#"替换为"(#",因为哈希符号暗示编辑器中的注释。 对于 Python 代码,"print"也被替换为"print('and addended'
(">

结果应该像下面的代码一样显示

print (tinydict.values() )# Prints all the values)

我把这段代码放到空的 excel 工作表中,然后运行下面的 vba 代码

Sub macro1()
Set Rng = Range([a1], [p40])
For Each c In Rng
If IsEmpty(c.Value) = False Then
If c Like "*print*" Then
c.Replace _
What:="print", Replacement:="print (", _
SearchOrder:=xlByRows, lookat:=xlPart
c.Value = c.Value & ")"   'appending ) at the end'
End If
If c Like "*#*" Then
c.Replace _
What:="#", Replacement:=")#", _
SearchOrder:=xlByRows, lookat:=xlPart
End If
End If
Next c
End Sub

但是,它仅适用于代码的第二行

print ( dict[2]           )# print (s value for 2 key)

包含"#"的行的其余部分没有受到影响。

我想知道是什么原因导致了这种情况。

嘟嘟

您可以将 Option Explicit 添加到程序的顶部。 然后为 Rng 和 c 添加 DIM-s。 但最重要的是,问题是

If c Like "*#*" Then

在线某处查找数字。 这需要通过括在方括号中来查找井号

If c Like "*[#]*" Then

最新更新