Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 使用工作表函数时出现问题。请将替换与偏移结合使用_Excel_Vba_Range_Offset_Substitution - Fatal编程技术网

Excel 使用工作表函数时出现问题。请将替换与偏移结合使用

Excel 使用工作表函数时出现问题。请将替换与偏移结合使用,excel,vba,range,offset,substitution,Excel,Vba,Range,Offset,Substitution,我需要编写一个函数,根据在工作表中查找的值,对字符串执行大量替换 我的意图是迭代工作表中的替换对列表,并为每次迭代调用工作簿函数“substitute” Function multiSub(original As Range, replaceList As Range) Dim temp1 As String Dim temp2 As String Dim temp3 As String ' temp1 = replaceList.Offset(0, 0).Value ' temp

我需要编写一个函数,根据在工作表中查找的值,对字符串执行大量替换

我的意图是迭代工作表中的替换对列表,并为每次迭代调用工作簿函数“substitute”

Function multiSub(original As Range, replaceList As Range)
Dim temp1 As String
Dim temp2 As String
Dim temp3 As String

  '  temp1 = replaceList.Offset(0, 0).Value
  '  temp2 = replaceList.Offset(0, 1).Value
  temp1 = "from"
  temp2 = "to"
    multiSub = Application.WorksheetFunction.Substitute(original, temp1, temp2)
End Function
如果您按照原样处理代码,那么它就可以工作,因为如果我创建的函数中的第一个参数指向某个包含单词“from”的单元格,那么它将用单词“to”替换单词“from”

但是,如果我将赋值注释掉到temp1或temp2,并取消对其他行的注释,我会得到一个#值!工作表中出现错误

有趣的是,即使我将一个不相关的变量(比如temp3)赋给这些范围偏移量中的一个,并将temp1和temp2保持为引用硬编码字符串,它仍然以同样的方式失败


为什么会发生这种情况?我想您不希望单元格偏移,因为偏移量将返回与父范围大小相同的范围

Function multiSub(original As Range, replaceList As Range)
Dim temp1 As String
Dim temp2 As String
If replaceList.Rows.Count <> 1 Or replaceList.Columns.Count <> 2 Then
    multiSub = "error"
End If

  temp1 = replaceList.Cells(1, 1).Value
  temp2 = replaceList.Cells(1, 2).Value


    multiSub = Replace(original, temp1, temp2)
End Function
函数multiSub(原始范围,替换列表范围)
作为字符串的Dim temp1
作为字符串的Dim temp2
如果replaceList.Rows.Count为1或replaceList.Columns.Count为2,则
multiSub=“错误”
如果结束
temp1=replaceList.Cells(1,1).Value
temp2=replaceList.Cells(1,2).Value
multiSub=更换(原件、临时1、临时2)
端函数
对于您的多重替换:

Function multiSub(original As Range, replaceList As Range)

If replaceList.Columns.Count <> 2 Then
    multiSub = "error"
End If

Dim temp1 As Variant
 temp1 = replaceList

Dim i As Long
For i = LBound(temp1, 1) To UBound(temp1, 1)
    multiSub = Application.Trim(Replace(" " & original & " ", " " & temp1(i, 1) & " ", " " & temp1(i, 2) & " "))
Next i
End Function
函数multiSub(原始范围,替换列表范围)
如果replaceList.Columns.Count为2,则
multiSub=“错误”
如果结束
Dim temp1作为变体
temp1=替换列表
我想我会坚持多久
对于i=LBound(temp1,1)到UBound(temp1,1)
multiSub=Application.Trim(替换(“&original&“,”&temp1(i,1)&“,”&temp1(i,2)&“)
接下来我
端函数

这将迭代两列范围中的行,并用第2列中的值替换第1列中的项。

我最后编写的代码是这样的,尽管它没有验证只有两列:

Function multiSub(original As Range, replaceList As Range)
Dim temp1 As String
Dim temp2 As String
Dim row As Range

multiSub = original
    For Each row In replaceList.Rows
        multiSub = Application.WorksheetFunction.Substitute(multiSub, row.Cells(1, 1), row.Cells(1, 2))
    Next row
End Function

当斯科特解决我的问题时,我将把他的答案作为公认的答案。

Replacelist是两个单元格的水平范围吗?可能是:它是一个水平两个单元格,垂直两个任意数字的列表。左边是原版,右边是替换版。哇,那是一个完全不同的野兽。你可能想看到多个替换版的编辑。谢谢你的帮助。你能想出一个合理的方法来扩展它,这样它就可以代替整个单词吗?e、 g.我可能会安排将“Ed”替换为“Education”,而“Education”不会替换为“Educationucation”,我正在考虑一些基于正则表达式的东西,但我想知道Excel是否还有其他一些技巧可以做到这一点?顺便说一句,您的代码会更慢,因为它会迭代一个范围而不是数组。使用“替换”将比“工作表公式替换”更快。啊,我知道你的方法更好。我看你已经花了一点时间来处理整句话。但是,单词可以以标点符号结尾,因此这里需要更复杂的方法。也许在标点符号周围插入空格,然后在末尾重新标点的方法是一种解决方法。