[英]Loop in different ranges VBA
Code below works good, but it needs modernization. 下面的代码很好用,但需要进行现代化。 This code for one table, it works until the end of table.
此代码用于一个表,直到表末尾都有效。 But I added addition table at the same page below actual (exist) table.
但是我在实际(存在)表的同一页下添加了加法表。 I need jumping i (loop) through 3 empty cells and start code for next table below.
我需要跳过3个空单元格(循环)并为下面的下表启动代码。 In another words, to find next table below and implement code there too.
换句话说,要找到下面的表格并在那里执行代码。 How to implement (realize) that?
如何实现(实现)?
Sub Test()
Dim score1 As Double, score2 As Double, score3 As Double, result As String, text As String
Dim ifrom As Long, ito As Long
Dim i As Long
ifrom = 2
ito = Range("E2").End(xlDown).Row ' find the row above the 1st blank cell in column E
name1 = "A"
name2 = "B"
audit = "C"
currenc = "Dollars"
For i = ifrom To ito
text = Range("E" & i).Value
score1 = Int(Range("B" & i).Value)
score2 = Int(Range("C" & i).Value)
score3 = Int(Abs(Range("D" & i).Value))
If score1 = 0 And score2 = 0 Then
result = text + ......
ElseIf score1 = score2 Then
result = text +........
ElseIf score1 > score2 And score2 <> 0 Then
result = text + ............
ElseIf score1 < score2 And score1 <> 0 Then
result = text +......
Else
result = text + " 00000000"
End If
Range("H" & i).Value = result
Next i
End Sub
Try this: 尝试这个:
Sub Test()
'... Define your variables ....
Dim LastRow As Long
LastRow = Range("E" & Rows.Count).End(xlUp).Row
name1 = "A"
name2 = "B"
audit = "C"
currenc = "Dollars"
ifrom = 2
Do
ito = Range("E" & ifrom).End(xlDown).Row ' find the row above the 1st blank cell in column E
For i = ifrom To ito
'... Your code comes here ...
Next i
ifrom = Range("E" & ifrom).End(xlDown).Row ' find top of next table, if no table return last row of worksheet
Loop While ifrom < LastRow
End Sub
Also because your formulas are related just to same row you can try following which is more simple: 另外,由于您的公式仅与同一行相关,因此您可以尝试以下更简单的方法:
Sub Test()
Dim score1 As Double, score2 As Double, score3 As Double, result As String, text As String
Dim ifrom As Long, ito As Long
Dim i As Long
ifrom = 2
ito = Range("E" & Rows.Count).End(xlUp).Row 'This row changed
name1 = "A"
name2 = "B"
audit = "C"
currenc = "Dollars"
For i = ifrom To ito
If Range("E" & i).Value <> "" Then
'.... Your code goes here
End If
Next i
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.