使用VBA循环search一个不断变化的值列表并删除它们?

我一直在做一个excel工作表,我正在使用一个macros来运行一个“查找function”,在单独的工作表上find一个单词/短语,然后删除这个单词/短语所在的整个列。目前,感谢我的最后一篇文章( 有没有办法使用查找function与改变,用户input值在Excelmacros? )我有这样的:

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(这是用户input的单词/短语的位置),然后转到“预测”表格,并删除单词/短语所在的列以及随后的列,直到右侧下一个填充的列。 但是,我需要它做2件事。

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

任何有关“循环初始”问题的帮助将不胜感激! 我对VBA非常陌生,我只是被赋予了这个任务。 非常感谢!

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

任何有关“循环初始”问题的帮助将不胜感激!

我认为像这样的东西应该有助于你的第一点(未经testing,但这是使用另一个循环,直到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 

为了您的第二点,请在数组中存储要search的值列表。 您可以使用逗号分隔的string上的拆分function来创build一个数组。 用于较小的列表。 否则,您可能需要使用不同的方法来创build数组。

更新每个评论:

 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 

我一直在做一个excel工作表,我正在使用一个macros来运行一个“查找function”,在单独的工作表上find一个单词/短语,然后删除这个单词/短语所在的整个列。

如果我正确地理解了你 – 在我看来,你已经过分复杂化了你的观点:

 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 

如果你想做多个string,你可以这样做:

 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