[英]Delete all columns in all sheets that contain specific word
I tried to modify the the below macro (taken elsewhere on internet) so that it applies to all the sheets within the Excel file. 我试图修改下面的宏(在互联网上的其他地方使用),以便将其应用于Excel文件中的所有工作表。 However it didn't work as expected. 但是,它没有按预期工作。 How do I make it work. 我如何使其工作。
Sub Col_Delete_by_Word_2()
Dim Found As Range, strWord As String, Counter As Long
Dim CurrentSheet As Object
Dim ws As Worksheet
strWord = Application.InputBox("Enter the word to search for.", _
"Delete the columns with this word", Type:=2)
If strWord = "False" Or strWord = "" Then Exit Sub 'User canceled
Set Found = Cells.Find(strWord, , , xlPart, , xlNext, False)
For Each ws In ActiveWorkbook.Worksheets
If Not Found Is Nothing Then
Application.ScreenUpdating = False
Do
Found.EntireColumn.Delete
Counter = Counter + 1
Set Found = Cells.Find(strWord, , , xlPart, , xlNext, False)
Loop Until Found Is Nothing
Application.ScreenUpdating = True
MsgBox Counter & " columns deleted.", vbInformation, "Process Complete"
Else
MsgBox "No match found for: " & strWord, vbInformation, "No Match"
End If
Next
End Sub
The problem is that you are not searching for the word in the loop. 问题是您没有在循环中搜索单词。 Also if you delete columns in the loop then the code will become slow. 同样,如果您在循环中删除列,则代码将变慢。 Store it in a rage variable and then delete it in one go when the search is over for that sheet. 将其存储在rage变量中,然后在该表的搜索结束时一次将其删除。
Also when you are setting off Application
events then use error handling so that if the code breaks, it can be set back to defaults. 同样,当您关闭Application
事件时,请使用错误处理,以便如果代码中断,则可以将其设置回默认值。 Another good thing would be to set the calculation to manual before the macro runs. 另一件事是在宏运行之前将计算设置为手动。
Is this what you are trying ( TRIED AND TESTED )? 这是您正在尝试的(“ 尝试并测试”的 )吗? I have commented the code so you shouldn't have any problem understanding it. 我已经注释了代码,因此您在理解它时应该没有任何问题。 However if you do then simply post back :) 但是,如果您这样做,则只需发回:)
Option Explicit
Sub Col_Delete_by_Word_2()
Dim ws As Worksheet
Dim aCell As Range, bCell As Range, delRange As Range
Dim strWord As Variant
Dim appCalc As Long
On Error GoTo Whoa
'~~> Set the events off so that macro becomes faste
With Application
.ScreenUpdating = False
appCalc = .Calculation
.Calculation = xlCalculationManual
End With
'~~> Take the input from user
strWord = Application.InputBox("Enter the word to search for.", _
"Delete the columns with this word", Type:=2)
'~~> Check if user pressed cancel orr is it a blank input
If strWord = "False" Or strWord = "" Then Exit Sub
'~~> Loop theough the worksheets
For Each ws In ThisWorkbook.Worksheets
With ws.Cells
'~~> Find the search text
Set aCell = .Find(What:=strWord, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
'~~> If FOund
If Not aCell Is Nothing Then
Set bCell = aCell
'~~> Instead of deleting the column in a loop
'~~> We will store it in a range so that we can
'~~> delete it later
Set delRange = aCell
'~~> Find Next
Do
Set aCell = .FindNext(After:=aCell)
If Not aCell Is Nothing Then
If aCell.Address = bCell.Address Then Exit Do
Set delRange = Union(delRange, aCell)
Else
Exit Do
End If
Loop
End If
'~~> Delete the columns in one go
If Not delRange Is Nothing Then _
delRange.EntireColumn.Delete Shift:=xlToLeft
End With
Next
LetsContinue:
'~~> Reset events
With Application
.ScreenUpdating = True
.Calculation = appCalc
End With
Exit Sub
Whoa:
MsgBox Err.Description
Resume LetsContinue
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.