VBA往EXCEL的单元格中写入公式

在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!"
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值