我有一份每周报告的工作簿。到本周末,它将包含8个工作表,一个用于一周中的每一天,第八个工作表用于报告整周的值。 工作表: 周一,周二,周三,周四,周五,周六,周日,周
每个工作表的格式完全相同,并从模板表中复制。
为了保持简单,我把它煮成一个B12单元格。
B12 on the workheeet" Week"需要平等: =周一!B12 +星期二!B12 +星期三!B12 +星期四!B12 +周五!B12 +周六!B12 +太阳!B12
我打算用VBA将上面的公式插入工作表的单元格B12"周"但是我有几个问题:
1)并非所有日常工作表都存在,例如,如果我仅在周三运行报告"周一"和"星期二"将存在。如果我在周六运行报告,"周一","周二","周三"," Thur"和"周五"将存在。这意味着我不能将上述公式插入到"周"的单元格B12中。因为我得到#REF!由于不存在的工作表而导致的错误。
因此,我需要一些代码,这些代码会将7张日常工作表中的任何一张的所有B12值相加,但仅限于该工作表存在。
因此,如果我在星期三运行报告,它等于: =周一!B12 +星期二!B12
如果我在周六运行报告则等于: =周一!B12 +星期二!B12 +星期三!B12 +周四!B12 +周五!B12
2)我的第二个问题是,这不仅适用于单元格B12,也适用于大约另外100个单元格,并且它们并非全部在一个范围内,它们会分散,有些是虽然我正在谈论B12以试图简化这篇文章,但我希望能够轻松地将解决方案应用于许多其他单元格,而无需写出数百个if语句等。
希望这是有道理的。
这是我到目前为止所处的地方,但它不起作用:
Sub test()
Dim ws As Worksheet
Dim testVar As Double
Dim wsName As String
wsName = ws.Name
For Each ws In ActiveWorkbook.Worksheets
Select Case wsName
Case "Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun"
testVar = testVar + Worksheets(wsName).Range("B12").Value
End Select
Next ws
Worksheets("Week").Range("B12").Value = testVar
End Sub
干杯
答案 0 :(得分:0)
试一试。
Sub test()
Dim ws As Worksheet
Dim testVar As Double
Dim rng As Range
Set rng = Selection
For Each c In rng
For Each ws In ActiveWorkbook.Worksheets
Select Case ws.Name
Case "Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun"
testVar = testVar + ws.Range(c.Address).Value
End Select
Next ws
Worksheets("Week").Range(c.Address).Value = testVar
testVar = 0
Next c
End Sub
您应该可以在“周”工作表中选择任何范围或单个单元格,然后点击宏以获取日常工作表中匹配单元格的总和。
答案 1 :(得分:0)
如果我理解正确,您每天都会有一个包含特定张数的文件,另外还有报告表作为最后一张。
然后你可以使用n = ThisWorkbook.Sheets.Count,它将是纸张总数并得到纸张(1)到纸张(n-1)的总和。
像这样:
x = 0
n = ActiveWorkbook.Sheets.Count
For i = 1 To n - 1
x = x + Sheets(i).Range("B12").Value
Next i
Sheets(n).Range("B12") = x
答案 2 :(得分:0)
好的,我让它工作,但我决定将它用作函数,而不是最小化我需要编写的代码量。
我已经写了以下作为例子:
Function sumWeekly(cellToSum As Range)
Dim ws As Worksheet
Dim cumulativeVar As Double
Dim wsName As String
For Each ws In ActiveWorkbook.Worksheets
wsName = ws.Name
Select Case wsName
Case "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
cumulativeVar = cumulativeVar + Worksheets(wsName).Range(cellToSum.Column & cellToSum.Row).Value
End Select
Next ws
Worksheets("Week").Range(cellToSum.Column & cellToSum.Row).Value = cumulativeVar
End Function
Sub test()
Dim ppcsessions As Range
Set ppcsessions = Worksheets("Mon").Range("B12:B12")
sumWeekly (ppcsessions)
End Sub
但是当我打电话给"测试"我得到一个对象!错误?
答案 3 :(得分:0)
以下是适当功能的代码:
Function sumWeekly(cellToSum As String) As Double
Dim ws As Worksheet
Dim cumulativeVar As Double
For Each ws In ActiveWorkbook.Worksheets
Select Case ws.Name
Case "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
cumulativeVar = cumulativeVar + Worksheets(ws.Name).Range(cellToSum).Value
End Select
Next ws
sumWeekly = cumulativeVar
End Function
然后会这样调用:
Sub test()
Dim ppcsessions() As String
Dim i As Long
'load this array with all the ranges desired.
ppcsessions = Array("B12", "I11", "G13")
For i = LBound(ppcseesions) To UBound(ppcsessions)
Worksheets("WEEK").Range(ppcseesions(i)).Value = sumWeekly(ppcsessions(i))
Next i
End Sub