使用VBA循环搜索更改的值列表并删除它们?

时间:2013-07-26 13:56:52

标签: excel vba loops excel-vba

我一直在制作excel表格,我正在使用宏基本上运行“查找功能”以在单独的工作表上查找单词/短语,然后删除此单词/短语所在的整个列目前,感谢我的上一篇文章(Is there a way to use the find function with changing, user inputted values in an excel macro?),我有这个:

Dim strFind As String
Dim rngFind As Range
Dim i As Integer


strFind = Sheets("Instructions").Range("E56").Value
Set rngFind = Sheets("Forecast").Cells.Find(What:=strFind, LookAt:=xlPart)
check if value is found
Do While Not rngFind Is Nothing
If Not rngFind Is Nothing Then
   i = 0
        Do While rngFind.Offset(0, i + 1) = ""
            i = i + 1
        Loop
    rngFind.Resize(1, i + 1).EntireColumn.Delete Shift:=xlShiftToLeft
Loop
End If

这完全适用于引用单元格C53(用户输入单词/短语的位置),然后转到“预测”表单并删除单词/短语所在的列以及后续列右边直到下一个填充的列。但是,我还需要做两件事。

  • 目前它只找到一次这个词,即使它可能多次存在于工作表中,我需要它来删除所有这些 - 基本上它“找到下一个”而不是“找到全部”。我正在考虑创建一个循环,以便一旦找到并删除该列,它就会再次找到相同的单词并删除该列(以及它旁边的后续空行),直到该单词不再出现在工作表上。 / LI>
  • 其次,我需要它来找到多个单词/短语。现在它引用了单元格'C53',但我需要它然后继续'C54'并做同样的事情,然后'C55',然后'C56'等等。直到它到达一个只有一个'的单元格' 0'在里面。问题是,C列中的列表大小总是不同(但总是从C53开始)。一旦它达到“0”(不是空白单元格),我需要它停止。

对此'循环启动'问题的任何帮助将不胜感激!我对VBA很新,我刚刚完成了这个任务。非常感谢!

2 个答案:

答案 0 :(得分:2)

  

我一直在使用excel表,我正在使用宏   基本上运行'查找功能'来单独查找单词/短语   选择工作表,然后删除此单词/短语所在的整个列   位于。

如果我理解正确 - 在我看来,你的副本过于复杂:

Sub DeleteColumnsWithString()

Dim strFind As String
Dim rngFind As Range

strFind = Sheets("Instructions").Range("C53").Value

Do
    Set rngFind = Sheets("Forecast").Cells.Find(What:=strFind, LookAt:=xlPart)
    If Not rngFind Is Nothing Then
        rngFind.EntireColumn.Delete
    End If
Loop Until rngFind Is Nothing

End Sub

如果你想做多个字符串,你可以这样做:

Sub DeleteColumnsWithAnyOfTheseStrings()

Dim strFind As String
Dim rngFind As Range
Dim SearchTerms As Range

Set SearchTerms = Sheets("Instructions").Range("C53:C55")

For Each Cell In SearchTerms.Cells
    strFind = Cell.Value

    Do
        Set rngFind = Sheets("Forecast").Cells.Find(What:=strFind, LookAt:=xlPart)
        If Not rngFind Is Nothing Then
            rngFind.EntireColumn.Delete
        End If
    Loop Until rngFind Is Nothing
Next Cell

End Sub

根据您的评论更新:

Sub DeleteColumnsWithTheseStringsAndToTheRightBlanksColumns()

Dim strFind As String
Dim rngFind As Range
Dim strFindAddress As String
Dim SearchTerms As Range
Dim Counter As Long

Set SearchTerms = Sheets("Instructions").Range("C53:C55")

For Each Cell In SearchTerms.Cells
    Counter = 0
    strFind = Cell.Value

    Do
        Set rngFind = Sheets("Forecast").Cells.Find(What:=strFind, LookAt:=xlPart)
        If Not rngFind Is Nothing Then
            strFindAddress = rngFind.Address
            Do
                Range(strFindAddress).EntireColumn.Delete
                Counter = Counter + 1
            Loop Until Range(strFindAddress).Value <> "" Or Counter >= 100
        End If
    Loop Until rngFind Is Nothing
Next Cell

End Sub

答案 1 :(得分:1)

我认为你在这里有一个很好的基础。

  

对此'循环启动'问题的任何帮助将不胜感激!

我认为这样的事情应该有助于你的第一点(未经测试,但这是使用另一个Do循环的想法,直到rngFind Is Nothing):

Set rngFind = Sheets("Forecast").Cells.Find(What:=strFind, LookAt:=xlPart)
Do While Not rngFind Is Nothing '## Makes sure the value is found before operating the rest of the loop
    i = 0
        Do While rngFind.Offset(0, i + 1) = ""
            i = i + 1
        Loop
    rngFind.Resize(1, i + 1).EntireColumn.Delete Shift:=xlShiftToLeft
    '## Find the NEXT instanct of strFind within rngFind
    Set rngFind = Sheets("Forecast").Cells.FindNext(What:=strFind, LookAt:=xlPart)
Loop 

对于第二点,存储要在数组中搜索的值列表。您可以在逗号分隔的字符串上使用Split函数来创建数组。适用于较小的列表。否则,您可能需要使用其他方法来创建数组。

每条评论更新:

Dim valsToSearch() As Variant
Dim val As Variant
Dim numberOfValues as Long

numberOfValues = CInt(Application.InputBox("How Many Search Terms?"))
valsToSearch = Range("C53").Resize(numberOfValues,1)

然后,将上面的For/Next循环括起来迭代数组中的值:

For each val in valsToSearch
    strFind = val
    Set rngFind = Sheets("Forecast").Cells.Find(What:=strFind, LookAt:=xlPart)
    Do While Not rngFind Is Nothing '## Makes sure the value is found before operating the rest of the loop
        i = 0
            Do While rngFind.Offset(0, i + 1) = ""
                i = i + 1
            Loop
        rngFind.Resize(1, i + 1).EntireColumn.Delete Shift:=xlShiftToLeft
        '## Find the NEXT instanct of strFind within rngFind
        Set rngFind = Sheets("Forecast").Cells.FindNext(What:=strFind, LookAt:=xlPart)
    Loop 
Next