2016-03-22 115 views
1

我正在尝试使用动态范围的索引匹配公式在工作表“报告”中分配单元格E8。范围是从表“数据”运行时错误5,无效的过程或调用参数

我已经找到最后一行(LR)和最后一列(lc)。在最后一行发生 运行时间错误:细胞(“E8”)式=“= ....”

这是代码:

Sub report() 
    Dim LR As Long, lc As Long, first As Long, proxy As String 

    Sheets("Data").Select 

    'Finding the first filled cell by moving down from A1 
    first = Sheets("Data").Range("A1").End(xlDown).Row 

    'The first row has column headers: Name, ID number, etc... SO I assign it to the next row where the first data entry is 
    first = first + 1 

    LR = Sheets("Data").Range("A" & first).End(xlDown).Row 
    lc = Sheets("Data").Range("A" & first).End(xlToRight).Column 

    Sheets("Report").Select 
    proxy = "=IFERROR(INDEX(Data!$A$10:" & Cells(LR, lc).Address & ",MATCH(Report!$C$3,Data!$A$10:" & Cells(LR, 1).Address & ",0),MATCH(Report!$C8,Data!A$9:" & Cells(9, lc).Address & ",0)),'N/A')" 

    Cells("E8").Formula = proxy 
End Sub 
+0

不应该是'Data!$ A $“&first&”:“&...'而不是'Data!$ A $ 10:”&...'? – Jeeped

回答

0

您正在使用单引号来包装'N/A'。你需要双引号,因为它们是引用字符串中的引号,所以你需要将它们加倍。此外,Range.Cells property不接受与Range object相同的单元格地址引用样式。

proxy = "=IFERROR(INDEX(Data!$A$10:" & Cells(LR, lc).Address & _ 
       ",MATCH(Report!$C$3,Data!$A$10:" & Cells(LR, 1).Address & _ 
       ",0),MATCH(Report!$C8,Data!A$9:" & Cells(9, lc).Address & _ 
       ",0)),""N/A"")" 
Sheets("Report").Select 
Range("E8").Formula = proxy 

这里是一个快速改写,从使用Worksheet.Select¹方法和隐ActiveSheet property逃脱。

Sub report() 
    Dim lr As Long, lc As Long, first As Long, proxy As String 

    With Worksheets("Data") 

     'Finding the first filled cell by moving down from A1 
     first = .Range("A1").End(xlDown).Row 

     'The first row has column headers: Name, ID number, etc... SO I assign it to the next row where the first data entry is 
     first = first + 1 

     lr = .Range("A" & first).End(xlDown).Row 
     lc = .Range("A" & first).End(xlToRight).Column 

    End With 

    proxy = "=IFERROR(INDEX(Data!$A$10:" & Cells(lr, lc).Address & _ 
        ",MATCH(Report!$C$3,Data!$A$10:" & Cells(lr, 1).Address & _ 
        ",0),MATCH(Report!$C8,Data!A$9:" & Cells(9, lc).Address & _ 
        ",0)),""NA"")" 

    With Worksheets("Report") 
     .Range("E8").Formula = proxy 
     'alternate with .Cells as one of these 
     '.Cells(8, "E").Formula = proxy 
     '.Cells(8, 5).Formula = proxy 
    End With 
End Sub 

How to avoid using Select in Excel VBA macros更多的方法从依靠选择越来越远,并激活,以实现自己的目标。

+0

单元格(“E8”)应该用Range替换,因为它似乎是监督错误。 – Sixthsense

+0

感谢您捕捉那第六。我打算回到代码中进行快速重写,并且只在我的叙述中提到了Cells/Range,并且没有在代码中改变它。 – Jeeped

1
Sub report() 
    Dim LR As Long, lc As Long, first As Long, proxy As String 

    With Sheets("Data") 
     'Finding the first filled cell by moving down from A1 
     first = .Range("A1").End(xlDown).Row 

     'The first row has column headers: Name, ID number, etc... SO I assign it to the next row where the first data entry is 
     first = first + 1 

     LR = .Range("A" & Rows.Count).End(xlUp).Row 
     lc = .Range("A" & first - 1).End(xlToRight).Column 
    End With 

    proxy = "=IFERROR(INDEX(Data!$A$10:" & Cells(LR, lc).Address & ",MATCH(Report!$C$3,Data!$A$10:" & Cells(LR, 1).Address & ",0),MATCH(Report!$C8,Data!A$9:" & Cells(9, lc).Address & ",0)),""N/A"")" 
    Sheets("Report").Range("E8").Formula = proxy 
End Sub 
相关问题