excel - 使用 do while 循环时,我的 VBA 代码不起作用
问题描述
我下面的 VBA 代码没有循环。请帮助
Public Sub FunWithLoops()
' Create a loop using Do while loop.
'The aim is to go through a list of numbers down a row, highlight them with a different color if greater than 10.
Dim i As Integer
i = 1
Do While i <= 10
If ActiveCell.Value > 10 Then
ActiveCell.Interior.Color = RGB(255, 0, 0)
'interior changes the background color
End If
ActiveCell.Offset(1, 0).Select
i = i + 10
Loop
End Sub
解决方案
只需更正您的代码而不进行太多更改,您可能还没有准备好:
Public Sub FunWithLoops()
' Create a loop using Do while loop.
'The aim is to go through a list of numbers down a row, highlight them with a different color if greater than 10.
Dim i As Integer
i = 1
Do While i <= 10
If ActiveCell.Value > 10 Then ActiveCell.Interior.Color = RGB(255, 0, 0)
ActiveCell.Offset(1, 0).Select
i = i + 1 ' change this to + "1" not + "10"
Loop
End Sub
请注意,您ActiveCell
必须始终在运行例程之前进行设置,因为例程中没有任何内容可以设置第ActiveCell
一个,例如一行:
Range("A2").Select
推荐阅读
- python - 为什么 CopyData 实用程序在一段时间后停止工作?
- javascript - 无法通过 CSS 设置自定义纸张大小
- google-api - 在 Google Drives API v3 的列表操作中获取附加字段
- python - 如何用手指从图像中提取指甲?
- seo - 打开图形描述元标记在 LinkedIn 中不起作用
- powershell - 将已发送的电子邮件复制到已发送文件夹(电子邮件服务器)
- r - 在 `googlesheets` 中使用来自 `googleAuthR` 的令牌
- azure - Velero 部署失败 - 无法找到 Azure /credentials/cloud 文件
- python - 尝试使用 catch 验证 Python 中的输入
- tfs - TFS 2013 团队构建 - 排队构建保持进行中状态