如何在VBA中删除带有红色标题的列

时间:2016-05-25 06:45:39

标签: excel-vba excel-formula vba excel

我有红色标题的文件,我想使用VBA代码删除整个列? 我可以使用循环查找红色标题并删除列而不是列Countrt ??

1 个答案:

答案 0 :(得分:1)

删除If Cells(1,ICntr). Font.ColorIndex = 3 ThenCells(1,ICntr).之间的行Font.ColorIndex中的空格,同时将Columns(ICntr).EntireColumn.Delete更改为Columns(ICntr).Delete

*************编辑************:
假设你的标题中有数据/文本(列的第一个单元格),这个宏就能解决问题。请记住,当循环通过ROWS或COLUMNS并删除时,您应该始终反转循环以确保检查所有行或列!

Sub DeleteColumnsWithRedHeader()

Dim ws As Worksheet
Dim lColumn As Long
Dim i As Long

'Change to you sheet name!!!!!!!!!!!!!
Set ws = Sheets("YOUR_SHEET_NAME")

'This will give you the last column index with data
lColumn = ws.Cells(1, Columns.Count).End(xlToLeft).Column

'Loop through all column with data - REVERSE LOOP TO CHECK ALL COLUMNS
For i = lColumn To 1 Step -1

    'If first cell in column is RED then delete that column
    If ws.Cells(1, i).Interior.ColorIndex = 3 Then ws.Columns(i).Delete

Next i

End Sub