我有这段代码(见下文),用于查找在删除所有需要删除的列时是否必须删除工作表前面的一列。代码找到最后使用的列并重新填充工作表,直到到达DO列为止。
但是,现在,从上次起,当该列已被填充时,我没有按下任何按钮。代码崩溃并说
执行时出现错误1004。类Range的Methode自动填充失败。
仅当尚未使用DO列时才起作用。
这是我的代码:
Dim lColumn As Long
Dim iCntr As Long
lColumn = 20
For iCntr = lColumn To 12 Step -1
If Cells(3, iCntr) = 0 Then
Columns(iCntr).Delete
End If
Next
With Workbooks("Bureauplanning2backup.xlsm").Worksheets("Planning")
Dim rngStart As Range
Set rngStart = .Cells(2, .Columns.Count).End(xlToLeft).Offset(,
-1).EntireColumn
Dim rngEnd As Range
Set rngEnd = .Cells(2, .Columns.Count).End(xlToLeft).EntireColumn
Dim rng As Range
Set rng = .Range(rngStart, .Columns("DO"))
Dim rngX As Range
Set rngX = .Range(rngStart, rngEnd)
End With
rngX.AutoFill rng, Type:=xlFillDefault
有没有一种方法可以检查DO列是否已被填充,并且当它被填充时,代码只是不会运行该行:
rngX.AutoFill rng, Type:=xlFillDefault
答案 0 :(得分:1)
您可以计算DO中有多少个单元格有数据
If Application.WorksheetFunction.CountA(.Columns("DO")) = 0 Then
'zero cells have data so autofill
rngX.AutoFill rng, Type:=xlFillDefault
Else
'at least one cell has data so don't autofill
MsgBox "Column DO has already data"
End If