Excel,VBA代码超过最后一行

时间:2016-01-23 14:17:12

标签: excel excel-vba vba

我很困惑为什么我的代码超过了最后一行。如果我在一个包含30,000行的工作表上运行它,它将填充到大约300k。此表计算我的所有客户交易。很难混淆的是哪种方式是最有效的计算方法,在我使用VBA插入的每一行上使用vlookup函数,或者使用VBA来计算单元格中的总数和显示。这是我的代码:

Sub UPDATE()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
lastRow = Sheets("Closed Trades").Range("A" & Rows.Count).End(xlUp).Row
lastRow2 = Sheets("Open Orders").Range("A" & Rows.Count).End(xlUp).Row
With Sheets("Closed Trades")
    .Range("Q3:Q3" & lastRow).FormulaR1C1 = "=SUBSTITUTE(LEFT(RC[-13],10),""."",""/"")"
    .Range("R3:R3" & lastRow).FormulaR1C1 = "=SUBSTITUTE(LEFT(RC[-9],10),""."",""/"")"
    .Range("S3:S3" & lastRow).FormulaR1C1 = "=VLOOKUP(RC[-13],'Symbols & Spreads'!C[-18]:C[-16],3,FALSE)"
    .Range("T3:T3" & lastRow).FormulaR1C1 = "=VLOOKUP(RC[-14],'Symbols & Spreads'!C[-19]:C[-14],6,FALSE)"
    .Range("U3:U3" & lastRow).FormulaR1C1 = "=VLOOKUP(RC[-15],'Symbols & Spreads'!C[-20]:C[-13],8,FALSE)*RC[-14]"
    .Range("V3:V3" & lastRow).FormulaR1C1 = "=IF(RC[-2]=""eur"",RC[-1]*R6C25,RC[-1]/(VLOOKUP(RC[-2],C[2]:C[3],2,FALSE)))"
End With
With Sheets("Open Orders")
    .Range("T3:T3" & lastRow2).FormulaR1C1 = "=SUBSTITUTE(LEFT(RC[-17],10),""."",""/"")"
    .Range("U3:U3" & lastRow2).FormulaR1C1 = "=VLOOKUP(RC[-16],'Symbols & Spreads'!C[-20]:C[-18],3,FALSE)"
    .Range("V3:V3" & lastRow2).FormulaR1C1 = "=VLOOKUP(RC[-17],'Symbols & Spreads'!C[-21]:C[-16],6,FALSE)"
    .Range("W3:W3" & lastRow2).FormulaR1C1 = "=VLOOKUP(RC[-18],'Symbols & Spreads'!C[-22]:C[-15],8,FALSE)*RC[-17]"
    .Range("X3:X3" & lastRow2).FormulaR1C1 = "=IF(RC[-2]=""eur"",RC[-1]*R6C27,RC[-1]/(VLOOKUP(RC[-2],C[2]:C[3],2,FALSE)))"
End With

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub

2 个答案:

答案 0 :(得分:2)

您必须替换:

.Range("Q3:Q3" & lastRow)

使用:

.Range("Q3:Q" & lastRow)

额外的 3 是问题。

答案 1 :(得分:0)

您可以尝试更改

lastRow = Sheets("Closed Trades").Range("A" & Rows.Count).End(xlUp).Row


lastRow = Sheets("Closed Trades").Range("A1").End(xlDown).Row

<强>

.Range("Q3:Q3" & lastRow).FormulaR1C1 = "=SUBSTITUTE(LEFT(RC[-13],10),""."",""/"")"

.Range("Q3:Q" & lastRow).FormulaR1C1 = "=SUBSTITUTE(LEFT(RC[-13],10),""."",""/"")"