|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
现在不能截图就直接上代码。反正根据原表意思写的。代码有点啰嗦:
Sub TEST()
Set d = CreateObject("scripting.dictionary")
Set d1 = CreateObject("scripting.dictionary")
Set d2 = CreateObject("scripting.dictionary")
With Sheets("GHZLKJ_平差")
ar = .[a1].CurrentRegion
End With
'==========================================以下工作表为过渡用或者另用也可===================================
'=========================================================================================================
With Sheets("结果")
.[a1].Resize(UBound(ar), 1).NumberFormatLocal = "@"
.[c1].Resize(UBound(ar), 1).NumberFormatLocal = "_0.00"
.[a1].Resize(UBound(ar), UBound(ar, 2)) = ar
.[a1].Resize(UBound(ar), UBound(ar, 2)).Offset(1, 3).ClearContents
.[h1].Resize(UBound(ar), 1) = Application.WorksheetFunction.Index(ar, 0, 8)
.[a1].CurrentRegion.Sort key1:=.[a2], order1:=2, key2:=.[d2], order2:=2, Header:=xlYes
ar = .[a1].CurrentRegion
For i = 2 To UBound(ar)
d(ar(i, 1)) = d(ar(i, 1)) + ar(i, 3) '拆分后面积
Next i
For i = 2 To UBound(ar)
按比例分配面积 = ar(i, 2) * ar(i, 3) / d(ar(i, 1))
ar(i, 4) = WorksheetFunction.Round(按比例分配面积, 2)
d1(ar(i, 1)) = d1(ar(i, 1)) + ar(i, 4) '按比例分配面积
Next i
For i = 2 To UBound(ar)
ar(i, 5) = WorksheetFunction.Round((ar(i, 2) - d1(ar(i, 1))), 2) '原面积-按比例分配面积 [即残差]
If Not d2.exists(ar(i, 1)) Then
ar(i, 6) = ar(i, 5)
d2(ar(i, 1)) = Int(ar(i, 5) * 100) 'WorksheetFunction.RoundUp(ar(i, 5), 2) '待分配残值
End If
'=======================根据残值进行分配===========================
' ar(i, 5) = Int((ar(i, 2) - d1(ar(i, 1))) * 100) / 100
' '发现0.03分配不均,固将残值修为放大100倍后取整
If d2(ar(i, 1)) > 0 Then
ar(i, 9) = ar(i, 4) + 0.01
d2(ar(i, 1)) = d2(ar(i, 1)) - 1
ar(i, 7) = 0.01
ElseIf d2(ar(i, 1)) < 0 Then
ar(i, 9) = ar(i, 4) - 0.01
d2(ar(i, 1)) = d2(ar(i, 1)) + 1
ar(i, 7) = -0.01
ElseIf d2(ar(i, 1)) = 0 Then
ar(i, 9) = ar(i, 4)
End If
'==================================================================
Next i
Sheets("结果").[a1].Resize(UBound(ar), UBound(ar, 2)) = ar
d.RemoveAll
d1.RemoveAll
d2.RemoveAll
For i = 2 To UBound(ar)
d(ar(i, 1) & "##" & ar(i, 3)) = ar(i, 9)
Next i
End With
'=========================================================================================================
'=========================================================================================================
'原表写入VBA平差面积==================================
br = Sheets("GHZLKJ_平差").[a1].CurrentRegion
For i = 2 To UBound(br)
br(i, 9) = d(br(i, 1) & "##" & br(i, 3))
Next i
Sheets("GHZLKJ_平差").[i1].Resize(UBound(br), 1) = Application.WorksheetFunction.Index(br, 0, 9)
d.RemoveAll
'====================================================
End Sub
|
|