热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

重复宏,直到满足条件-Repeatmacrountilthecriteriaismet

Iamworkingonamacrotodoarepetitivetaskforme.Togiveyouaclearview,Ihavepreparedmy

I am working on a macro to do a repetitive task for me. To give you a clear view, I have prepared my initial macro (vba) code below as well as the flow chart of what it suppose to do:

我正在研究一个宏来为我做一个重复的任务。为了给你一个清晰的视图,我准备了下面的初始宏(vba)代码以及它想要做的事情的流程图:

Here a narrative description of the macro:

这里是宏的叙述性描述:

  1. Check all the cells in Column K if it contains the word "MERGED"
  2. 检查K列中的所有单元格是否包含单词“MERGED”

  3. IF the macro found the word "Merged", it will call another macro (Macro_X)
  4. 如果宏发现单词“Merged”,它将调用另一个宏(Macro_X)

  5. After calling the Macro, it will check again all the cells in Column K if it still contains the word "MERGED"
  6. 调用宏后,如果它仍包含单词“MERGED”,它将再次检查K列中的所有单元格

  7. If the macro still finds the word "Merged", it will call again the Macro_X.
  8. 如果宏仍然找到单词“Merged”,它将再次调用Macro_X。

  9. The condition will only stop until the Column K doesn't contain the word "Merged".
  10. 条件只会停止,直到列K不包含单词“Merged”。

  11. If the macro didn't find any "Merged" word in column K, it will now call the Macro_Z.

    如果宏没有在列K中找到任何“合并”字,它现在将调用Macro_Z。

    Last = Cells(Rows.Count, "K").End(xlUp).Row
    For i = Last To 1 Step -1
        If (Cells(i, "K").Value) = "Merged" Then
    
            Call macro_x
    
        End If
    Next I
    

enter image description here

Screen shot of column K

列K的屏幕截图

enter image description here

I already have the code above but it's not working. Not sure why. Could you please help to point out what's wrong or suggest a better code?

我已经有上面的代码,但它不起作用。不知道为什么。你能帮忙指出什么是错的或建议更好的代码吗?

3 个解决方案

#1


1  

You can add simple Boolean variable to check if the word "MERGED" was found.

您可以添加简单的布尔变量来检查是否找到了单词“MERGED”。

Dim Word_Found as Boolean

Last = Cells(Rows.Count, "K").End(xlUp).Row
For i = Last To 1 Step -1
    If (Cells(i, "K").Value) = "Merged" Then
        Call macro_x

        Word_Found = True
        i = LAST

    End If
Next I

If Not(Word_Found) Then Call macro_z

Above code is what can be modified from your code.

上面的代码是可以从代码中修改的代码。

Below code is written looking at your flow chart.

下面的代码是查看您的流程图。

While Not (Columns("K").Find("Merged") Is Nothing)
    Call macro_x
Wend
Call macro_z

#2


1  

You can call your initial macro at the end of your Macro_X. for the initial macro:

您可以在Macro_X的末尾调用初始宏。对于初始宏:

Sub checker()
Last = Cells(Rows.Count, "K").End(xlUp).Row
For i = 1 To Last Step -1
    If (Cells(i, "K").Value) = "Merged" Then

        Call macro_x

    End If
Next I
Call macro_z
End Sub

for macro_x:

Sub macro_x()
'DO SOMETHING HERE

Call checker
End Sub

EDIT:

For i = 1 To Last Step -1

#3


1  

First thing I see is "merged" in your cell doesn't start with capital letter but in your code does.

我看到的第一件事是你的单元格中的“合并”并不是以大写字母开头,而是在你的代码中。

So please change to this.

所以请改为这个。

Last = Cells(Rows.Count, "K").End(xlUp).Row
For i = Last To 1 Step -1
    If (Cells(i, "K").Value) = "merged" Then

        Call macro_x

    End If
Next I

推荐阅读
author-avatar
4号的国哥
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有