陷入循环,帮助 [英] Stuck in loop, Help

查看:78
本文介绍了陷入循环,帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我是VBA的新手,并且找不到结束这个循环的方法。我已经尝试了几种解决方案但是我不得不重新启动应用程序。编码按照我的意愿运行,只是想结束无限循环。任何帮助都会非常感激
。谢谢


Sub Macro5()

 

  将我的数据调暗为范围

   Dim myCell As Range

  

  设置myData = Range(Cells.Address)

 

  每个myCell在我的数据中$
  

  如果myCell.Value Mod 2 = 0并且myCell.Value> 0然后

  

   myCell.Interior.ColorIndex = 37

  

   ElseIf myCell.Value Mod 2 = 1然后

  

   myCell.Interior.ColorIndex = 4

  

  结束如果


  下一步


结束子


解决方案

回复: 太多循环



您正在循环通过工作表上的每个单元格,因为"单元格"指活动工作表上的每个单元格。

如果您在"单元格"前加上"如果有一个范围,则单元格仅指该范围内的单元格。

因此,您可以使用...来限制循环到您选择的单元格。



设置myData = Selection.Cells

 而不是...

设置myData =范围(Cells.Address)

'---



FWIW ...
$
我在内华达州的太浩湖看到你的同名演出,不久之后就死了。



'---

Jim Cone

美国俄勒冈州波特兰市

https://goo.gl/IUQUN2  (Dropbox)

Hi all, I am new to VBA and can't find a way to end this loop. I have tried several solutions but am stuk in a constant loop and have to restart application. The coding works how I want, just want to end infinite loop. Any help would be greatly appreciated. Thanks

Sub Macro5()
 
   Dim myData As Range
   Dim myCell As Range
  
   Set myData = Range(Cells.Address)
 
   For Each myCell In myData
  
   If myCell.Value Mod 2 = 0 And myCell.Value > 0 Then
  
   myCell.Interior.ColorIndex = 37
  
   ElseIf myCell.Value Mod 2 = 1 Then
  
   myCell.Interior.ColorIndex = 4
  
   End If

   Next

End Sub

解决方案

Re:  too much loop

You are looping thru every cell on the worksheet because "cells" refers to every cell on the active worksheet.
If you prefix "cells" with a range then cells refers to the cells only in that range.
So, you can limit the loop to the cells you select by using...

Set myData = Selection.Cells
  instead of...
Set myData = Range(Cells.Address)
'---

FWIW...
I saw your namesake perform in Lake Tahoe, Nevada, not too long before he died.

'---
Jim Cone
Portland, Oregon USA


这篇关于陷入循环,帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆