excel 尝试替换VBA中的单词“VERDADERO”或“FALSO”时出错

t3psigkw  于 2022-11-18  发布在  其他
关注(0)|答案(1)|浏览(123)

我在隔壁桌。
| 标题1|标题2|
| - -|- -|
| xxxx年|贝达德罗|
| 贝达德罗|贝达德罗|
| xxxx年|xxxx年|
| 贝达德罗|xxxx年|
| 贝达德罗|xxxx年|
我正在使用西班牙语版本的Excel,我有下一个代码:

Sub replace()
Dim sheet_name, table_name As String

sheet_name= InputBox("enter the sheet name", "Input")
table_name = InputBox("enter the table name", "Input")

With Worksheets(sheet_name).ListObjects(table_name )

.ListColumns("header1").DataBodyRange.Replace _
 What:="VERDADERO", Replacement:="USE LVL 2", _
 SearchOrder:=xlByColumns, MatchCase:=True
    
.ListColumns("header2").DataBodyRange.Replace _
 What:="VERDADERO", Replacement:="USE LVL 1", _
 SearchOrder:=xlByColumns, MatchCase:=True
End With
End Sub

该代码运行正常,但在尝试替换单词“VERDADERO”或“FALSO”时,它没有进行任何更改

laik7k3q

laik7k3q1#

请尝试下一种更简洁的方式。无需将格式更改为文本:

Sub replace()
Dim sheet_name, table_name As String, ws As Worksheet, tbl As ListObject

sheet_name = InputBox("enter the sheet name", "Input")
table_name = InputBox("enter the table name", "Input")

Set ws = Worksheets(sheet_name)
Set tbl = ws.ListObjects(table_name)

 ws.Range(tbl & "[[header1]:[header2]]").replace VERDADERO, "USE LVL 2", xlWhole
 'if the above code does not work (I cannot test), comment it and uncomment the next  line:
 'ws.Range(tbl & "[[header1]:[header2]]").replace True, "USE LVL 2", xlWhole

End Sub

相关问题