如果为空,如何删除整列

时间:2013-02-21 15:20:54

标签: excel excel-vba excel-2010 vba

我有专栏 第1行正在前进..

IF Column C doesn't have data in entire column then delete C D E F
IF Column D doesn't have data in entire column then delete D E F
IF Column E doesn't have data in entire column then delete E F
IF Column F doesn't have data in entire column then delete F

标题NN的相同方式

IF Column G doesn't have data in entire column then delete G H I J K L M N 
IF Column H doesn't have data in entire column then delete H I J K L M N 
IF Column I doesn't have data in entire column then delete I J K L M N 
IF Column J doesn't have data in entire column then delete J K L M N 
IF Column K doesn't have data in entire column then delete K L M N 
IF Column L doesn't have data in entire column then delete L M N 
IF Column M doesn't have data in entire column then delete M N 
IF Column N doesn't have data in entire column then delete N


A   B   C   D   E   F   G   H   I   J   K   L   M   N   O   P   Q   R
TI1 TE2 TT1 TT2 TT3 TT4 NN1 NN2 NN3 NN4 NN5 NN6 NN7 NN8 CMT K2  K3  
BLAH    BLAH

2 个答案:

答案 0 :(得分:2)

我会通过添加一个额外的行来解决这个问题(你可以随时隐藏它)。

假设您的标题位于第1行,然后在第2行添加额外的(帮助)行。

  • 在C2中,输入以下公式(将C30替换为适当的值):

    =COUNTA(C3:C30)

  • 在D2中,输入以下公式:

    =IF(C2=0,0,COUNTA(D3:D30))

  • 将该公式拖到E& ˚F

  • 重做C的公式

  • 重做D中的公式并拖到N

......等等。

现在只需编写一个非常简单的宏来删除row2的值= 0的任何列。

这对我来说似乎是最简单的方法,但还有其他方法。

希望这会有所帮助!!

答案 1 :(得分:0)

这个宏做你的第一个位 - 第二个阶段非常相似。

你需要将5变为你桌子的长度。

GoTo通常不受欢迎,但我已在此代码中使用过它。

Sub firstbit()

If Excel.WorksheetFunction.CountA(ActiveSheet.Range("C2:C5")) = 0 Then
    Excel.ActiveSheet.Range("C2:F5").clearcontents
    GoTo FirstStageComplete:
End If
If Excel.WorksheetFunction.CountA(ActiveSheet.Range("D2:D5")) = 0 Then
    Excel.ActiveSheet.Range("D2:F5").clearcontents
    GoTo FirstStageComplete:
End If
If Excel.WorksheetFunction.CountA(ActiveSheet.Range("E2:E5")) = 0 Then
    Excel.ActiveSheet.Range("E2:F5").clearcontents
    GoTo FirstStageComplete:
End If
If Excel.WorksheetFunction.CountA(ActiveSheet.Range("F2:F5")) = 0 Then
    Excel.ActiveSheet.Range("F2:F5").clearcontents
End If
FirstStageComplete:

End Sub