Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.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 应用程序。匹配不匹配错误13-如何解决?_Vba_Excel_Excel Match - Fatal编程技术网

Vba 应用程序。匹配不匹配错误13-如何解决?

Vba 应用程序。匹配不匹配错误13-如何解决?,vba,excel,excel-match,Vba,Excel,Excel Match,我一直在编写一些代码,试图找到“新的沉默价格”或“新的反对价格”。如果在数组迭代过程中没有找到,它会给出一个 不匹配错误运行代码13 我知道这是因为它没有找到它,但是我如何让它跳过错误并继续代码呢?我试过了 if not iserror (PriceCol = Application.Match("New Opposed Price", rng, 0)) then but it is still showing the mismatch error. 出现错误的代码部分是: PriceCo

我一直在编写一些代码,试图找到“新的沉默价格”或“新的反对价格”。如果在数组迭代过程中没有找到,它会给出一个

不匹配错误运行代码13

我知道这是因为它没有找到它,但是我如何让它跳过错误并继续代码呢?我试过了

if not iserror (PriceCol = Application.Match("New Opposed Price", rng, 0)) then but it is still showing the mismatch error. 
出现错误的代码部分是:

 PriceCol = Application.Match("New Opposed Price", rng, 0)

有人对如何解决这个问题有什么建议吗

Sub WIP()
    Dim wb As Workbook
    Dim wsMain As Worksheet
    Dim wsLookup As Worksheet
    Dim rng As Range
    Dim rng2 As Range
    Dim rFind1 As Range
    Dim rFind2 As Range
    Dim rFind3 As Range
    Dim MyArray As Variant
    Dim LookupHeaders As Variant
    Dim LookupHeaders2 As Variant
    Dim LR As Long
    Dim i As Long
    Dim PriceCol As Long
    Dim pricecol2 As Long
    Dim LastColumn As Long
     Dim LastColumn2 As Long
       Dim LastColumn3 As Long
     Dim LastColumn4 As Long
    Dim IndexCol As Long

     'Unformatted Price Row
  Sheets("Consolidate List").Select
    LR = Range("A" & Rows.Count).End(xlUp).Row
    Columns("H:H").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Columns("J:N").Delete
    Columns("J:J").Select
    ActiveWindow.FreezePanes = True
    Range("H2").Select
    ActiveCell.FormulaR1C1 = "New Price"
    ActiveCell.Interior.ColorIndex = 22
         Range("H3:H" & LR).Formula = "=VLOOKUP(RC[-7],'Connect Report'!C[-7]:C[-6],2,FALSE)"
         ActiveCell.EntireColumn.Resize(Rows.Count - 2).Offset(2).Select
 Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        Range("I2").Select
    ActiveCell.FormulaR1C1 = "Difference"
    ActiveCell.Interior.ColorIndex = 22
    Range("I3:I" & LR).Formula = "=IF(OR(OR(RC[-2]="""",RC[-1]="""",RC[-1]=""x"",)),"""",RC[-1]-RC[-2])"
         ActiveCell.EntireColumn.Resize(Rows.Count - 2).Offset(2).Select
 Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

    Set wb = ActiveWorkbook
     Sheets("Consolidate List").Select
    Set wsMain = wb.ActiveSheet
    Set wsLookup = wb.Sheets("Connect Report")     '<-- Change to correct sheet name for the Lookup sheet
    LR = wsMain.Cells(wsMain.Rows.Count, "A").End(xlUp).Row
    MyArray = Array("US", "SPAIN", "California")
    LookupHeaders = Array("TTIER", "Time333", "Round6")
  LookupHeaders2 = Array("TELLER5", "Fly7", "Mine4")

    For i = LBound(MyArray) To UBound(MyArray)
        With wsMain.Rows(1)
            Set rFind1 = .Find(What:=MyArray(i), LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
            If Not rFind1 Is Nothing Then
                Set rng = rFind1.Offset(1).Resize(, 8)
                PriceCol = Application.Match("New Opposed Price", rng, 0)
                LastColumn = rFind1.Column + PriceCol
                If wsMain.Cells(rng.Row, LastColumn) <> "New Opposed Price" Then
                    wsMain.Columns(LastColumn).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
                    wsMain.Cells(rng.Row, LastColumn).Value = "New Opposed Price"
                    wsMain.Cells(rng.Row, LastColumn).Interior.ColorIndex = 22
                    LastColumn2 = LastColumn + 1
                     wsMain.Columns(LastColumn2).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
                    wsMain.Cells(rng.Row, LastColumn2).Value = "Difference"
                    wsMain.Cells(rng.Row, LastColumn2).Interior.ColorIndex = 22

                Set rFind2 = wsLookup.Rows(1).Find(LookupHeaders(i), wsLookup.Range("A1"), xlValues, xlWhole)
                If Not rFind2 Is Nothing Then
                    IndexCol = rFind2.Column
                    wsMain.Cells(rng.Row + 1, LastColumn).Resize(LR - 2).Formula = "=VLOOKUP(A" & rng.Row + 1 & ",'Connect Report'!$A:$AL," & IndexCol & ",FALSE)"

                      wsMain.Cells(rng.Row + 1, LastColumn2).Resize(LR - 2).Formula = "=IF(OR(OR(RC[-2]="""",RC[-1]="""",RC[-1]=""x"",)),"""",RC[-1]-RC[-2])"
                              Else
                    MsgBox "Excel could not find " & LookupHeaders(i) & " in the lookup table."
                End If

                Set rng2 = rFind1.Offset(1).Resize(, 8)
                pricecol2 = Application.Match("New Muted Price", rng, 0)
                LastColumn3 = rFind1.Column + pricecol2
                   If wsMain.Cells(rng.Row, LastColumn3) <> "New Muted Price" Then
                    wsMain.Columns(LastColumn3).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
                    wsMain.Cells(rng2.Row, LastColumn3).Value = "New Muted Price"
                    wsMain.Cells(rng2.Row, LastColumn3).Interior.ColorIndex = 22
                    LastColumn4 = LastColumn3 + 1
                      wsMain.Columns(LastColumn4).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
                    wsMain.Cells(rng2.Row, LastColumn4).Value = "Difference"
                    wsMain.Cells(rng2.Row, LastColumn4).Interior.ColorIndex = 22
                End If

                      Set rFind3 = wsLookup.Rows(1).Find(LookupHeaders2(i), wsLookup.Range("A1"), xlValues, xlWhole)
                If Not rFind3 Is Nothing Then
                    IndexCol = rFind3.Column
                    wsMain.Cells(rng2.Row + 1, LastColumn3).Resize(LR - 2).Formula = "=VLOOKUP(A" & rng2.Row + 1 & ",'Connect Report'!$A:$AL," & IndexCol & ",FALSE)"

                      wsMain.Cells(rng2.Row + 1, LastColumn4).Resize(LR - 2).Formula = "=IF(OR(OR(RC[-2]="""",RC[-1]="""",RC[-1]=""x"",)),"""",RC[-1]-RC[-2])"

                    Else
                    MsgBox "Excel could not find " & LookupHeaders2(i) & " in the lookup table."
                End If
                End If
             End If
        End With
    Next i
End Sub
子在制品()
将wb设置为工作簿
将wsMain设置为工作表
将wsLookup设置为工作表
变暗rng As范围
变暗rng2 As范围
Dim rFind1 As范围
Dim rFind2 As范围
Dim rFind3 As范围
Dim MyArray作为变体
作为变体的暗色斗士
作为变型,2的外观暗淡
变暗LR为长
我想我会坚持多久
暗淡的、漫长的
暗淡的价格2一样长
将最后一列变长
第2列的长度相同
第三列的长度与第三列一样长
第4列和第4列一样长
Dim IndexCol尽可能长
'未格式化价格行
工作表(“合并列表”)。选择
LR=范围(“A”和Rows.Count).End(xlUp).Row
列(“H:H”)。选择
选择。插入Shift:=xlToRight,CopyOrigin:=xlFormatFromLeftOrAbove
列(“J:N”)。删除
列(“J:J”)。选择
ActiveWindow.FreezePanes=True
范围(“H2”)。选择
ActiveCell.FormulaR1C1=“新价格”
ActiveCell.Interior.ColorIndex=22
范围(“H3:H”和LR.Formula=“=VLOOKUP(RC[-7],'Connect Report'!C[-7]:C[-6],2,FALSE)”
ActiveCell.entireclumn.Resize(Rows.Count-2).偏移量(2).选择
选择,复制
Selection.Paste特殊粘贴:=xlPasteValues,操作:=xlNone,SkipBlank_
:=假,转置:=假
范围(“I2”)。选择
ActiveCell.FormulaR1C1=“差异”
ActiveCell.Interior.ColorIndex=22
范围(“I3:I”和LR).Formula=“=IF(或(或(RC[-2]=”,RC[-1]=”,RC[-1]=”,RC[-1]=“x”),“”,RC[-1]-RC[-2]))
ActiveCell.entireclumn.Resize(Rows.Count-2).偏移量(2).选择
选择,复制
Selection.Paste特殊粘贴:=xlPasteValues,操作:=xlNone,SkipBlank_
:=假,转置:=假
设置wb=ActiveWorkbook
工作表(“合并列表”)。选择
设置wsMain=wb.ActiveSheet

设置wsLookup=wb.Sheets(“连接报告”)”使用
Application.Match
(它可以返回
错误
对象)与
WorksheetFunction.Match
(它总是会引发错误)相比,您的做法是正确的

但是由于您的
PriceCol
PriceCol2
变量被强类型化为
Long
,您将得到一个不匹配的错误

使用另一个一次性变量来处理返回:

Dim matchVal as Variant
matchVal = Application.Match("New Muted Price", rng, 0)
If Not IsError(matchVal) Then
    PriceCol2 = matchVal
    ...


Else
    ' if there is no match, you may need to do something else here.

End If
或者,您可以使用
Range.Find
方法:

If Not rng.Find("New Muted Price") Is Nothing
    PriceCol2 = Application.Match("New Muted Price", rng, 0)
Else
    ...

您应该将这两个变量
声明为Variant
,因为如果找不到该值,它们将不是数字,并在继续之前添加if子句以检查是否返回了一个数字。@SRJ,如果pricecol不是空的话。。。在pricecol行之后?@Nic否。pricecol不能是空的,因为它不是一个对象。请稍等,我有一个答案。请尝试
IsNumeric
。否。如果你有一个新问题,问一个新问题。不要继续对已经回答的问题进行忍者编辑。通过编辑您的问题,您已经销毁了原始问题,而此答案现在实际上不是您最初提出的问题的答案。@Nic请不要像这样对您的问题进行变色处理。如果你的代码按预期工作,并且你想让它更高效,那么就转到。哦,对不起,我没有意识到这一点,我会把它带到那里。谢谢我把问题退回到以前的状态。为你将来的问题问一个新问题——问是免费的,你知道:D
If Not rng.Find("New Muted Price") Is Nothing
    PriceCol2 = Application.Match("New Muted Price", rng, 0)
Else
    ...