Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
For循环在excel VBA中未完全循环_Excel_Vba - Fatal编程技术网

For循环在excel VBA中未完全循环

For循环在excel VBA中未完全循环,excel,vba,Excel,Vba,我有一个宏,在其中搜索行中的文本,如果列中没有我指定的文本,它将被删除。这是我的密码: Private Sub Test() Dim lColumn As Long lColumn = ActiveSheet.Cells(2, Columns.Count).End(xlToLeft).Column Dim i As Long Dim myCell As Range Dim myRange As Range Set myRange = Worksheets("2019").Range

我有一个宏,在其中搜索行中的文本,如果列中没有我指定的文本,它将被删除。这是我的密码:

Private Sub Test()

Dim lColumn As Long
    lColumn = ActiveSheet.Cells(2, Columns.Count).End(xlToLeft).Column


Dim i As Long
Dim myCell As Range
Dim myRange As Range
Set myRange = Worksheets("2019").Range(Cells(2, 1), Cells(2, lColumn))

For Each myCell In myRange
  If Not myCell Like "*($'000s)*" And Not myCell Like "*Stmt Entry*" And Not myCell Like "*TCF*" And_ 
  Not myCell Like "*Subtotal*" And Not myCell Like "*Hold*" Then
    myCell.EntireColumn.Select
    Selection.Delete
  End If

Next

End Sub
我的问题是,当我执行宏时,它只会删除一些列,而不会删除接近范围末尾的列。如果我再次运行宏,它将成功删除我要求它删除的所有列

如果我将宏切换到(比方说)使单元格加粗而不是删除它们,那么每次都能完美地工作

我错过了什么


非常感谢

尽管每个人都说在这个链接的帖子中只需向后循环,但这不是你想要做的

它会起作用的,然后你的下一个问题是我如何加速这个循环

真正的解决办法是停止你正在做的事情,用不同的方式做事。在迭代集合时对其进行修改从来都不是一个好主意

从可以将两个范围合并为一个范围的辅助函数开始:

Private Function CombineRanges(ByVal source As Range, ByVal toCombine As Range) As Range
    If source Is Nothing Then
        'note: returns Nothing if toCombine is Nothing
        Set CombineRanges = toCombine
    Else
        Set CombineRanges = Union(source, toCombine)
    End If
End Function
然后声明一个toDelete范围,并在迭代时使用此CombineRanges函数构建并选择一个范围-请注意,此循环不会在任何地方修改任何单元格:

Dim sheet As Worksheet
' todo: use sheet's codename instead if '2019' is in ThisWorkbook
Set sheet = ActiveWorkbook.Worksheets("2019")

Dim source As Range
' note: qualified .Cells member calls refer to same sheet as .Range call
Set source = sheet.Range(sheet.Cells(2, 1), sheet.Cells(2, lColumn))

Dim toDelete As Range
Dim cell As Range
For Each cell In source
    'note: needed because comparing cell.Value with anything will throw error 13 "type mismatch" if cell contains a worksheet error value.
    'alternatively, use cell.Text.
    If Not IsError(cell.Value) Then
        If Not cell.Value Like "*($'000s)*" _
            And Not cell.Value Like "*Stmt Entry*" _
            And Not cell.Value Like "*TCF*" _
            And Not cell.Value Like "*Subtotal*" _
            And Not cell.Value Like "*Hold*" _
        Then
            Set toDelete = CombineRanges(cell, toDelete)
        End If
    End If
Next
最后一步是删除toDelete范围的.EntireColumn。。。如果在那一点上它不是什么:

If Not toDelete Is Nothing Then toDelete.EntireColumn.Delete

如果你在循环和删除,你应该从右到左循环。我该怎么做?你在迭代一个集合的同时修改它。这通常意味着意外、怪异的行为。考虑合并列,而不是选择它们,然后在循环后一次运行联合对象对象的.DELETE方法,而不是在每次迭代中删除选择。向后循环以低效地修改正在迭代的集合只是。。。。我也是。每行打几十遍,每列都是一样的歌舞。建议不要使用ActiveSheet获取最后一列;使用你真正感兴趣的表格。无法保证ActiveSheet将是您所期望的。不可思议!为什么这个答案有4个喜欢,而我几乎相同的结果的答案是零,即使我的答案是一个惊人的两分钟前。这就是BigBen所做的一切——称我的代码为黑客。因为我没有更好的事要做,所以我要投第五票,好吧,要么我妈妈回到StackOverflow,要么肯定有人读到了我的抱怨,给了我一个赞。谢谢对任何有声望的人来说都是一个严肃的问题:请参阅我创建最后一行/列方法的方法,以避免在每个循环上使用if语句。思考这是否更好/更糟/太微不足道而不值得担心?谢谢。@PGSystemTester将killrng初始赋值到工作表的最后一列,以确保killrng不是空的,这是无关的工作,不需要进行空检查。虽然该列中不太可能有任何有价值的内容,但宏不应该影响没有理由受到影响的单元格:使用这种黑客手段意味着在代码中烘焙一个假设,任何可以从代码中删除的假设都应该从代码中删除。谢谢。我感谢你的反馈。我可以忍受在最后一排出现问题的可能性,但是我认为我在速度上有所提高。我做了一些测试。。。。根本没有明显的改善。所以我同意。感谢您的反馈!