UDF来做一种替代数组函数
我试图做一个函数,不仅用一个文本replace另一个文本,而且用另一个横向值集合replace范围内的一组值。
我有这个:
Public Function SubstituteRange(RangeWithText As Range, TwoColumnMatrix As Range) As String Dim Text As String Text = "/" & RangeWithText.Value & "/" 'as example st like this: "/" & "1/2/3/4/5/6/7/8" & "/" = "/1/2/3/4/5/6/7/8/" Dim SearchForRange As Range Set SearchForRange = TwoColumnMatrix.Columns(1) 'let us say "A1:A4" with /2/ /3/ /4/ /5/ in each cell Dim ReplaceWithRange As Range Set ReplaceWithRange = TwoColumnMatrix.Columns(2) 'let us say "B1:B4" with /9/ /10/ /11/ /12/ in each cell Dim i As Integer SubstituteRange = Text For i = 1 To SearchForRange.Rows.Count '4 rows SubstituteRange = Application.WorksheetFunction.Substitute(SubstituteRange, _ SearchForRange.Item(i), ReplaceWithRange.Item(i)) Next i End Function
但是这会返回一个“#Value!” 错误,有人可以帮助我吗? 我希望从这个例子中得到像“/ 1/9/10/11/12/6/7/8 /”这样的东西,但我没有得到它。 先谢谢你。
Function MultiReplace(v, rng) Dim rw As Range, rv rv = "/" & v & "/" For Each rw In rng.Rows rv = Replace(rv, rw.Cells(1).Value, rw.Cells(2).Value) Next rw MultiReplace = rv End Function
尝试改变这个替代语句(testing):
SubstituteRange = Application.Substitute(SubstituteRange, _ SearchForRange.cells(i).Text, ReplaceWithRange.Cells(i).Text)