Excel VBA偏移量
初始问题: 因此,我: int001,int001,…int024 和29个独特的数据 我觉得我做错了什么:Excel VBA偏移量,vba,Vba,初始问题: 因此,我: int001,int001,…int024 和29个独特的数据 我觉得我做错了什么: ) * 24 + 1) ...24 times....* 24 + 24) 但我不知道是什么 Dim iDate As Long With Range("C1") For iDate = 1 To nr_unique_dates - 1 .Offset((iDate - 1) * 24 + 1) = Application.WorksheetFuncti
) * 24 + 1) ...24 times....* 24 + 24)
但我不知道是什么
Dim iDate As Long
With Range("C1")
For iDate = 1 To nr_unique_dates - 1
.Offset((iDate - 1) * 24 + 1) = Application.WorksheetFunction.SumIfs(INT001, dates, unique_dates(iDate), INT001, ">0")
.Offset((iDate - 1) * 24 + 2) = Application.WorksheetFunction.SumIfs(INT002, dates, unique_dates(iDate), INT002, ">0")
|
|
|
|
.Offset((iDate - 1) * 24 + 22) = Application.WorksheetFunction.SumIfs(INT022, dates, unique_dates(iDate), INT022, ">0")
.Offset((iDate - 1) * 24 + 23) = Application.WorksheetFunction.SumIfs(INT023, dates, unique_dates(iDate), INT023, ">0")
.Offset((iDate - 1) * 24 + 24) = Application.WorksheetFunction.SumIfs(INT024, dates, unique_dates(iDate), INT024, ">0")
Next iDate
在关于输出零的OP请求后编辑 编辑后2关于输出零的OP澄清 如果您的其余代码与链接代码中的代码保持一致,那么您将使用带有范围(“C1”)的
而不是带有范围(“H1”)
的原始,因为C1就在intXXX数据中
由于intXXX的24列范围从“B”列到“Y”列,您应该使用和范围(“Z1”)
(或者,为了使两列远离数据,和范围(“AA1”)
)
您应该完成这样的修改,但我建议您遵循@JohnColeman关于“数组方法”的建议
以下是开放数量intXXX的“范围方法”:
选项显式
副秘书长()
将唯一日期作为范围,输出作为范围
Dim nr_唯一_日期尽可能长
尺寸与长度相同,长度与长度相同,长度与长度相同
有了工作表(“TSUM”),你能说得更清楚些吗?你的代码有什么问题?有错误吗?什么不工作?“int001,int001,…int024”?你熟悉阵列吗?@Marius:你通过了吗?谢谢,它很有效。但是当我在一列中只有负值时,总和被设置为0,在这种情况下,我不需要设置任何值。2/1/2016
10 6 4
2/2/2016
-5 3 2
2/3/2016
3 7 3
2/1/20165
2/2/2016
-4 3 7
2/3/2016
6 4 2
在这种情况下,日期为2/2/2016的int 1列中只有负数。我不希望正数之和之间有空格,我希望正数之和的连续。当我的列中有负值时,你能用if来跳转算法吗?当我有和的时候
Option Explicit
Sub Tsum2()
Dim unique_dates As Range, outputRng As Range
Dim nr_unique_dates As Long
Dim iDate As Long, nr_Cols As Long, iCol As Long
With Worksheets("TSums") '<-- change "TSums" with your actual sheet name
Set outputRng = Cells(1, .Columns.Count).End(xlToLeft).Offset(, 2) '<--| set output range as tow columns right of the last non empty cell in row 1
With .Range("G1", .Cells(.Rows.Count, 1).End(xlUp)) '<-- reference the range from cell "A1" to cell in column "G" corresponding to last non empty row in column "A"
nr_Cols = .Columns.Count '<--| store data columns
.Columns(1).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=outputRng, Unique:=True '<-- call AdvancedFilter on first column of the referenced range
Set unique_dates = Range(outputRng.Offset(1), outputRng.End(xlDown)) '<--| set unique dates range
nr_unique_dates = unique_dates.Count '<--| store unique dates number
With .Offset(1).Resize(.Rows.Count - 1) '<--| skip headers of reference range
For iDate = 1 To nr_unique_dates '<--| outer loop through unique dates
For iCol = 2 To nr_Cols '<--| inner loop through "int" columns (from column 2 rightwards)
outputRng.Offset((iDate - 1) * (nr_Cols - 1) + iCol - 1, 2) = Application.WorksheetFunction.SumIfs(.Columns(iCol), .Columns(1), unique_dates(iDate), .Columns(iCol), ">0")
Next iCol
Next iDate
End With
End With
End With
With outputRng.Offset(, 2)
.SpecialCells(xlCellTypeConstants).Replace 0, "" '<--| substitute all output "zeros" with a blank value
.SpecialCells(xlCellTypeBlanks).Delete '<--| delete all blanks
End With
End Sub