我有一个宏,该宏将每周的数据导入到一个新列中,然后对其进行许多操作。我很难对过去十周的数据进行汇总。显然,公式不起作用,因为每周插入新列时,公式都不会移动以包括新列并删除第11列。
我为此编写的代码是:
Dim h As Range
Dim preCol As Long
With wsBOS.Rows(7)
Set h = .Find("Total", LookIn:=xlValues)
If Not h Is Nothing Then
preCol = h.Column - 1
End If
End With
For jCombo = 1 To 175
Dim siteCombo As String
siteCombo = ThisWorkbook.Sheets("Results Sheet").Cells(jCombo, 3)
If ((siteCombo = "Bone & Connective Tissue") Or (siteCombo = "Brain/CNS") Or (siteCombo = "Breast") Or (siteCombo = "GI") Or (siteCombo = "Gland/Lymphatic") Or (siteCombo = "GYN") _
Or (siteCombo = "Head & Neck") Or (siteCombo = "Leukemia Lymphoma") Or (siteCombo = "Lung") Or (siteCombo = "Gu") Or (siteCombo = "GU") Or (siteCombo = "Male") _
Or (siteCombo = "Metastasis Genital Organ") Or (siteCombo = "Other") Or (siteCombo = "Skin")) Then
ThisWorkbook.Sheets("Results Sheet").Cells(jCombo, preCol - 2).Value = Application.Sum(Range(Cells(jCombo, preCol - 11), (Cells(jCombo, preCol - 3))))
End If
Next jCombo
其中jCombo向下递增行,而preCol引用新创建的列。
无论出于何种原因,此代码段在运行时都无济于事。它不会引发任何错误,只会使所有175行preCol保持不变。我很沮丧,正在向您寻求帮助。
如果任何人有任何想法并希望分享,我将不胜感激。谢谢!!
preCol是指列OL。
我正在尝试获取要填充在OJ列中的值。
答案 0 :(得分:2)
将其放在第8行中并向下复制:
=SUM(INDEX(8:8,COLUMN()-1):INDEX(8:8,COLUMN()-10))
由于没有列引用,因此它将始终查看第8行的最后10列。向下拖动时,8:8将更改为下一行。
答案 1 :(得分:1)
这似乎对我有用
Option Explicit
Sub Dynamic_Duo()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim LC As Long, LR As Long, SumRange As String
LC = ws.Cells(6, ws.Columns.Count).End(xlToLeft).Offset.Column
LR = ws.Range("B" & ws.Rows.Count).End(xlUp).Offset(-2).Row
ws.Cells(6, LC + 1) = "10 Week"
ws.Cells(7, LC + 1) = "Total"
SumRange = ws.Range(ws.Cells(8, LC - 9), ws.Cells(8, LC)).Address(False, False)
ws.Range(ws.Cells(8, LC + 1), ws.Cells(LR, LC + 1)).Formula = "=Sum(" & SumRange & ")"
MsgBox "@Scott Craner's solution is better", vbCritical
End Sub