|
楼主 |
发表于 2011-8-17 11:19
|
显示全部楼层
代码调试与优化
本帖最后由 shanchuan 于 2011-8-20 15:30 编辑
Sub sheet4明细0()
Dim n%
Dim sht4 As Worksheet, sht1 As Worksheet
Set sht4 = ThisWorkbook.Worksheets("sheet4")
Set sht1 = ThisWorkbook.Worksheets("sheet1")
For n = 9 To 200
sht4.Cells(n - 7, 1) = sht1.Cells(3, n)
sht4.Cells(n - 7, 3) = Application.WorksheetFunction.SumIf(sht1.Range("G$4:$G$526").Offset(0, n - 7), "<>", sht1.Range("$E$4:$E$526"))
sht4.Cells(n - 7, 4) = Application.WorksheetFunction.SumIf(sht1.Range("G$526:$G$2000").Offset(0, n - 7), "<>", sht1.Range("$E$526:$E$2000"))
'这一句就是4天的计划数量,是通过手工更改行数(大于等于当前日期的行数,比如526)来实现的,每过一天,需要重新改一次,现在想通过增加一个判断日期一列大于当前日期的条件来实现自动计算。
Next
End Sub
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
sht4.Cells(n - 7, 4) = Application.WorksheetFunction.SumProduct((sht1.Range("G$4:$G$654").Offset(0, n - 7) <> "") * (Day(sht1.Range("$C$4:$C$654")) > Day(Now)), (sht1.Range("$E$4:$E$654")))
想法是将上面程序的2个条件求和变为三个条件求和,但是可能第三个条件表达的语句有问题,运行显示“类型不匹配” 请诊断
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
错误“1004” 应用程序定义或对象定义错误。
Sub sheet4明细1()
Dim n%
Dim sht4 As Worksheet, sht1 As Worksheet
Set sht4 = ThisWorkbook.Worksheets("sheet4")
Set sht1 = ThisWorkbook.Worksheets("sheet1")
For i = 730 To 2000
If sht1.Cells(i, 3) <> "上线日期" And sht1.Cells(i, 3) > Date Then ‘想法是 由于 将三个条件求和中的大于当前日期一句一直调试不成。退而求其次考虑将这个条件移出,提前判断出行号。
k = i
GoTo line1
End If
Next
line1:
For n = 9 To 180
sht4.Cells(n - 7, 1) = sht1.Cells(3, n)
sht4.Cells(n - 7, 3) = Application.WorksheetFunction.SumIf(sht1.Range(Cells(4, 7), Cells(k - 1, 7)).Offset(0, n - 7), "<>", sht1.Range(Cells(4, 5), Cells(k - 1, 5))) ’有时候成功,有时候运行到这里报错:运行时错误“1004” 应用程序定义或对象定义错误。
Next
End Sub
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
运行成功
Sub sheet4明细2()
Dim n%
Dim k
Dim i
Dim sht4 As Worksheet, sht1 As Worksheet
Set sht4 = ThisWorkbook.Worksheets("sheet4")
Set sht1 = ThisWorkbook.Worksheets("sheet1")
For i = 737 To 2000
If sht1.Cells(i, 3) <> "上线日期" And sht1.Cells(i, 3) > Date Then ’得到的k=i是大于当前日期的最上面一行的行号。如何能得到第三列中最新最大日期的最上面一行的行号?
k = i
GoTo line1
End If
Next
line1:
For n = 9 To 180
sht4.Cells(n - 7, 1) = sht1.Cells(3, n)
sht4.Cells(n - 7, 2) = Application.WorksheetFunction.SumIf(sht1.Cells(4, n).Resize(k - 3, 1), "<>", sht1.Range("E4:E" & k - 1))
sht4.Cells(n - 7, 3) = Application.WorksheetFunction.SumIf(sht1.Range(Cells(k, 7).Address, Cells(2000, 7).Address).Offset(0, n - 7), "<>", sht1.Range(Cells(k, 5).Address, Cells(2000, 5).Address))
Next
End Sub
|
|