又一个问题,
我想在myList
中delete
所有列not
我的类型不匹配我很确定这是问题AllHeaders = Array(1, UBound(headname, 2))
我没有制作一个正确的标题名称数组
由于
编辑:UpDated with
正确的AllHeaders数组AllHeaders = Range("A1", Cells(1, Columns.Count).End(xlToLeft)).Value
仍然会删除For
和If
中我的逻辑中的所有内容。
Sub DeleteColsNotInList()
Dim wsSource As Excel.Worksheet
Dim myList As Variant
Dim AllHeaders As Variant
Dim hName As Variant
Dim headname
Dim Destination As Range
Dim iCol As Range
ActiveWorkbook.Sheets("Source").Select
myList = Array("user id", "user name") 'My list of header names to keep
headname = ActiveCell.CurrentRegion.Rows(1).Value
AllHeaders = Headers = Range("A1", Cells(1, Columns.Count).End(xlToLeft)).Value 'Collect all header names into an array
For Each hName In myList
For Each iCol In AllHeaders
If iCol <> hName Then
iCol.Delete
End If
Next hName
Next iCol
End Sub
答案 0 :(得分:1)
也许是这样的:
Sub colKiller()
Set r = ActiveSheet.UsedRange
nLastColumn = r.Columns.Count + r.Column - 1
nFirstColumn = r.Column
For i = nLastColumn To nFirstColumn Step -1
v = Cells(1, i).Value
If v = "user id" Or v = "user name" Then
Else
Cells(1, i).EntireColumn.Delete
End If
Next i
End Sub