简体   繁体   English

VBA复制粘贴循环未正确执行

[英]VBA Copy Paste Loop Not Executing Correctly

I've had a piece of code for a VBA copy-paste loop that I've used for a year and half without any issues that is suddenly not executing correctly. 我已经使用了一年半的VBA复制粘贴循环的一段代码,而没有突然无法正确执行的任何问题。 The intent is to take a specific column with a few unique values, and copy paste those filters into new sheets that take the name of unique values. 目的是获取带有一些唯一值的特定列,然后将这些过滤器复制粘贴到具有唯一值名称的新工作表中。 Now there suddenly appears to be a problem with the filtering part of this - when I run the macro it executes without any errors, but completely fails to actually copy and paste anything or create any new sheets. 现在,过滤的部分突然出现了问题-当我运行宏时,它执行时没有任何错误,但是完全无法真正复制和粘贴任何内容或创建任何新的工作表。 The one thing that DOES happen is copying the unique values into the CO column, but the subsequent loop isn't working right for some reason. 确实发生的一件事是将唯一值复制到CO列中,但是由于某种原因,随后的循环无法正常工作。

As far as I can tell there have been no inadvertent changes in my code or in the formatting of the reports that I run this with (daily, dynamic ranges) so I'm really stumped on what could possibly be different all of the sudden. 据我所知,我运行此代码的代码或报表的格式(每日,动态范围)没有发生意外更改,因此我真的很惊讶突然之间可能有什么不同。

Specifically, the range of values to be copied is A1:CN with a dynamic number of rows and the column with the unique values to be filtered is U. Any ideas why it might not be working? 具体来说,要复制的值的范围是A1:CN,其中行数是动态的,而要过滤的唯一值的列是U。有什么想法可能不起作用?

Dim rng as Range
Dim c As Range
Dim LR As Long

    LR = Cells(Rows.Count, "A").End(xlUp).Row
    Set rng = Range("A1:CN" & LR)

    Range("U1:U" & LR).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("CO1"), Unique:=True

    For Each c In Range([CO2], Cells(Rows.Count, "CO").End(xlUp))
        With rng
            .AutoFilter
            .AutoFilter Field:=21, Criteria1:=c.Value
            .SpecialCells(xlCellTypeVisible).Copy
            Sheets.Add(After:=Sheets(Sheets.Count)).Name = c.Value
            ActiveSheet.Paste
        End With
    Next c

You are relying in your code on ActiveSheet , all of your objects, such as Set rng = Range("A1:CN" & LR) , and Range("U1:U" & LR).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=.Range("CO1"), Unique:=True are not qualified with a worksheet. 您将代码依赖于ActiveSheet ,所有对象,例如Set rng = Range("A1:CN" & LR)Range("U1:U" & LR).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=.Range("CO1"), Unique:=True不符合工作表的条件。

You need to add a With Worksheets("Sheet1") statement at the beginning of your code, then qualify all the nested objects with a . 您需要在代码的开头添加With Worksheets("Sheet1")语句,然后使用限定所有嵌套对象. , and it should work fine. ,它应该可以正常工作。

Code

With Worksheets("Shee1") ' <-- you need this line,  modify to your sheet's name

    LR = .Cells(.Rows.Count, "A").End(xlUp).Row
    Set rng = .Range("A1:CN" & LR)

    .Range("U1:U" & LR).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=.Range("CO1"), Unique:=True

    For Each c In .Range(Range("CO2"), .Cells(.Rows.Count, "CO").End(xlUp))
        With rng
            .AutoFilter
            .AutoFilter Field:=21, Criteria1:=c.Value
            .SpecialCells(xlCellTypeVisible).Copy
            Sheets.Add(After:=Sheets(Sheets.Count)).Name = c.Value
            ActiveSheet.Paste
        End With
    Next c
End With

So - it's resolved itself after I closed all Excel sheets and re-opened. 所以-我关闭所有Excel工作表并重新打开后,它本身就解决了。 But now I'm curious if anybody knows why this might have happened at all? 但是现在我很好奇是否有人知道为什么可能会发生这种情况? Is there an issue where the code truncates itself if Excel is low on memory? 如果Excel内存不足,代码会自行截断吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM