错误400消息 - 不知道如何解决公式问题

时间:2015-07-22 09:08:14

标签: excel vba excel-vba compiler-errors

您好我已经通过查看本网站上的帖子整理了这个VBA,这使我能够在一张表格中为所有值运行公式。但是每次运行它都会收到错误400消息。我在哪里做错了另外如何使这个vba工作使用由另一张纸上的列确定的长度。

Sub FillWorksheet1()

Application.ScreenUpdating = False

lastRow = Range("B" & Rows.Count).End(xlUp).Row

Range("AF2").Formula = "=firstPart($G2)"
Range("AF2").AutoFill Destination:=Range("AF2:AF" & lastRow)

Range("AG2").Formula = "=VLOOKUP($AF2,'Naming Lookup'!$A$2:$G$10815,2,FALSE)"
Range("AG2").AutoFill Destination:=Range("AG2:AG" & lastRow)

Range(AH2).Formula = "=VLOOKUP($AF2,'Naming Lookup'!$A$2:$G$10815,3,FALSE)"
Range("AH2").AutoFill Destination:=Range("AH2:AH" & lastRow)

Range(AI2).Formula = "=VLOOKUP($AF2,'Naming Lookup'!$A$2:$G$10815,4,FALSE)"
Range("AI2").AutoFill Destination:=Range("AI2:AI" & lastRow)

Range(AJ2).Formula = "=VLOOKUP($AF2,'Naming Lookup'!$A$2:$G$10815,5,FALSE)"
Range("AJ2").AutoFill Destination:=Range("AJ2:AJ" & lastRow)

Range(AK2).Formula = "=VLOOKUP($AF2,'Naming Lookup'!$A$2:$G$10815,6,FALSE)"
Range("AK2").AutoFill Destination:=Range("AK2:AK" & lastRow)

Range(AL2).Formula = "=VLOOKUP($AF2,'Naming Lookup'!$A$2:$G$10815,7,FALSE)"
Range("AL2").AutoFill Destination:=Range("AL2:AL" & lastRow)
ActiveSheet.AutoFilterMode = False

Application.ScreenUpdating = True

End Sub

非常感谢任何帮助。

firstPart公式是由VBA创建的,用于删除数字后面的文字,以便不仅收集客户特定的产品代码。

2 个答案:

答案 0 :(得分:4)

我会这样做:

Sub FillWorksheet1()
    Dim lastRow               As Long
    Dim lCalc                 As XlCalculation

    With Application
        .ScreenUpdating = False
        lCalc = .Calculation
        .Calculation = xlCalculationManual
    End With

    lastRow = Sheets("some other sheet").Range("B" & Rows.Count).End(xlUp).Row

    Range("AF2").Formula = "=firstPart($G2)"
    Range("AG2").Formula = "=MATCH($AF2,'Naming Lookup'!$A$2:$A$10815,0)"
    Range("AH2:AM2").Formula = "=INDEX('Naming Lookup'!B$2:B$10815,$AG2)"
    Range("AF2:AM2").AutoFill Destination:=Range("AF2:AM" & lastRow)
    ActiveSheet.AutoFilterMode = False

    With Application
        .Calculation = lCalc
        .ScreenUpdating = True
    End With

End Sub

答案 1 :(得分:0)

如上所述,报价缺失。

您还可以通过为每个公式使用一行来使代码更具可读性。 例如:

Dim LastRow As Long

LastRow = Range("B" & Rows.Count).End(xlUp).Row

Range("AF2:AF" & LastRow) = "=firstPart($G2)"
Range("AG2:AG" & LastRow) = "=VLOOKUP($AF2,'Naming Lookup'!$A$2:$G$10815,2,FALSE)"
Range("AH2:AH" & LastRow) = "=VLOOKUP($AF2,'Naming Lookup'!$A$2:$G$10815,3,FALSE)"
Range("AI2:AI" & LastRow) = "=VLOOKUP($AF2,'Naming Lookup'!$A$2:$G$10815,4,FALSE)"
'ect