如何使每个循环向后运行 [英] How to Make a For-Each Loop Run Backwards

查看:41
本文介绍了如何使每个循环向后运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在VBA中编写了一个小脚本,该脚本根据列表检查给定范围内的单元格值.如果单元格值与列表中的值匹配,则将其保留,否则将其删除.我想知道如何使它向后运行,因为向前运行会产生问题.我对此进行了一些研究,并尝试在开始for循环的行末尾附加步骤-1",但这在这种情况下不起作用.

I have written a small script in VBA which checks the value of a cell in a given range against a list. If the cell values match a value in the list it is kept, else it is deleted. I was wondering how I could make it run backward, as running it forwards creates issues. I have researched this somewhat and I have tried appending 'Step -1' to the end of the line which begins the for loop, but this doesn't work in this case.

Set Rng = Range("A9:V9")
For Each cell In Rng
    If Not myList.Exists(cell.Value) Then
        cell.EntireColumn.Delete
    End If
Next

推荐答案

在这种情况下,像这样的for循环可能就足够了:

In this case, probably some for-loop like this one would be enough:

Option Explicit

Sub TestMe()

    Dim rng As Range
    Dim cnt As Long

    Set rng = Range("A9:V9")

    For cnt = rng.Cells.Count To 1 Step -1
        Cells(rng.Row, cnt) = 23
        Stop
    Next

End Sub

我放置了Stop,因此您可以看到所引用的单元格.按下Stop后,请继续按 F5 .

I have put Stop so you can see which cell is referred. Once you hit the Stop, continue further with F5.

这篇关于如何使每个循环向后运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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