Vba 如何使用“查找列索引”功能同时删除多个列
我用谷歌搜索了一下,不知道如何同时删除多个列。由于输入到该工具中的数据不一致,我必须使用查找功能来指示要删除的列。现在,只有第一个delete函数起作用,其余的被忽略。我使用MessageBox来确保find函数正确地提取了正确的列索引。有人对如何删除多个文件有什么想法吗?谢谢Vba 如何使用“查找列索引”功能同时删除多个列,vba,excel,Vba,Excel,我用谷歌搜索了一下,不知道如何同时删除多个列。由于输入到该工具中的数据不一致,我必须使用查找功能来指示要删除的列。现在,只有第一个delete函数起作用,其余的被忽略。我使用MessageBox来确保find函数正确地提取了正确的列索引。有人对如何删除多个文件有什么想法吗?谢谢 Vertical = Worksheets("Target List").Rows("1").Find("Vertical").Column DistID = Worksheets("Target List").Rows
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
试试看
试图一次删除所有列将需要重复错误控制。最好一次删除一个。这是我个人认为更容易理解的另一种方法:
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()来代替精确比较
希望有帮助。什么都没发生:/F8时,本地窗口中的c值是多少?我很笨!成功了。我看错了工作表。非常感谢你!
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