我编写了一个智能列表和一个支持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正在运行宏两次,但如果有人能帮助我解决这个问题,那就太好了。