我创建了一个包含1列和200行的简单excel文件。每个单元格都包含一个整数值。
我正在尝试创建一个VBA代码,该代码将自动使整数值超过100的第一个单元格成为活动单元格。
这是我想出的代码。我的问题是它什么都不做:(
Sub test1()
Dim trial As Range
Dim cell As Range
Set trial = Range("A1:A100")
For Each cell In trial
If cell > 100 Then
End If
Next cell
End Sub
试试这个。
当使用Range
并且您想要使用Cell
值时,请始终使用.Value
。正如@gizmeier在下面的评论中提到的那样。当你在后期开始为更干净的编码编程时,把这样的小事做好是很好的。
此外,Sub
将运行,然后停止,而不会更改任何内容,但它确实有效。因此,您只需要将Activate
添加到If
语句中,然后决定仅结束For
循环或将Sub
添加到End
Sub test1()
Dim trial As Range
Dim cell As Range
Set trial = Range("A1:A100")
For Each cell In trial
If cell > 100 Then
cell.Activate
'Exit For will exit the For Loop
Exit For
'End will stop the code from running
End
End If
Next cell
End Sub
无循环:
Dim rng1 As Range
Dim lngPos As Long
Set rng1 = [a1:a100]
lngPos = Evaluate("=MIN(IF(" & rng1.Address & ">100,ROW(" & rng1.Address & "),1000))")
If lngPos < rng1.cells.count Then Application.Goto rng1.Cells(lngPos)