[英]How do I make an Excel VBA macro to delete columns with many different strings as headers?
So I'm importing a large spreadsheet with anywhere from 20 to 40 headers at the top and I'd like to automatically (and quickly) delete all the columns with specific headers. 因此,我要导入一个大型电子表格,该电子表格的顶部应有20至40个标题,而我想自动(快速)删除所有具有特定标题的列。 I have a current code that works, but it loops through all the columns multiple times, each time looking for a different string.
我有一个当前的代码可以正常工作,但是它多次遍历所有列,每次都查找不同的字符串。 I would like it to loop through once and delete any column that has any of the strings to delete.
我希望它循环一次并删除具有要删除的任何字符串的任何列。
Here's a fraction of my code to explain a little better: 这是我的代码的一部分,可以更好地进行解释:
Sub SetUpICTRP()
‘MsgBox “Excel will refresh when macro is complete”
Application.ScreenUpdating = False
Dim c As Range
Dim SrchRng As Range
Dim lastColumn As Long
lastColumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set SrchRng = ActiveSheet.Range("A1", Cells(1, lastColumn))
Do
Set c = SrchRng.Find("column", LookIn:=xlValues)
If Not c Is Nothing Then c.EntireColumn.Delete
Loop While Not c Is Nothing
Do
Set c = SrchRng.Find("Erroneous", LookIn:=xlValues)
If Not c Is Nothing Then c.EntireColumn.Delete
Loop While Not c Is Nothing
Do
Set c = SrchRng.Find("Unnecessary", LookIn:=xlValues)
If Not c Is Nothing Then c.EntireColumn.Delete
Loop While Not c Is Nothing
Do
Set c = SrchRng.Find("Not_", LookIn:=xlValues)
If Not c Is Nothing Then c.EntireColumn.Delete
Loop While Not c Is Nothing
Application.ScreenUpdating = True
End Sub
So currently it will delete all the columns with headers that contain the phrase "column," "Erroneous," "Unnecessary", and "Not_". 因此,当前它将删除带有标题的所有列,这些标题包含短语“ column”,“ Er错误”,“不必要”和“ Not_”。 What I really need is for it to be able to find and delete columns like "column218", "columnadfe", "column099", "Not_needed", without running through a separate loop multiple times.
我真正需要的是它能够查找和删除“ column218”,“ columnadfe”,“ column099”,“ Not_needed”之类的列,而无需多次运行单独的循环。 I think the solution lies somehow in creating an array of strings and then looping through each string for each column, but I'm rather new to VBA and can't seem to make that work.
我认为解决方案在于以某种方式创建字符串数组,然后遍历每一列的每个字符串,但是我对VBA还是很陌生,似乎无法实现这一目标。 Any help would be great!
任何帮助将是巨大的!
You could loop through and find the words and use the Union()
method to collate your headers into a single Range object, then use the .EntireColumn
method to delete columns related to the non-contiguous cells. 您可以遍历并找到单词,然后使用
Union()
方法将标题整理到单个Range对象中,然后使用.EntireColumn
方法删除与非连续单元格相关的列。
Sub SO()
Dim findValue As Variant, findRange As Excel.Range, deleteRange As Excel.Range
Dim findAddress As String
For Each findValue In Array("column", "Erroneous", "Unnecessary", "Not_", _
"Other1", "Other2", "Other3", "etc...")
Set findRange = Rows(1).EntireRow.Find(what:=findValue, LookIn:=xlValues, lookat:=xlPart)
If Not findRange Is Nothing Then
findAddress = findRange.Address
Do
If Not deleteRange Is Nothing Then
Set deleteRange = Union(deleteRange, findRange)
Else
Set deleteRange = findRange
End If
Set findRange = Rows(1).EntireRow.FindNext(findRange)
Loop Until findRange Is Nothing Or findRange.Address = findAddress
Set findRange = Nothing
End If
Next findValue
If deleteRange Is Nothing Then
MsgBox "No columns were found for deletion!"
Else
deleteRange.EntireColumn.Delete
End If
End Sub
or following your original setup you could use something like: 或按照您的原始设置,您可以使用以下方法:
Sub SO()
Dim c As Range, findValue As Variant
For Each findValue In Array("column", "Erroneous", "Unnecessary", "Not_", _
"Other1", "Other2", "Other3", "etc...")
Do
Set c = Rows(1).EntireRow.Find(findValue, LookIn:=xlValues)
If Not c Is Nothing Then c.EntireColumn.Delete
Loop While Not c Is Nothing
Next findValue
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.