[英]VBA replacement - syntax error?
I am trying to do a repacement function in VBA. 我正在尝试在VBA中执行repacement功能。 Clearly I have some syntax problem, rookie that I am. 很明显我有一些语法问题,我是菜鸟。 my current approach: 我目前的做法:
Dim i As Integer
For i = 1 To 100
Rows("i:i").Select
Selection.Replace What:="i.xlsx", Replacement:="i+1.xlsx", LookAt:=xlPart _
, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Next i
Can anyone help me to correct the syntax for i ? 任何人都可以帮我纠正我的语法吗? Also instead of 100 lines, i would like to loop until end of file, how can I do that? 而不是100行,我想循环,直到文件结束,我该怎么做?
Thankful for help 感谢您的帮助
"i"
is a string literal ; "i"
是一个字符串文字 ; as such, its value is constant throughout execution of the loop: it will always be "i"
, regardless of the value of i
. 因此,它的值在循环执行期间是恒定的:无论i
的值如何,它总是"i"
。
If you want the variable i
as part of the search/replacement string (eg 12.xlsx
), then you need to concatenate its string representation; 如果你想将变量i
作为搜索/替换字符串的一部分(例如12.xlsx
),那么你需要连接它的字符串表示; What:=i & ".xlsx"
and Replacement:=i + 1 & ".xlsx"
. What:=i & ".xlsx"
和Replacement:=i + 1 & ".xlsx"
。
Avoiding the very frail .Select
instruction is rather easy, too. 避免非常脆弱的.Select
的指令是相当容易了。 Work with the range you're interested in, rather than working with Selection
. 使用您感兴趣的范围,而不是使用Selection
。
Rows("i:i").Replace ...
Or did you mean "i:i"
to also be your loop counter? 或者你的意思是"i:i"
也是你的循环计数器? Because Range("I:I")
points to column I. If you meant to do Rows("12:12")
at the 12th iteration, then you need to concatenate the range address there too: 因为Range("I:I")
指向第I 列 。如果您想在第12次迭代时执行Rows("12:12")
,那么您还需要在那里连接范围地址:
Rows(i & ":" & i).Replace ...
If that's not the case then you'll want to heed @Jeeped's comment regarding the state of your values as the loop progresses. 如果情况并非如此,那么随着循环的进行,你会想要注意@ Jeeped关于你的价值状态的评论 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.