宏复制空白单元格

宏复制空白单元格

我有这个宏:

在 Sheet 1 中,它选择 A 列中的所有值,然后仅选择常量,然后选择过滤器并复制 A 列中的过滤值并将其粘贴到另一个列表中。然后再次进行,但选择下一列。

问题是当已过滤列为空。当有一些值时,没有问题,但是当过滤后的列为空时,我收到运行时错误。请问我该如何解决这个问题?

这只是一个小例子

这里我的宏只会复制 A 列和 B 列,当它到达 CI 列时出现错误并且无法继续。 过滤后

更新:

例 1:

'   PO DC2 O
Sheets("Celkový harmonogram").Select
ActiveWorkbook.SlicerCaches("Průřez_dispečer32111").ClearManualFilter
Range("Tabulka141121518[13]").Select
Selection.SpecialCells(xlCellTypeConstants, 23).Select
With ActiveWorkbook.SlicerCaches("Průřez_dispečer32111")
    .SlicerItems("Bodnariucová Renáta").Selected = False
    .SlicerItems("Kajer Roman").Selected = True
End With

If Application.CountIf(Selection, "<>0") < 1 Then GoTo Line442
Application.CutCopyMode = False
Selection.Copy
Sheets("jednotlivci").Select
Range("Y6").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

'   UT DC2 R
Line442:
Sheets("Celkový harmonogram").Select
ActiveWorkbook.SlicerCaches("Průřez_dispečer32111").ClearManualFilter
Range("Tabulka141121518[14]").Select
Selection.SpecialCells(xlCellTypeConstants, 23).Select
With ActiveWorkbook.SlicerCaches("Průřez_dispečer32111")
...

例 2:(空单元格错误)

If Application.CountIf(myRange, "<>0") > 0 Then
Application.CutCopyMode = False
Selection.Copy
Sheets("jednotlivci").Select
Range("Y6").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Else: GoTo Line442
End If

例 3:(“If..”行处出现类型不匹配错误)

If Application.CountIf(Selection, "<>0") < 1 Then GoTo Line442

Application.CutCopyMode = False
Selection.Copy
Sheets("jednotlivci").Select
Range("Y6").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

答案1

我在你的代码中添加了三行。它们检查你的选择/范围是否为空,如果是,则退出子程序。你需要运行此代码,并可能删除你已经添加的 if 语句(.Value对我来说没有多大意义)。此外,你需要检查我添加的哪个 if 语句解决了你在问题中首先提到的原始错误(很可能你最终只会遇到第三个错误)。

Sheets("Celkový harmonogram").Select
ActiveWorkbook.SlicerCaches("Průřez_dispečer32111").ClearManualFilter
Range("Tabulka141121518[13]").Select
Selection.SpecialCells(xlCellTypeConstants, 23).Select

'This is the part added (if you get the error in the following lines):
If Application.CountIf(Selection, "<>0") < 1 Then Exit Sub
'This will exit the sub; you can also "GoTo" a line as error handler or _
'Skip one step of a loop as you mentioned you are doing this for number of columns.

With ActiveWorkbook.SlicerCaches("Průřez_dispečer32111")
    .SlicerItems("Bodnariucová Renáta").Selected = False
    .SlicerItems("Kajer Roman").Selected = True
End With

'Also this cause it's not clear where you exactly get the error:
If Application.CountIf(Selection, "<>0") < 1 Then Exit Sub
'OR
If Application.CountIf(Sheets("Celkový harmonogram").Range("Tabulka141121518[13]") _
 , "<>0") < 1 Then Exit Sub


If Sheets("Celkový harmonogram").Range("Tabulka141121518[13]").Value <> "" Then
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("jednotlivci").Select
    Range("Y6").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
End If

相反,Exit Sub你可以转到另一行,如下所示:

If Criteria Then 
isError = True
GoTo ErrorHandler
End If

'Rest of the code in case of no error
'...

ErrorHandler:
If IsError Then
'Code you want to be performed in case of error
End If

注意:添加第二个 if 语句是为了避免在没有错误的情况下运行错误处理程序(VBA 运行所有内容并且不会跳过 ErrorHandler 块)。

相关内容