我写了一个 smartlist 和一个启用了 excel 宏的工作簿,它应该对两列求和,并将总计放在每列下的新行上。它直接从 excel 中完美运行,但是当从 GP Smartlist 导出运行时,它对我的列求和两次。第一次正确地对它们求和,然后将总数加到总和中,在完全不同的行上给我两倍的原始总和。
这是我的代码:
Private Sub sumColumns()
Dim i As Integer
Dim sum1 As Double
Dim sum2 As Double
Dim lastRow As Integer
Dim counter As Integer: counter = 0
'ActiveSheet.UsedRange
If ThisWorkbook.Sheets(2).Cells(1, 1).Value2 = "" Then
ThisWorkbook.Sheets(2).Cells(1, 1).Value2 = GetLastRowOnSheet(ThisWorkbook)
lastRow = ThisWorkbook.Sheets(2).Cells(1, 1).Value2
Else
Exit Sub
End If
For i = 2 To lastRow
sum1 = sum1 + ThisWorkbook.Sheets(1).Cells(i, 11).Value2
sum2 = sum2 + ThisWorkbook.Sheets(1).Cells(i, 12).Value2
Next i
ThisWorkbook.Sheets(1).Cells(lastRow + 2, 11).Value2 = sum1
ThisWorkbook.Sheets(1).Cells(lastRow + 2, 12).Value2 = sum2
End Sub
Function GetLastRowOnSheet(ByVal book As Workbook) As Long
Dim lastRow As Long
With ActiveSheet
GetLastRowOnSheet = Sheets(1).Cells(1048576, 11).End(xlUp).Row
End With
End Function
我相对确定 GP 运行宏两次,但如果有人能帮我解决这个问题,那就太好了。