Excel vba,基于列标题的总和

Excel vba,基于列标题的总和,excel,vba,Excel,Vba,我需要你在VBA方面的帮助! 我想写一个代码,将“销售”列在不同的7页中求和。问题在于,列在每个表中的位置不同,并且有一个dinamic行数。总和应在最后一行+1 我不太擅长宏,但我想我应该从检查I到7页开始。然后,我应该根据标题(“销售”)对范围求和。我不知道怎么写这一切 请尝试下一个代码: Sub SumSales() Dim sh As Worksheet, rngS As Range, lastRow As Long For Each sh In ActiveWorkbook.S

我需要你在VBA方面的帮助! 我想写一个代码,将“销售”列在不同的7页中求和。问题在于,列在每个表中的位置不同,并且有一个dinamic行数。总和应在最后一行+1

我不太擅长宏,但我想我应该从检查I到7页开始。然后,我应该根据标题(“销售”)对范围求和。我不知道怎么写这一切


请尝试下一个代码:

Sub SumSales()
  Dim sh As Worksheet, rngS As Range, lastRow As Long
  For Each sh In ActiveWorkbook.Sheets 'iterate through all sheets
    'find the cell having "Sales" text/value
    Set rngS = sh.Range(sh.Cells(1, 1), sh.Cells(1, _
        sh.Cells(1, Columns.count).End(xlToLeft).Column)).Find("Sales")
    'if the cell has been found (the cell range is NOT Nothing...)
    If Not rngS Is Nothing Then
        'Determine the last row of the found cell column:
        lastRow = sh.Cells(Rows.count, rngS.Column).End(xlUp).row
        'Write the Sum formula in the last empty cell:
        rngS.Offset(lastRow).formula = "=Sum(" & rngS.Offset(1).address & _
             ":" & sh.Cells(lastRow, rngS.Column).address & ")"
        sh.Range("A" & lastRow + 1).Value = "Sum of sales is:"
    Else
        'if any cell has been found, it returns in Immediate Window (Being in VBE, Ctrl + G) the sheet names not having "Sales" header:
        Debug.Print "No ""Sales"" column in sheet """ & sh.name & """."
    End If
  Next
End Sub

哦,天哪,是的!!!!你能解释一下你在这里做了什么吗?特别是IF和ELSE部分……是的。但现在我在开车……;)另外,我还有其他几张和销售无关的表格。我可以订购excel来检查具体的7张表格吗?我不介意在1号的时候按下一个按钮,然后告诉考试来检查下面的6。哈哈,别担心!安全驾驶@gohog69950:注释代码行是为了易于理解(我想…)。我还修改了代码,使其仅适用于某些特定的工作表名称。如果代码回答了问题,我们在此处勾选代码左侧复选框,以使其成为可接受的答案。这样,其他搜索类似内容的人就会知道代码是有效的…:)