Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.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 vlookup中的类型失配_Vba_Excel_Runtime Error_Vlookup - Fatal编程技术网

Vba vlookup中的类型失配

Vba vlookup中的类型失配,vba,excel,runtime-error,vlookup,Vba,Excel,Runtime Error,Vlookup,我正在尝试执行以下操作: 查看entiresalespipeline中第一行的B列(事件) 将B列与将日期链接到每个事件的表(事件和活动)匹配 如果该日期在将来,请将整行复制到第三个工作表(CurrentSalesPipeline)的下一个空行中 重复此过程,直到第一个电子表格中不再有填充行 我创建了以下代码,当我将鼠标悬停在文本上时,该代码似乎给出了正确的数据,但它给出了以下错误: 运行时错误“13”:类型不匹配 您正在将DateConf声明为Long: Dim DateConf As Lon

我正在尝试执行以下操作:

  • 查看entiresalespipeline中第一行的B列(事件)
  • 将B列与将日期链接到每个事件的表(事件和活动)匹配
  • 如果该日期在将来,请将整行复制到第三个工作表(CurrentSalesPipeline)的下一个空行中
  • 重复此过程,直到第一个电子表格中不再有填充行
  • 我创建了以下代码,当我将鼠标悬停在文本上时,该代码似乎给出了正确的数据,但它给出了以下错误:

    运行时错误“13”:类型不匹配


    您正在将
    DateConf
    声明为
    Long

    Dim DateConf As Long
    
    然后将
    VLookup
    调用的结果分配给它:

    DateConf = Application.VLookup(rCell, Worksheets("Events_and_Activities").Range("A2:B12"), 2, False)
    
    这需要做很多假设:您依靠VBA将返回值隐式转换为
    Long
    ,而不知道返回值是否为有效数字

    如果
    VLookup
    返回一个空字符串怎么办?如果它返回了一个
    #N/A
    错误值怎么办

    Dim lookupResult As Variant
    lookupResult = Application.VLookup(rCell, Worksheets("Events_and_Activities").Range("A2:B12"), 2, False)
    
    If IsNumeric(lookupResult) Then
        DateConf = CLng(lookupResult)
        ...
    End If
    

    减少你所做的假设的数量,你将减少潜在问题的数量。

    谢谢大家的深思熟虑和非常有用的意见! 经过一些修补,我终于想出了下面的方法,它完成了我需要它做的一切

    Mat的杯子-你的建议太完美了! Tim Williams-我尝试了复制/粘贴的单行解决方案,但它只在范围内的每一行上复制了第一行(标题),所以我坚持使用我所拥有的

    Sub ShowUpcoming_Click()
    Dim lastrow As Long
    Dim ws As Worksheet
    Dim DateConf As Long
    Dim r As Long
    Set ws = Worksheets("CurrentSalesPipeline")
    Dim lookupresult  As Variant
    'find last completed row of entire spreadsheet
    lastrow = Worksheets("EntireSalesPipeline").Range("B" & Rows.Count).End(xlUp).Row
    'From the first completed line (row 3) to last completed row
        For r = 3 To lastrow
            'lookup conference date from events/activities spreadsheet
        lookupresult = Application.VLookup(Worksheets("EntireSalesPipeline").Cells(r, 2).Value, Worksheets("Events_and_activities").Range("A2:B13"), 2, False)
            If IsNumeric(lookupresult) Then
            DateConf = CLng(lookupresult)
            End If
        'If vlookup finds a date, then check to make sure it is in the future from when the button was hit.
        If CDate(DateConf) >= CDate((Date)) Then
        'If it is a future event, then copy that data into the current spreadsheet
                iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
                SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
                With ws
                    .Cells(iRow, 1).Value = Worksheets("entiresalespipeline").Cells(r, 1).Value
                    .Cells(iRow, 2).Value = Worksheets("entiresalespipeline").Cells(r, 2).Value
                    .Cells(iRow, 3).Value = Worksheets("entiresalespipeline").Cells(r, 3).Value
                    .Cells(iRow, 4).Value = Worksheets("entiresalespipeline").Cells(r, 4).Value
                    .Cells(iRow, 5).Value = Worksheets("entiresalespipeline").Cells(r, 5).Value
                    .Cells(iRow, 6).Value = Worksheets("entiresalespipeline").Cells(r, 6).Value
                    .Cells(iRow, 7).Value = Worksheets("entiresalespipeline").Cells(r, 7).Value
                    .Cells(iRow, 8).Value = Worksheets("entiresalespipeline").Cells(r, 8).Value
                    .Cells(iRow, 9).Value = Worksheets("entiresalespipeline").Cells(r, 9).Value
                    .Cells(iRow, 10).Value = Worksheets("entiresalespipeline").Cells(r, 10).Value
                    .Cells(iRow, 11).Value = Worksheets("entiresalespipeline").Cells(r, 11).Value
                    .Cells(iRow, 12).Value = Worksheets("entiresalespipeline").Cells(r, 12).Value
                    .Cells(iRow, 13).Value = Worksheets("entiresalespipeline").Cells(r, 13).Value
                    .Cells(iRow, 14).Value = Worksheets("entiresalespipeline").Cells(r, 14).Value
                    .Cells(iRow, 15).Value = Worksheets("entiresalespipeline").Cells(r, 15).Value
                    .Cells(iRow, 16).Value = Worksheets("entiresalespipeline").Cells(r, 16).Value
                    .Cells(iRow, 17).Value = Worksheets("entiresalespipeline").Cells(r, 17).Value
                    .Cells(iRow, 18).Value = Worksheets("entiresalespipeline").Cells(r, 18).Value
                    .Cells(iRow, 19).Value = Worksheets("entiresalespipeline").Cells(r, 19).Value
                    .Cells(iRow, 20).Value = Worksheets("entiresalespipeline").Cells(r, 20).Value
                End With
        End If
    'Repeat for next line in existing
    Next r
    End Sub
    
    这是一个伟大的社区。再次感谢你!
    Sarah

    你说的“但这给了我以下的错误”是什么意思?你到底在哪里看到了错误?是否有特定的代码行给出了错误,或者您在其他地方看到了错误?1。你不能确认VLookup返回了一个日期。2) 您尝试使用CDate(DateConf)将未知返回值转换为日期。错误显示了导致错误的确切原因,该行上的断点将显示错误。与当前问题无关,但复制大量值可以用一行替换:
    ws.Cells(iRow,1)。Resize(1,20)。value=Worksheets(“entiresalespipeline”)。Range(“A1”)。Resize(1,20).Value
    如果您能使用该代码,我建议您使用它,因为有许多方法可以显著提高可扩展性、可读性甚至性能。干杯
    Sub ShowUpcoming_Click()
    Dim lastrow As Long
    Dim ws As Worksheet
    Dim DateConf As Long
    Dim r As Long
    Set ws = Worksheets("CurrentSalesPipeline")
    Dim lookupresult  As Variant
    'find last completed row of entire spreadsheet
    lastrow = Worksheets("EntireSalesPipeline").Range("B" & Rows.Count).End(xlUp).Row
    'From the first completed line (row 3) to last completed row
        For r = 3 To lastrow
            'lookup conference date from events/activities spreadsheet
        lookupresult = Application.VLookup(Worksheets("EntireSalesPipeline").Cells(r, 2).Value, Worksheets("Events_and_activities").Range("A2:B13"), 2, False)
            If IsNumeric(lookupresult) Then
            DateConf = CLng(lookupresult)
            End If
        'If vlookup finds a date, then check to make sure it is in the future from when the button was hit.
        If CDate(DateConf) >= CDate((Date)) Then
        'If it is a future event, then copy that data into the current spreadsheet
                iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
                SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
                With ws
                    .Cells(iRow, 1).Value = Worksheets("entiresalespipeline").Cells(r, 1).Value
                    .Cells(iRow, 2).Value = Worksheets("entiresalespipeline").Cells(r, 2).Value
                    .Cells(iRow, 3).Value = Worksheets("entiresalespipeline").Cells(r, 3).Value
                    .Cells(iRow, 4).Value = Worksheets("entiresalespipeline").Cells(r, 4).Value
                    .Cells(iRow, 5).Value = Worksheets("entiresalespipeline").Cells(r, 5).Value
                    .Cells(iRow, 6).Value = Worksheets("entiresalespipeline").Cells(r, 6).Value
                    .Cells(iRow, 7).Value = Worksheets("entiresalespipeline").Cells(r, 7).Value
                    .Cells(iRow, 8).Value = Worksheets("entiresalespipeline").Cells(r, 8).Value
                    .Cells(iRow, 9).Value = Worksheets("entiresalespipeline").Cells(r, 9).Value
                    .Cells(iRow, 10).Value = Worksheets("entiresalespipeline").Cells(r, 10).Value
                    .Cells(iRow, 11).Value = Worksheets("entiresalespipeline").Cells(r, 11).Value
                    .Cells(iRow, 12).Value = Worksheets("entiresalespipeline").Cells(r, 12).Value
                    .Cells(iRow, 13).Value = Worksheets("entiresalespipeline").Cells(r, 13).Value
                    .Cells(iRow, 14).Value = Worksheets("entiresalespipeline").Cells(r, 14).Value
                    .Cells(iRow, 15).Value = Worksheets("entiresalespipeline").Cells(r, 15).Value
                    .Cells(iRow, 16).Value = Worksheets("entiresalespipeline").Cells(r, 16).Value
                    .Cells(iRow, 17).Value = Worksheets("entiresalespipeline").Cells(r, 17).Value
                    .Cells(iRow, 18).Value = Worksheets("entiresalespipeline").Cells(r, 18).Value
                    .Cells(iRow, 19).Value = Worksheets("entiresalespipeline").Cells(r, 19).Value
                    .Cells(iRow, 20).Value = Worksheets("entiresalespipeline").Cells(r, 20).Value
                End With
        End If
    'Repeat for next line in existing
    Next r
    End Sub