Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.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 使用或使用单元格获取多个错误。需要查找-RTE424对象,RTE13类型不匹配,未设置E91块变量_Excel_Vba - Fatal编程技术网

Excel 使用或使用单元格获取多个错误。需要查找-RTE424对象,RTE13类型不匹配,未设置E91块变量

Excel 使用或使用单元格获取多个错误。需要查找-RTE424对象,RTE13类型不匹配,未设置E91块变量,excel,vba,Excel,Vba,这是我有史以来的第一个S.O.问题,因此,如果我的文章太多/缺乏相关信息,我深表歉意 我维护一位前员工为p&L由市场和销售代表创建的Macro Excels。他们从其他软件包中提取数据,由于使用的版本不同,这些软件包的格式也不同。还有一个法语市场。一切只对英语有效,但现在他们希望法语市场也包括在内 我从多个外部文件中的一个单元格中提取一个值,该值除了随附的标签文本外,是不固定的。使用Cells.Find,我正在搜索一个英语短语,并激活相邻的单元格以将该值复制到主Excel中。这很有效 现在,他们

这是我有史以来的第一个S.O.问题,因此,如果我的文章太多/缺乏相关信息,我深表歉意

我维护一位前员工为p&L由市场和销售代表创建的Macro Excels。他们从其他软件包中提取数据,由于使用的版本不同,这些软件包的格式也不同。还有一个法语市场。一切只对英语有效,但现在他们希望法语市场也包括在内

我从多个外部文件中的一个单元格中提取一个值,该值除了随附的标签文本外,是不固定的。使用Cells.Find,我正在搜索一个英语短语,并激活相邻的单元格以将该值复制到主Excel中。这很有效

现在,他们希望在这个过程中包括法语版本,我试图对英语和法语短语进行并行搜索,以找到它是哪一个,并取其中一个,但在尝试StackOverflow中找到的解决方案时,我会得到连续的和循环的错误

这个代码有什么问题

Sub FindMonthlyValues(ByVal varFile As String, ByRef MonthlyProfitLine As String, ByRef MonthlyTonnageLine As String, ByRef MonthlyTripNumberRange As String)

    Dim CurrentPnL, CurrentPath As String
    Dim k, FirstRow, LastRow As Integer
    Dim ProfitEn, ProfitFr As Range    

    'loop through excel files only
    Application.ScreenUpdating = False

    CurrentPnL = varFile

    k = InStr(CurrentPnL, "[") 'find the end position of the default link
    CurrentPath = Left(CurrentPnL, k - 1)
    CurrentPnL = Replace(CurrentPnL, "[", "")
    CurrentPnL = Replace(CurrentPnL, "]", "")

    ChDir _
        CurrentPath
    Workbooks.Open FileName:= _
        CurrentPnL

    ' Find Monthly Profit Line in Initial sheet
    Sheets("Initial").Select
    Range("A1:AK1").Select
    Set ProfitFr = Cells.Find(What:="PROFIT MENSUEL:", After:=ActiveCell, LookIn:=xlFormulas _
        , LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Value

    If ProfitFr Is Nothing Then
        Set ProfitEn = Cells.Find(What:="Monthly Profit:", After:=ActiveCell, LookIn:=xlFormulas _
            , LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False).Value
    End If

    Cells.FindNext(After:=ActiveCell).Activate
    ActiveCell.Offset(, 1).Select
    MonthlyProfitLine = ActiveCell.Address
我尝试了三种不同的StackOverflow解决方案,但它们导致了标题中定义的不同错误循环

在我正在修改的代码部分中,我没有看到显式的选项

我尝试为新变量设置一个Dim来保存找到的短语

我已尝试从原始和功能Cells.FindBLAH.Activate中设置[DimName]=Cells.FindBLAH.Value

我尝试过一个If[DimName]是Nothing,但是它对错误没有影响,错误发生在第一个Cells.Find

我尝试过改变英语或法语单元格的顺序。Find,但它对错误没有影响,错误发生在第一个单元格。Find

我尝试将Dim从范围更改为字符串,但仅得到:

对象未定义


将部分代码分解成独立的子/函数,封装一些特定的功能,您的代码将从中受益匪浅

在一个范围内查找文本是一个明显的备选方案:

Sub Tester()

    Dim f As Range

    Set f = FindFirst(ActiveSheet.Columns(1), Array("PROFIT MENSUEL:", "Monthly Profit:"))

    If Not f Is Nothing Then
        Debug.Print "Found:" & f.Offset(0, 1).Value
    Else
        Debug.Print "No match"
    End If

End Sub


'Find the first value in What in the range rngSearch
'   Returns Nothing if not matches found
Function FindFirst(SearchWhere As Range, FindWhat) As Range
    Dim f As Range, s
    For Each s In FindWhat
        Set f = SearchWhere.Find(What:=s, lookat:=xlPart, _
                     SearchDirection:=xlNext, MatchCase:=False)
        If Not f Is Nothing Then
            Set FindFirst = f
            Exit For
        End If
    Next s
End Function
通过将常见任务推到单独的方法中,您的主代码变得更易于开发和维护


此外,请查看是否可以将此处的指导原则应用到代码中:

如果您单步执行代码,会发生什么?旁注:一般来说,你会想在你的代码中删除.value。对于初学者来说,你是在分配一个对象变量,而不是一个单元格的值。我是F8步进代码,这就是我得到的全部。这是一个单元格。查找在调试中突出显示,并说明了各种错误消息。我知道,这不是很有帮助.值正在替换.Activate。我应该放弃这两个吗?要将两者声明为范围,您需要将Dim ProfitEn作为范围,ProfitFr作为范围等。