2016-03-22 118 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 
相關問題