|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
我想通过VBA实现如下功能,如图:
表中cells(3,2)和cells(4,2)是两个输入量(给出初始量),各列都被worksheet函数编辑过,经运算得到最后一列(H11:H27)是误差,我的目的是想通过给定一个范围和步长(通过两个for循环)来找出使H11:H27的平方和最小的数值(反映在cells(3,2)和cells(4,2))。cells(3,3)是用来监控平方和的计算结果的。但现在运行下面的代码,cells(3,2)和cells(4,2)和cells(3,3)还有H11:H27没有变化。我搞不明白了,所以求给位高手帮忙看看,我才接触VBA,因为论坛中高手的帮助,我得以很快入门,非常感谢给位的帮助
代码如下
Sub baichang()
Dim sumtol As Double, temp As Double, Lz0 As Double, Ly0 As Double, zstep As Double, ystep As Double
Set myRange1 = Worksheets("Sheet1").Range("H11:H27")
Set myRange2 = Worksheets("Sheet1").Range("G32:G45")
sumtol = Application.WorksheetFunction.SumSq(myRange1)+Application.WorksheetFunction.SumSq(myRange2)
Worksheets(1).Cells(3, 3).Value=sumtol
For zstep = -30 To 30 Step 0.01
Lz0 = Worksheets(1).Cells(3, 2).Value
Lz0 = Lz0+zstep
For ystep = -20 To 20 Step 0.01
Ly0 = Worksheets(1).Cells(4, 2).Value
Ly0 = Ly0+ystep
Application.ScreenUpdating = True
temp = Application.WorksheetFunction.SumSq(myRange1)+Application.WorksheetFunction.SumSq(myRange2)
If sumtol - temp>0 Then
Worksheets(1).Cells(3, 3).Value=temp
sumtol = temp
Worksheets(1).Cells(3, 2).Value=Lz0
Worksheets(1).Cells(4, 2).Value=Ly0
EndIf
If ystep=20 Then
Exit For
EndIf
Next
If zstep=30 Then
Exit for
Endif
Next
End Sub
该贴已经同步到 Dolza的微博 |
|