[英]VBA: Setting a variable as a range based on cell addess
I have the following code: 我有以下代码:
Sub findReplace()
Dim myArray As Variant, rng As Range, str As Variant, cAdd2 As Range
myArray = Array("è", "é", "ë", "ê", "í", "?", "ñ", "ò", "ó", "ô", "ö", "à", "ã", "á", "Á", "ä", "ü", "â", "ø", "š", "??", ">", "<", "+", "*", "^", "ß", "ç", "å", "æ", ".", ";", "#", ":", "'", "-", "@", "Ã", "¨", "É", "Ô", "[", "]", "Ó", "Ñ", "(", ")", "Ö")
Set rng = Workbooks("User").Sheets("Result").Range("B2:B10")
For Each cell In rng
cAdd = cell.Address
Set cAdd2 = Range(cell.Address)
For Each str In myArray
If InStr(cell, str) Then
cAdd2.Offset(, 1).Formula = "=Substitute(" & cAdd & ", " & str & ",""_"")" -->**# This is where I get my error**
Else
cAdd2.Offset(, 1) = "=(" & cAdd & ")"
End If
Next str
Next cell
End Sub
What I want to be able to do is go through a range of cells, replace any characters that are in my array in that cell with an underscore in the cell next to it, and if there is no special characters, then just copy it over. 我想要做的是遍历一系列单元格,用该单元格旁边的下划线替换该单元格中数组中的所有字符,如果没有特殊字符,则将其复制过来。
I believe the problem is with the cell.Address function, but I'm not sure. 我相信问题出在cell.Address函数上,但是我不确定。
Any suggestions is highly appreciated! 任何建议,高度赞赏!
Thanks! 谢谢!
Can you give this a try. 你可以试试看吗? Will also allow for replacements of more then one character
也将允许替换多个字符
Sub findReplace()
Dim myArray As Variant, rng As Range, str As Variant
Dim Form As String
myArray = Array("è", "é", "ë", "ê", "í", "?", "ñ", "ò", "ó", "ô", "ö", "à", "ã", "á", "Á", "ä", "ü", "â", "ø", "š", "??", ">", "<", "+", "*", "^", "ß", "ç", "å", "æ", ".", ";", "#", ":", "'", "-", "@", "Ã", "¨", "É", "Ô", "[", "]", "Ó", "Ñ", "(", ")", "Ö")
Set rng = Workbooks("User").Sheets("Result").Range("B2:B10")
For Each cell In rng
Form = "=(" & cell.Address & ")"
For Each str In myArray
If Not str = vbNullString Then
If InStr(cell, str) Then
If Len(Form) > 0 Then
Form = Replace(Form, cell.Address, "Substitute(" & cell.Address & ", """ & str & """,""_"")") ' -->**# This is where I get my error**
Else
Form = "=Substitute(" & cell.Address & ", """ & str & """,""_"")"
End If
End If
End If
Next str
cell.Offset(, 1).Formula = Form
Next cell
End Sub
The error with that line is in my comments but this will be quicker, as it only has one loop and will replace every special character and not just the last found as @Flephal stated: 该行的错误在我的注释中,但这会更快,因为它只有一个循环,并且将替换每个特殊字符,而不仅仅是@Flephal声明的最后一个字符:
Sub findReplace()
Dim myArray As Variant, rng As Range, str As Variant
myArray = Array("è", "é", "ë", "ê", "í", "?", "ñ", "ò", "ó", "ô", "ö", "à", "ã", "á", "Á", "ä", "ü", "â", "ø", "š", "??", ">", "<", "+", "*", "^", "ß", "ç", "å", "æ", ".", ";", "#", ":", "'", "-", "@", "Ã", "¨", "É", "Ô", "[", "]", "Ó", "Ñ", "(", ")", "Ö")
Set rng = Workbooks("User").Sheets("Result").Range("B2:B10")
rng.Offset(, 1).Value = rng.Value
For Each str In myArray
rng.Offset(, 1).Replace str, "_"
Next str
End Sub
Test: 测试:
Sub findReplace()
Dim myArray As Variant, rng As Range, str As Variant
myArray = Array("E", "S", "!", ")")
Set rng = ActiveSheet.Range("B2:B10")
rng.Offset(, 1).Value = rng.Value
For Each str In myArray
rng.Offset(, 1).Replace str, "_"
Next str
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.