在VBA中replacestring中的字符的最快方法

我有一个string(一个Excel范围),如“$ A $ 1:$ AB $ 1200”。

我需要将起始范围的行(这里是1)replace为其他值,比如6(所以string变成“$ A $ 6:$ AB $ 1200”)

我试着用下面的代码,但似乎变得太复杂了

Dim stRng As String stRng = Split(usdRange, ":")(0) Dim row2bRepl As Long row2bRepl = Right(stRng, Len(stRng) - InStr(2, stRng, "$")) usdRange = Replace(stRng, row2bRepl, hdrRow, InStr(2, stRng, "$")) 

任何帮助如何实现这个更简单?

TIA!

这很快很直接!

 Sub test() Dim s1 As String, s2 As String s1 = "$A$1:$AB$1200" s2 = Intersect(Range(s1), Range(s1).Offset(5, 0)).Address 'evaluates to $A$6:$AB$1200 End Sub 

根据你的评论,在这里你可以看到它完全适用于一个完全随机的范围,只在运行时确定

 Sub test() Dim usdRange As String, usdRange2 As String Dim lastRow As Long, lastCol As Long lastRow = Application.WorksheetFunction.RandBetween(10, 100) lastCol = Application.WorksheetFunction.RandBetween(1, 100) usdRange = Range("A1", Cells(lastRow, lastCol)).Address usdRange2 = Intersect(Range(usdRange), Range(usdRange).Offset(5, 0)).Address Debug.Print usdRange, usdRange2 End Sub 

输出以下内容,将所有范围抵消指定的数量:

 Old string New string $A$1:$CF$22 $A$6:$CF$22 $A$1:$AA$93 $A$6:$AA$93 $A$1:$N$82 $A$6:$N$82 

像这样的东西将工作。 这不是很简单,但看起来很简单:

 Public Sub ReplaceString() Dim strInput As String Dim lngFrom As Long Dim lngTo As Long strInput = "$A$1:$AB$1200" lngFrom = InStr(2, strInput, "$") lngTo = InStr(1, strInput, ":") Debug.Print Left(strInput, lngFrom) & "77" & Right(strInput, Len(strInput) - lngFrom-1) End Sub 

它把string的左边和右边的位置放在中间,在这里是77