我做了一些谷歌搜索,无法弄清楚如何同时删除多列。由于将要输入到该工具中的数据不一致,因此我必须使用find函数来表示要删除哪一列。目前,只有第一个删除功能有效,其余功能将被忽略。我已经使用消息框来确保查找功能正确地提取了正确的列索引。有谁对删除多个有任何想法吗?谢谢!
Vertical = Worksheets("Target List").Rows("1").Find("Vertical").Column
DistID = Worksheets("Target List").Rows("1").Find("Distributor ID").Column
DistName = Worksheets("Target List").Rows("1").Find("Distributor Name").Column
Sheets("Target List").Select
Columns(Vertical).EntireColumn.Delete
Columns(DistID).EntireColumn.Delete
Columns(DistName).EntireColumn.Delete
答案 0 :(得分:4)
尝试
dim i as long, col as variant, c as variant
col = array("Vertical", "Distributor ID", "Distributor Name")
with Worksheets("Target List")
for i=lbound(col) to ubound(col)
c= application.match(col(i), .rows(1), 0)
if not iserror(c) then .columns(c).entirecolumn.delete
next i
end with
尝试一次删除所有列将要求反复进行错误控制。最好一次只删除一个。
答案 1 :(得分:1)
我个人觉得更容易理解的另一种方法:
Dim sht As Worksheet
Dim Lastcolumn As Long
Dim y As Long
Set sht = ThisWorkbook.Sheets(1)
Lastcolumn = sht.Cells(1, sht.Columns.Count).End(xlToLeft).Column
For y = Lastcolumn To 1 Step -1
If sht.Cells(1, y) = "Vertical" Or sht.Cells(1, y) = "Distributor ID" Or sht.Cells(1, y) = "Distributor Name" Then
sht.Columns(y).EntireColumn.Delete
End If
Next
当您不完全了解标题字符串时,也可以使用Instr()代替精确比较。
希望有帮助。