在多个工作表中复制VLOOKUP宏

时间:2015-04-14 20:04:37

标签: excel vba vlookup

我从Sheet“P& L”(包含所有数据的第一个选项卡)中记录了一个VLOOKUPs的宏,并在当前工作表中向下过滤,直到A列中的数据用完为止。有用;但是,我需要此代码才能用于剩余的工作表。这些都是每月更新。每张表中的A列将有不同数量的输入。这些都是我用来从P& L选项卡中查看信息的ID#。

当我将此宏写为FoorLoopIndex时,我不断收到“编译错误:无效或不合格”消息。

我对宏没有任何经验 - 我很难找到我的错误。

Sub update_gp_profits()

Dim StartIndex, EndIndex, LoopIndex As Integer
StartIndex = Sheets("P&L").Index + 1
EndIndex = Sheets("Sheet4").Index - 1

For LoopIndex = StartIndex To EndIndex

lastrow = .Range("A" & .Rows.Count).End(xlUp).Row

.Range("B2:B" & lastrow).Formula = "=+VLOOKUP(RC[-1],'P&L'!R15C3:R29702C4,2,FALSE)"
    Range("C2").Select
.Range("C2:C" & lastrow).Formula = "=+VLOOKUP(RC[-2],'P&L'!R15C3:R29702C5,3,FALSE)"
    Range("D2").Select
.Range("D2:D" & lastrow).Formula = "=+VLOOKUP(RC[-3],'P&L'!R15C3:R29702C6,4,FALSE)"
    Range("E2").Select
.Range("E2:E" & lastrow).Formula = "=+VLOOKUP(RC[-4],'P&L'!R15C3:R29702C17,15,FALSE)"
    Range("F2").Select
.Range("F2:F" & lastrow).Formula = "=+VLOOKUP(RC[-5],'P&L'!R15C3:R29702C18,16,FALSE)"
    Range("J2").Select
.Range("k2:k" & lastrow).Formula = "=+VLOOKUP(RC[-10],'P&L'!R15C3:R29702C160,158,FALSE)"
Range("k2").Select

Next LoopIndex
End Sub

2 个答案:

答案 0 :(得分:0)

试试这个,

Sub update_gp_profits()
Dim ws As Worksheet
Dim rng As Range
Dim lRow As Long

Set ws = ActiveSheet
      '
    With ws
            lRow = ws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row ' Last row

            Set rng = .Range("A2" & ":" & "A" & lRow) ' This is your range
                rng.Offset(0, 1).FormulaR1C1 = "=VLOOKUP(RC[-1],'P&L'!R15C3:R29702C4,2,FALSE)"
                rng.Offset(0, 2).FormulaR1C1 = "=VLOOKUP(RC[-2],'P&L'!R15C3:R29702C5,3,FALSE)"
                rng.Offset(0, 3).FormulaR1C1 = "=VLOOKUP(RC[-3],'P&L'!R15C3:R29702C6,4,FALSE)"
                rng.Offset(0, 4).FormulaR1C1 = "=VLOOKUP(RC[-4],'P&L'!R15C3:R29702C17,15,FALSE)"
                rng.Offset(0, 5).FormulaR1C1 = "=VLOOKUP(RC[-5],'P&L'!R15C3:R29702C18,16,FALSE)"
                rng.Offset(0, 10).FormulaR1C1 = "=VLOOKUP(RC[-10],'P&L'!R15C3:R29702C160,158,FALSE)"

            Debug.Print rng.Address
    End With

End Sub

答案 1 :(得分:0)

尝试以下代码,它将循环播放工作表4上的所有行。 2010办公室的最大行数= https://stackoverflow.com/a/527026/1411000 https://stackoverflow.com/a/527026/1411000

Sub update_gp_profits()

Dim StartIndex, EndIndex, LoopIndex As Integer
StartIndex = Sheets("Sheet4").).Index + 1
EndIndex =  1048576    
For LoopIndex = StartIndex To EndIndex

lastrow = .Range("A" & .Rows.Count).End(xlUp).Row

.Range("B2:B" & lastrow).Formula = "=+VLOOKUP(RC[-1],'P&L'!R15C3:R29702C4,2,FALSE)"
    Range("C2").Select
.Range("C2:C" & lastrow).Formula = "=+VLOOKUP(RC[-2],'P&L'!R15C3:R29702C5,3,FALSE)"
    Range("D2").Select
.Range("D2:D" & lastrow).Formula = "=+VLOOKUP(RC[-3],'P&L'!R15C3:R29702C6,4,FALSE)"
    Range("E2").Select
.Range("E2:E" & lastrow).Formula = "=+VLOOKUP(RC[-4],'P&L'!R15C3:R29702C17,15,FALSE)"
    Range("F2").Select
.Range("F2:F" & lastrow).Formula = "=+VLOOKUP(RC[-5],'P&L'!R15C3:R29702C18,16,FALSE)"
    Range("J2").Select
.Range("k2:k" & lastrow).Formula = "=+VLOOKUP(RC[-10],'P&L'!R15C3:R29702C160,158,FALSE)"
Range("k2").Select

Next LoopIndex
End Sub