2016-09-20 43 views
0

我是新到Excel VBA和我想要计算两个原子之间的距离,使一个循环来计算它的所有希望的情况下不能使环路上的Excel VBA和打印效果

与坐标B(我),C(i),D(i)在Excel工作表中对应于x,y,z笛卡尔坐标..

这些原子位于:一行一行(i),另一行一行(i + 5)

我写这个算法,但我不能转移到excel VBA

For i=4 to 1000 
    For j=9 to 1000 

    d=SQRT(POWER(B(i)-B(j),2)+ POWER(C(i)-C(j),2)+ POWER(D(i)-D(j),2)) 

    print **d** in (P(i)) #want to print the distance **d** in a case 
    j=j+4 # **j** is a multiple of 4 
    i=i+4 # **i** is a multiple of 4 

next i 

谢谢,这是我的第一个问题

+1

你的意思'Debug.Print'到即时窗口?或'MsgBox'? –

+0

所以你有997个原子,你想计算每个前992个原子与5个原子向前的原子的距离?按'B(i)'你是指B列的第i个元素吗?你想用这些距离做什么?将它们转储到列E? –

+0

是的我想把它们转储到列E – tatitechno

回答

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 
+0

哇,这正是我想要的。我正在计算一个原子与另一个原子结合的可能性......距离应该在1.1-1.4范围内...我知道我必须计算相反的......也就是最后一个例如......我会整理出来的..非常感谢你..我一直在等待几个月的时间让我的同事完成他的任务 - 这个任务 - – tatitechno

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 
+0

谢谢CMArg,我会用这个脚本来建立我的,并告诉 – tatitechno

+0

在我以前的回答中看到版本。让我知道它是否可以帮助你。 – CMArg

+0

该脚本工作正常,非常感谢你..因为你看到我最近修改了算法打印在我和j ..我的坏。约翰先生帮了我的忙。再次感谢你 – tatitechno

0
Option Explicit 

Sub AtomDistance() 
' 
' AtomDistance Macro1 
' 

' 
Dim i As Integer 
Dim j As Integer 
Dim Distance As Double 

Dim Column As String 
Column = InputBox("Which column you want to print results(put a letter)?") 
Dim MyCell11 As String 
Dim MyCell12 As String 
Dim MyCell13 As String 

Dim MyCell21 As String 
Dim MyCell22 As String 
Dim MyCell23 As String 

Dim MyCell3 As String 
j = 9 
For i = 4 To 12 

MyCell3 = Column & i 

MyCell11 = "B" & i 
MyCell12 = "C" & i 
MyCell13 = "D" & i 

MyCell21 = "B" & j 
MyCell22 = "C" & j 
MyCell23 = "D" & j 

Distance = (((Range(MyCell11).Value - Range(MyCell21).Value)^2) + ((Range(MyCell12).Value - Range(MyCell22).Value)^2) + ((Range(MyCell13).Value - Range(MyCell23).Value)^2))^0.5 


If i Mod 4 = 0 Or j Mod 4 = 0 Then 

    Range(MyCell3).Value = Distance 
End If 

j = j + 1 

Next i