2016-09-20 67 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