|
本帖最后由 jordanlive 于 2024-7-27 16:09 编辑
求VB运算程序;在一个工作表中,第七行开始J列=H列减-N列 ,L列等于R列:BV列总和, M列等于=L列除以/(I列加H列) ,O列=N7除以H7
编了几组代码,计算时反映速度慢,请高手指点修正代码
Sub SubtractHFromNAndPlaceInJ()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("日报表") ' 修改为你的工作表名
Dim lastRow As Long
lastRow = ws.Cells(ws.Rows.Count, "H").End(xlUp).Row ' 获取H列最后一个非空单元格的行号
lastRow = ws.Cells(ws.Rows.Count, "i").End(xlUp).Row ' 获取H列最后一个非空单元格的行号
Dim i As Long
For i = 7 To lastRow ' 假设你的数据从第2行开始
ws.Cells(i, "J").Value = ws.Cells(i, "H").Value - ws.Cells(i, "N").Value
ws.Cells(i, "k").Value = ws.Cells(i, "i").Value - ws.Cells(i, "p").Value
Dim sum As Double
Dim sum1 As Double
Dim sum2 As Double
sum = Application.WorksheetFunction.sum(ws.Range("R" & i & ":BV" & i))
sum1 = Application.WorksheetFunction.sum(ws.Range("R" & i & ":U" & i))
sum2 = Application.WorksheetFunction.sum(ws.Range("V" & i & ":BV" & i))
' 将累加值写入"L"列
ws.Cells(i, "L").Value = sum
ws.Cells(i, "N").Value = sum1
ws.Cells(i, "P").Value = sum
Next i
End Sub
|
|