我目前正坐在一个excel宏上,它应该替换不同国家的不同特殊字符。 每个国家都在替换特殊的字符。另一种方式。
E.g:
表格如下:
A栏:名字
B栏:中间名
C栏:姓氏
D栏:国家
首先应检查宏,在excel中列出哪个国家/地区,然后更换特殊字符。在A栏和C栏中。
实际代码:
Sub UmlauteErsatezen()
Dim Sh As Worksheet
If cell D:D = "Germany" then
For Each Sh In ActiveWorkbook.Worksheets
With Sh.UsedRange
.Replace What:="Ö", Replacement:="oe", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True
.Replace What:="Ü", Replacement:="ue"
.Replace What:="Ä", Replacement:="ae"
else if cell D:D ="Belgium" then
For Each Sh In ActiveWorkbook.Worksheets
With Sh.UsedRange
.Replace What:="Ö", Replacement:="o", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True
.Replace What:="Ü", Replacement:="u"
.Replace What:="Ä", Replacement:="a"
End If
End With
Next
End If
End Sub
我有什么想法可以做到吗?
答案 0 :(得分:4)
如果要检查,是否在单元格D1中写入“德国”,请写下:
If Range("D1") = "Germany" Then
此If cell D:D = "Germany" then
会产生编译错误。
答案 1 :(得分:2)
Select Case
或创建要与VLOOKUP一起使用的表Select Case
方法示例Sub UmlauteErsatezen()
Dim Sh As Worksheet
Dim iCell As Range
Dim vArr(), vArrSub()
Dim i As Long
For Each Sh In Thisworkbook.Worksheets
For Each iCell in Sh.UsedRange.Columns("D").Cells
Erase vArr : Erase vArrSub
Select Case iCell.Value
Case "Germany"
vArr = Array("Ö", "Ü", "Ä")
vArrSub = Array("oe", "ue", "ae")
Case "Belgium"
vArr = Array("Ö", "Ü", "Ä")
vArrSub = Array("o", "u", "a")
Case Else
'Think of something
End Select
For i = Lbound(vArr) To Ubound(vArr)
Intersect(iCell.EntireRow, _
Sh.UsedRange).Replace vArr(i), _
vArrSub(i), _
xlPart
Next i
Next
Next
End Sub
我并未声明这些是回答这个问题的唯一方法。