简体   繁体   English

如何使Excel VBA宏删除带有许多不同字符串作为标题的列?

[英]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.

相关问题 如何在 Excel VBA 中使用间接 Function 将方程合并到 VBA 宏 Function - How do I use the Indirect Function in Excel VBA to incorporate the equations in a VBA Macro Function 如何比较不同工作簿中的两个字符串(Excel VBA) - How to compare two strings in different workbooks (Excel VBA) 如何在VBA的单元格范围内搜索excel,以获取不等于“短语”的字符串值 - How do I search through a Range of cells in VBA for excel for values that are a strings not equal to “phrase” 如何删除数组中的空字符串 - How do I delete empty strings in array Excel:如何求和与不同列中的某些字符串关联的值 - Excel: How to sum values associated with certain strings in different columns Excel VBA:宏未初始化单元格值的字符串,不接受站点连接 - Excel VBA: Macro not Initializing Strings for Cell Value, not Accepting Site Connection 如何使Excel电子表格范围存储VBA变量? - How do you make an Excel spreadsheet range store a VBA variable? 如何在不同文本框中的阵列中显示所有不同的字符串? - How do I show all different strings in an ARRAY in DIFFERENT textboxes? 如何有条件地从数组中删除字符串? - How do I conditionally delete strings from an array? Excel - 计算单元格块中有多少字符串 - Excel - Counting how many Strings in cell block
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM