excel - 在不同范围内循环VBA
问题描述
下面的代码运行良好,但需要现代化。此代码用于一个表,它一直有效到表结束。但我在实际(存在)表下方的同一页面添加了附加表。我需要通过 3 个空单元格跳过 i (循环)并为下表启动代码。换句话说,要在下面找到下一个表并在那里实现代码。如何实现(实现)这一点?
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
解决方案
另外,因为您的公式仅与同一行相关,您可以尝试以下更简单的方法:
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
推荐阅读
- java - 如何让“select into”语句与 H2 一起使用
- python - Pandas df 根据与不同列中的行匹配的字典中的值更改一列中的行的值
- java - 如何使用两个类获得两个变量的总和
- javascript - 使用正则表达式从 XML 中提取字符串
- django - 在 DRF 创建表单上添加 Django 传单位置选择器
- python - 如何增加 axs[i].title.set_text 上的标题字体大小
- ios - SwiftUI:iPad 弹出框上奇怪的关闭动画
- javascript - 在不丢失原始堆栈的情况下覆盖 console.log 方法 - javascript
- struct - LLVM 后端如何处理具有动态访问权限的结构的 getelementptr?
- cryptography - 无法将 pkcs11-tool 与自定义 hsm 角色一起使用