我一直在制作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(用户输入单词/短语的位置),然后转到“预测”表单并删除单词/短语所在的列以及后续列右边直到下一个填充的列。但是,我还需要做两件事。
对此'循环启动'问题的任何帮助将不胜感激!我对VBA很新,我刚刚完成了这个任务。非常感谢!
答案 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