我是Excel VBA的新手,我想计算两个原子之间的距离并制作一个循环来计算所有想要的情况
坐标B(i),Excel表格中的C(i),D(i)对应 x,y,z 笛卡尔坐标..
这些原子位于:连续一行(i),另一行(i + 5)
我写这个算法,但我不能把它转移到excel VBA
JSON::PP
谢谢,这是我的第一个问题
答案 0 :(得分:1)
我认为以下内容适合您:
Sub FindDistances()
Dim i As Long, j As Long
Dim r As Long, c As Long 'row and column indices for output
Dim data As Variant
Application.ScreenUpdating = False 'useful when doing a lot of writing
data = Range("B4:D1000").Value 'data is a 1-based array
c = 5 'column E
For i = 1 To UBound(data) - 5 Step 4
r = 1 'first row printed in -- adjust if need be
For j = i + 5 To UBound(data) Step 4
Cells(r, c).Value = Sqr((data(i, 1) - data(j, 1)) ^ 2 + (data(i, 2) - data(j, 2)) ^ 2 + (data(i, 3) - data(j, 3)) ^ 2)
r = r + 1
Next j
c = c + 1
Next i
Application.ScreenUpdating = True
End Sub
答案 1 :(得分:0)
这样的东西?在VBA中,您可以引用Cells(row, column)
之类的单元格。数据应位于名为Sheet1
的工作表中。我为了简单起见而单独计算每个维度(d1, d2, d3
)。如果您愿意,可以将这四行合并为一行。 编辑:阅读上面的评论,我添加了一个嵌套循环(j)。
Sub Distances()
Dim i As Integer
Dim j As Integer
Dim d1 As Double, d2 As Double, d3 As Double, d As Double
For i = 4 To 1000 Step 4 'Can't understand your data, but Step 4 tries to account for your j=j+4 and i=i+4
For j = 9 To 1000 Step 4
d1 = (Worksheets("Sheet1").Cells(i, 2) - Worksheets("Sheet1").Cells(j, 2)) ^ 2
d2 = (Worksheets("Sheet1").Cells(i, 3) - Worksheets("Sheet1").Cells(j, 3)) ^ 2
d3 = (Worksheets("Sheet1").Cells(i, 4) - Worksheets("Sheet1").Cells(j, 4)) ^ 2
d = Sqr(d1 + d2 + d3)
Worksheets("Sheet1").Cells(i, 16).Value = d
Next j
Next i
End Sub
答案 2 :(得分:0)
import scipy.sparse
data = []
row = []
col = []
csr = scipy.sparse.csr_matrix((data, (row, col))) #error happens here
print(type(csr))
print(csr)