首页 > 解决方案 > 在工作簿中循环通过 Excel 工作表

问题描述

我每天导入我的主表,并在我的主表底部计算总数。下面我计算平均值。然后我在 A 列和 B 列的底部执行另外两个平均计算。然后我根据市场拆分我的表,然后我的程序为每个市场打开一个新选项卡。然后我设法拆分我的表,以便数据进入正确的选项卡。我现在想在每个市场上执行相同的总和平均计算。Marketplaces 是一个动态值 - 如果在 8 个市场上有销售,该程序可能会打开 8 个,或者如果....等则打开 5 个。此代码执行总和和平均行计算,但不执行 A 和 B 列底部的其他平均计算。我无法让它工作。

更新....我已经解决了这部分问题 - 我没有意识到有 sheet.count 功能

此外,由于工作簿中的工作表数量是动态的,并且我想执行从工作表 3 到工作簿末尾的计算,所以我使用 n 作为变量并在其中有一个 for n = 3 to 10 语句,但必须有是一种将工作簿中的工作表数分配给 n 的方法吗?

Sub CalcOnSheets()

Application.ScreenUpdating = False
Dim row As Integer
Dim lastRow As Long
Dim ActiveWorksheet As Long
Dim ThisWorksheet As Long
Dim n As Integer



'I've put 3 to 10 here because I don't know how to count the actual number of sheets that I want to 
'run this for Is there a variable which is the count of the sheets in the workbook, as this value is 
'dynamic?

For n = 3 To 10
lastRow = Sheets(n).Cells(Rows.Count, "D").End(xlUp).row
If lastRow > 1 Then
For row = 2 To lastRow

'I've got row 4 in here so the calculations stop performing
'when the iteration gets down to the blank row under the sheet
'but I'm not sure if it's necessary. Could maybe take out the if and the end if

If Sheets(n).Cells(row, 4).Value <> "" Then

Sheets(n).Cells(row, 16).Value = Sheets(n).Cells(row, 10).Value - Sheets(n).Cells(row, 11).Value - _
Sheets(n).Cells(row, 12).Value - Sheets(n).Cells(row, 13).Value - Sheets(n).Cells(row, 14).Value - _
Sheets(n).Cells(row, 15).Value
Sheets(n).Cells(row, 17).Value = Sheets(n).Cells(row, 16).Value / Sheets(n).Cells(row, 10).Value

End If


Next

Dim r As range, j As Long, k As Long
j = Sheets(n).range("A1").End(xlToRight).Column



'This adds up the totals on the sheet
'changing the first value of k stops it adding up un-needed columns
 For k = 9 To j
 Set r = Sheets(n).range(Sheets(n).Cells(1, k), Sheets(n).Cells(1, k).End(xlDown))
 r.End(xlDown).Offset(2, 0) = WorksheetFunction.Sum(r)
 r.End(xlDown).Offset(3, 0) = WorksheetFunction.Average(r)
 Next k


'This deletes the total of the profit margins whch is meaningless
Sheets(n).range("Q" & lastRow).Offset(2) = ""


'This changes the format of column I average value back to two decimal places
 Sheets(n).range("I" & lastRow).Offset(3).NumberFormat = "0.00_);(0.00)"


'These don't work....
'This calculates Average profit per unit
'Sheets(n).range("B" & lastRow).Offset(2, 0).Value = Sheets(n).range("P" & lastRow).Offset(2).Value / Sheets(n).range("I" 
& lastRow).Offset(2).Value
'This calculates Average profit per order
'Sheets(n).range("A" & lastRow).Offset(2, 0).Value = Sheets(n).range("P" & lastRow).Offset(2).Value / Sheets(n).range("D2").End(xlDown).row



Else

MsgBox ("There is no data at column D")
End If

Application.ScreenUpdating = True


Next n

End Sub

我对 VBA 非常缺乏经验,只是在寻找解决方法。我还想知道是否有一种简单的方法来加宽某些列以便我适合(第 10 到 17 列)

标签: excelvba

解决方案


我发现了 sheet.count 函数,它帮助我处理我正在努力解决的动态工作表数量:

Dim x As Integer
x = Sheets.Count
For n = 3 To x
next x

我还发现我在这一行有一个错误:

range("I" & lastRow).Offset(2).Value

应该是:

Sheets(n).range("I" & lastRow).Offset(2).Value

推荐阅读