我有一个excel文件,从A到GM。在第3行中,我有列标题,例如,如下所示:
Sub bar Sub Sim bar Sub IV bar
1 1 0 1 1 0 0 4
1 1 1 0 0 1 1 0
0 1 0 1 1 1 0 1
0 1 0 1 3 0 0 0
0 1 1 0 1 1 1 1
0 0 0 0 0 0 1 1
0 0 0 1 1 0 1 1
1 0 0 1 1 1 0 1
1 1 1 1 1 0 0 1
1 0 0 1 0 1 0 1
1 4 0 0 1 1 0 0
我有几个列标题(sub,bar,sim,...)我想选择具有特定标题列的列,例如“bar”,并在表2中查看它们,如下所示:
bar bar bar
1 1 4
1 0 0
1 1 1
1 3 0
1 1 1
0 0 1
0 1 1
0 1 1
1 1 1
0 0 1
4 1 0
我知道如何使用vlookup和filter做到这一点。但是,如果可能的话,我想使用简单的VBA。可能吗?
你可以试试这个:
Sub main()
Worksheets("Sheet1").Range("A3").CurrentRegion.Copy Destination:=Worksheets("Sheet2").Range("A3") 'copy data from sheet1 to sheet2
With Worksheets("Sheet2").Range("A3:GM3") 'reference sheet2 headings
.Replace what:="bar", lookat:=xlWhole, replacement:="" ' replace wanted heading with blank
.SpecialCells(xlCellTypeBlanks).EntireColumn.Hidden = True ' hide blank headings columns
.SpecialCells(xlCellTypeVisible).EntireColumn.Delete ' delete visible heading columns
.EntireColumn.Hidden = False ' unhide hidden columns
.Value = "bar" 'write wanted heading back
End With
End Sub
只需将“Sheet1”和“Sheet2”更改为您的实际工作表名称