Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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
VBA查找和替换问题_Vba_Loops_Excel_Replace_Excel 2010 - Fatal编程技术网

VBA查找和替换问题

VBA查找和替换问题,vba,loops,excel,replace,excel-2010,Vba,Loops,Excel,Replace,Excel 2010,我有一个来自其他人的工作簿,因此文件路径指向此人的本地驱动器。因此,我需要将文件路径替换为本地驱动器中的路径。我尝试了3种方法,但都失败了。请给我一些指导方针。基本上,我试图在整个工作表(几乎所有单元格)的公式中找到替换2个文件路径(见下文): ='U:\Futochan\2012\[Futochan2012.xlsm]计数'!E6+'U:\Futochan\2013\[Futochan2013.xlsm]计数'!E6 第一种方法: 这是手动完成的。数据->编辑链接->更改来源(失败,提示我输入

我有一个来自其他人的工作簿,因此文件路径指向此人的本地驱动器。因此,我需要将文件路径替换为本地驱动器中的路径。我尝试了3种方法,但都失败了。请给我一些指导方针。基本上,我试图在整个工作表(几乎所有单元格)的公式中找到替换2个文件路径(见下文):

='U:\Futochan\2012\[Futochan2012.xlsm]计数'!E6+'U:\Futochan\2013\[Futochan2013.xlsm]计数'!E6

第一种方法: 这是手动完成的。数据->编辑链接->更改来源(失败,提示我输入链接)

第二种方法: VBA:是否替换了range.replace。它只替换了第一个单元并停止

第三种方法: VBA:逐单元格循环:“范围内的每个单元格”。我关掉了一切。它工作了,但花了2个小时/


请帮忙!!谢谢

首先,您为什么不能手动查找并替换“U:\Futochan\2012[Futochan2012.xlsm]”的所有内容?如果只是两个链接,而这是一次性的,那么这是迄今为止最快的方法

对于Range.replace,您的范围是多少?如果您在sheet.Cells.replace(…)上调用它,它应该替换所有实例

最后,下面介绍了一种不涉及Range.Replace的快速方法,但再次强调,重新发明车轮是一种不太可取的方法:)


我不知道为什么第一种方法不起作用。执行此操作之前,请尝试切换到手动计算模式(在“公式”选项卡上),然后将其设置回手动,然后按F9强制重新计算。简单的
Find+Replace
可以按照中的说明工作。只有这个方法可以解决我的问题!很高兴这有帮助。如果它解决了您的问题,请将其标记为所选答案;)
Private stringsToReplace As New Collection
Sub blah()
    Dim ws As Worksheet
    Dim arr
    Dim formulaCells As Range, area As Range
    Dim i As Long, j As Long

    stringsToReplace.Add Array("old1", "new1") 'add as many pairs as you like in the format of Array(oldString,newString)

    Set ws = ActiveSheet

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    On Error Resume Next
    Set formulaCells = ws.Cells.SpecialCells(xlCellTypeFormulas) 'only look at formula cells for speed
    On Error GoTo 0

    If Not formulaCells Is Nothing Then

        For Each area In formulaCells 'we will load into an array in memory, to prevent the horrendously slow enumeration through cells
            If area.Count = 1 Then
                area.Formula = newFormulaText(area.Formula)
            Else
                arr = area.Formula
                For i = LBound(arr, 1) To UBound(arr, 1)
                    For j = LBound(arr, 2) To UBound(arr, 2)
                        arr(i, j) = newFormulaText(arr(i, j))
                    Next j
                Next i
                area.Formula = arr
            End If
        Next area

    End If

    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

End Sub
Function newFormulaText(ByVal oldText As String) As String
    Dim oldNewPair
    Dim newText As String
    newText = oldText
    For Each oldNewPair In stringsToReplace
        newText = Replace(newText, oldNewPair(0), oldNewPair(1))
    Next oldNewPair
    newFormulaText = newText
End Function