如果标题包含“Group time:”
,我有一个删除列的宏Sub remove_columns()
For i = ActiveSheet.Columns.Count To 1 Step -1
If InStr(1, Cells(1, i), "Group time:") Then Columns(i).EntireColumn.Delete
Next i
End Sub
我还有其他列需要删除“总时间”,“最后一页”,“开始语言”...大约10个。
我可以创建上述宏的10个版本但是有一种方法只有一个宏,它有一个名称列表并删除那些列
由于
答案 0 :(得分:3)
Sub remove_columns()
Dim rngFound As Range
Dim rngDel As Range
Dim arrColumnNames() As Variant
Dim varName As Variant
Dim strFirst As String
arrColumnNames = Array("Group time:", "Total time", "Last page", "Start language")
For Each varName In arrColumnNames
Set rngFound = Rows(1).Find(varName, Cells(1, Columns.Count), xlValues, xlPart)
If Not rngFound Is Nothing Then
strFirst = rngFound.Address
Do
If rngDel Is Nothing Then Set rngDel = rngFound Else Set rngDel = Union(rngDel, rngFound)
Set rngFound = Rows(1).Find(varName, rngFound, xlValues, xlPart)
Loop While rngFound.Address <> strFirst
End If
Next varName
If Not rngDel Is Nothing Then rngDel.EntireColumn.Delete
Set rngFound = Nothing
Set rngDel = Nothing
Erase arrColumnNames
End Sub