在表格中获取重心

时间:2019-02-26 22:49:05

标签: excel vba algorithm math

Excel table

我有5x5的表格和25个随机整数。我需要计算桌子的重力。

我花了几个小时试图弄清楚this代码是如何工作的,它是用scala编写的,我试图将其更改为VBA,但是没有任何运气。 这是我的最佳尝试,仍然是不正确的,而不是生成我手动输入的数字然后输入到excel中,这对我来说已经足够了。

Excel table

Sub calcGravity()
'get sum
For i = 1 To 5
    For j = 1 To 5
    Sum = Sum + Cells(i, j).Value
    Next j
Next i

For i = 1 To 4
    For j = 1 To 4
    x = x + (i + i) * Cells(i, j).Value / Sum
    y = y + (j + i) * Cells(i, j).Value / Sum
    Next j
Next i
Msgbox("Center :" & x & y)
End Sub

2 个答案:

答案 0 :(得分:1)

这是可以计算质心的UDF:

Function CenterOfGravity(Masses As Variant) As Variant
    'Masses is a 2-dimensional array of weights
    'calculates center of gravity, returning the answer as
    'an array of 2 value

    If TypeName(Masses) = "Range" Then Masses = Masses.Value

    Dim mass As Double
    Dim momentX As Double, momentY As Double
    Dim i As Long, j As Long

    For i = LBound(Masses, 1) To UBound(Masses, 1)
        For j = LBound(Masses, 2) To UBound(Masses, 2)
            mass = mass + Masses(i, j)
            momentX = momentX + j * Masses(i, j)
            momentY = momentY + i * Masses(i, j)
        Next j
    Next i
    CenterOfGravity = Array(momentX / mass, momentY / mass)
End Function

例如:

enter image description here

在上面的单元格A7:B7中,我输入了=CenterOfGravity(A1:E5)作为数组公式(Ctrl+Shift+Enter接受)。

答案 1 :(得分:0)

对您的代码进行了较小的修改

<input id="inputText"
type="text"    
name="filter"
class="form-control"    
data-ng-model="selectedVal"
uib-typeahead="item.userValue for item in getUserDetails(1,$viewValue) | filter:$viewValue | limitTo:10 "
typeahead-template="displayTemplate.html"/>

<script type="text/ng-template" id="displayTemplate.html">
<a>
<span bind-html-unsafe="match.label | uibTypeaheadHighlight:query"></span>
</a>
</script>

测试以上代码

Option Explicit

Sub calcGravity()
Dim i As Integer, j As Integer
Dim sum As Long
Dim x As double, y As double
'get sum
For i = 1 To 5
    For j = 1 To 5
    sum = sum + Cells(i, j).Value
    'Debug.Print Cells(i, j).Address
    Next j
Next i

For i = 1 To 5
    For j = 1 To 5
    x = x + (i) * Cells(i, j).Value / sum
    y = y + (j) * Cells(i, j).Value / sum
    'Debug.Print Cells(i, j).Address
Next j
Next i
Debug.Print ("Center x: " & x & " y: " & y)
End Sub

立即窗口

Sub Test1()
    Call Set_All_to_0
    Range("A1").Value = 1
    Debug.Print "Only A1 = 1"
    Call calcGravity
    Debug.Print

    Call Set_All_to_0
    Range("E1").Value = 1
    Debug.Print "Only E1 = 1"
    Call calcGravity
    Debug.Print

    Call Set_All_to_0
    Range("A5").Value = 1
    Debug.Print "Only A5 = 1"
    Call calcGravity
    Debug.Print

    Call Set_All_to_0
    Range("E5").Value = 1
    Debug.Print "Only A5 = 1"
    Call calcGravity
    Debug.Print

    Call Set_All_to_0
    Range("A1:E1").Value = 1
    Debug.Print "A1:E1 = 1"
    Call calcGravity
    Debug.Print

    Call Set_All_to_0
    Range("A5:E5").Value = 1
    Debug.Print "A5:E5 = 1"
    Call calcGravity
    Debug.Print

    Call Set_All_to_0
    Range("A1:A5").Value = 1
    Debug.Print "A1:A5 = 1"
    Call calcGravity
    Debug.Print

End Sub

Sub Set_All_to_0()
Dim i As Integer, j As Integer
    For i = 1 To 5
        For j = 1 To 5
            Cells(i, j) = 0
        Next j
    Next i
End Sub