循环和行参考

下面的代码需要在循环中运行= disvincenty公式,并且每次代码块运行时引用的单元格$ C $ 2,$ D $ 2需要每次在同一列中向下一行,直到ABF(列中具有数据的最后一行T.)然后它退出的子

对于每一行,它需要在已经引用的相同列中运行公式= Min和两个= small,而且一次只下降一行 – 与= distvincenty相同,但是每次粘贴值以保留结果。

所以= distvincenty在同一行中查看彼此相邻的两个条件,比较列表中的列表,将另外三个公式应用到该行,然后向下移动。

Dim rng As Range Dim row As Range Dim cell As Range Dim ABF As Integer ABF = Sheet9.Range("T" & Rows.Count).End(xlUp).row Set rng = Range("Q2:Q" & ABF) For Each row In rng.Rows For Each cell In row.Cells Sheet9.Range("Q2").Formula = "=distVincenty($C$2,$D$2,$R2,$S2)/1609.344" Sheet9.Range("Q2").Copy Sheet9.Range("Q2:Q" & ABF).PasteSpecial xlPasteAll Application.CutCopyMode = False Sheet9.Range("E2").Formula = "=MIN(Q:Q)" Sheet9.Range("H2").Formula = "=SMALL(Q:Q,2)" Sheet9.Range("K2").Formula = "=SMALL(Q:Q,3)" Next cell Next row 

更新:

我现在有这个:

 Dim ABF As Integer Dim i As Integer For i = 2 To Sheet9.Range("A" & Rows.Count).End(xlUp).row ABF = Sheet10.Range("AC" & Rows.Count).End(xlUp).row Sheet10.Range("AE3").Formula = "=distVincenty('Booking Workings'!$C$2,'Booking Workings'!$D$2,'User List'!$Z3,'User List'!$AA3)/1609.344" Sheet10.Range("AE3").Copy Sheet10.Range("AE3:AE" & ABF).PasteSpecial xlPasteAll Application.CutCopyMode = False Sheet9.Range("E" & i).Formula = "=MIN('User List'!AE:AE)" Sheet9.Range("H" & i).Formula = "=SMALL('User List'!AE:AE,2)" Sheet9.Range("K" & i).Formula = "=SMALL('User List'!AE:AE,3)" Next i End sub 

我唯一的问题是每次这个代码运行,我需要$ C $ 2和$ D $ 2像更简单的公式一样改变。

我可以这样做吗?

  Sheet10.Range("AE3").Formula = "=distVincenty('Booking Workings'!$C$"(I)",'Booking Workings'!$D$"(I)",'User List'!$Z3,'User List'!$AA3)/1609.344" 

更新看起来像这样的作品:

 "=distVincenty('Booking Workings'!$C$" & (i) & ",'Booking Workings'!$D$" & (i) & ",'User List'!$Z3,'User List'!$AA3)/1609.344" 

这很难解释。 让我知道我是否接近。

 Sub Test() Dim rng As Range Dim row As Range Dim cell As Range Dim ABF As Integer Dim arrValues Dim count As Integer ABF = Sheet9.Range("T" & Rows.count).End(xlUp).row Set rng = Range("Q2:Q" & ABF) arrValues = rng.Value For Each cell In rng Sheet9.Range("Q2:Q" & ABF).Formula = "=distVincenty($C$" & cell.row & ",$D$" & cell.row & ",$R2,$S2)/1609.344" Sheet9.Range("E2").Formula = "=MIN(Q:Q)" Sheet9.Range("H2").Formula = "=SMALL(Q:Q,2)" Sheet9.Range("K2").Formula = "=SMALL(Q:Q,3)" count = count + 1 arrValues(count) = cell.Value Next cell Sheet9.Range("Q2:Q" & ABF).Value = arrValues End Sub