在VBA+EXCEL编程模式中,可能需要往EXCEL表中写入公式,下面的代码可以给你一点帮助。
''Address采用相对地址,这样COPY到其它位置后,公式还可以使用。
Dim strRange as string
...........
strRange = "(" & Cells(9, i).Address(RowAbsolute:=False, ColumnAbsolute:=False) & ":" & Cells(r - 1, i).Address(RowAbsolute:=False, ColumnAbsolute:=False) & ")"
Cells(r + 1, i).Formula = "=COUNT" & strRange
Cells(r + 2, i).Formula = "=MAX" & strRange
Cells(r + 3, i).Formula = "=MIN" & strRange
Cells(r + 4, i).Formula = "=AVERAGE" & strRange
If Cells(r + 1, i) < 6 Then
Cells(r + 5, i) = ""
Cells(r + 6, i) = ""
Cells(r + 7, i) = ""
Else
Cells(r + 5, i).Formula = "=STDEV" & strRange
Cells(r + 6, i).Formula = "=" & Cells(r + 5, i).Address & "/" & Cells(r + 4, i).Address
''=1-(1.704/SQRT(AD197)+4.678/AD197^2)*AD202 修正系数
Cells(r + 7, i).Formula = "=1-(1.704/SQRT(" & Cells(r + 1, i).Address & ")+4.678/" & Cells(r + 1, i).Address & "^2)*" & Cells(r + 6, i).Address
End If
''=IF(AD197<6,AVERAGE(AD199,AD200),AD200*AD203) 标准值
Cells(r + 8, i).Formula = "=IF(" & Cells(r + 1, i).Address & "<6,AVERAGE(" & Cells(r + 3, i).Address & "," & Cells(r + 4, i).Address & ")," & Cells(r + 4, i).Address & "*" & Cells(r + 7, i).Address & ")"
i = i + 1
Wend
strRange = Cells(9, n).Address & ":" & Cells(r - 1, k - 1).Address
''设置单元格格式
Range(strRange).NumberFormatLocal = "0.00"
strRange = Cells(r + 2, n).Address & ":" & Cells(r + 8, k - 1).Address
''设置单元格格式
Range(strRange).NumberFormatLocal = "0.00"
MsgBox "Oooooooooooooooooooooooook!"