0

我一直在处理 Excel 工作表,我使用宏基本上运行“查找功能”以在单独的工作表上查找单词/短语,然后删除该单词/短语所在的整个列。目前,感谢我的上一篇文章(有没有办法使用 find 函数在 excel 宏中更改用户输入的值?)我有这个:

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

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

任何有关此“循环启动”问题的帮助将不胜感激!我对 VBA 很陌生,我刚刚被赋予了这样做的任务。非常感谢!

4

2 回答 2

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
于 2013-07-26T14:24:40.027 回答
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
于 2013-07-26T14:14:46.260 回答