我下面是由要按顺序运行的几个查询组成的,但是当我到达最后一个查询时,它似乎无法删除(通过删除本身起作用),任何人都可以帮忙吗?
期望的行为:
在多张纸上采集一些数据
然后冻结第一行
然后将其格式化为表格
然后调整大小,居中和换行
然后搜索所有工作表并删除单词“ completed”所在的任何行。
具体问题:
似乎它没有执行最后一步(删除单词已完成的所有行)
实际上,它通过声明“范围错误”而在行rDelete.EntireRow.Delete上出错。
复制最短代码:
我认为下面的代码是最短的代码,除了消除最后一个宏以外的所有代码,但我不确定在尝试重现结果时是否会产生其他错误。
希望这能解决下面的Mat's Mug的评论,并且符合“最小,完整和可验证”的示例。
Sub TEST()
'
' Freeze_Panes Macro
'
' This one Freezes Row 1 (under Header)
Dim s As Worksheet
Dim c As Worksheet
' store current sheet
Set c = ActiveSheet
' Stop flickering...
Application.ScreenUpdating = False
' Loop throught the sheets
For Each s In ActiveWorkbook.Worksheets
' Have to activate - SplitColumn and SplitRow are properties of ActiveSheet
s.Activate
With ActiveWindow
.SplitColumn = 0
.SplitRow = 1
' .SplitRow = 2 'Depending on if it has a header maybe?
.FreezePanes = True
End With
Next
' Back to original sheet
c.Activate
Application.ScreenUpdating = True
Set s = Nothing
Set c = Nothing
Call Format_As_Table
End Sub
Private Sub Format_As_Table()
'
' Format_As_Table Macro
'
' Declaration
Dim Tbl As ListObject
Dim Rng As Range
Dim sh As Worksheet
Application.ScreenUpdating = False
' Loop Through All Sheets and Format All Data as Table, then Orient as Landscape
For Each sh In ActiveWorkbook.Sheets
With sh
Set Rng = .Range(.Range("A1"), .Range("A1").SpecialCells(xlLastCell))
Set Tbl = .ListObjects.Add(xlSrcRange, Rng, , xlYes)
Tbl.TableStyle = "TableStyleMedium15"
.PageSetup.Orientation = xlLandscape
End With
Next sh
Application.ScreenUpdating = False
Call Resize_Columns_And_Rows_No_Header
End Sub
Private Sub Resize_Columns_And_Rows_No_Header()
'
'Resize_Columns_And_Rows Macro
'
'Declaration
Dim wkSt As String
Dim wkBk As Worksheet
Dim temp As Variant
Dim lastCol As Long
wkSt = ActiveSheet.Name
' This Loops Through All Sheets
For Each wkBk In ActiveWorkbook.Worksheets
On Error Resume Next
wkBk.Activate
lastCol = wkBk.Cells(1, Columns.Count).End(xlToLeft).Column
'This is only needed if you are wrapping the text
wkBk.Rows.WrapText = True
'This is to center align all rows
'wkBk.Rows.VerticalAlignment = xlCenter
wkBk.Rows.HorizontalAlignment = xlCenter
'Resize Columns due to size
wkBk.Columns("F:F").ColumnWidth = 50
wkBk.Columns("G:G").ColumnWidth = 50
' Resize Rows
wkBk.Rows.EntireRow.AutoFit
' Resize Columns
wkBk.Columns.EntireColumn.AutoFit
Next wkBk
Sheets(wkSt).Select
Call TestDeleteRows
End Sub
Private Sub TestDeleteRows()
Dim rFind As Range
Dim rDelete As Range
Dim strSearch As String
Dim sFirstAddress As String
Dim sh As Worksheet
strSearch = "Completed"
Set rDelete = Nothing
Application.ScreenUpdating = False
For Each sh In ActiveWorkbook.Sheets
With sh.Columns("A:AO")
Set rFind = .Find(strSearch, LookIn:=xlValues, LookAt:=xlPart, SearchDirection:=xlNext, MatchCase:=False)
If Not rFind Is Nothing Then
sFirstAddress = rFind.Address
Do
If rDelete Is Nothing Then
Set rDelete = rFind
Else
Set rDelete = Application.Union(rDelete, rFind)
End If
Set rFind = .FindNext(rFind)
Loop While Not rFind Is Nothing And rFind.Address <> sFirstAddress
rDelete.EntireRow.Delete
Set rDelete = Nothing
End If
End With
Next sh
Application.ScreenUpdating = False
End Sub
最佳答案
似乎您已经在评论中找到解决方案。但是,我只是想提到以下内容:
当您重叠选择并尝试删除它们时,Excel不喜欢它。如果在同一行的多个单元格中都有单词“ Completed”,则最终会与rDelete.EntireRow.Delete
重叠。代替为具有“ Complete”的每个单元格创建并集,您应该简单地为每个ROW创建一个并集。
只需更改即可轻松完成Set rDelete = Application.Union(rDelete, rFind)
至Set rDelete = Application.Union(rDelete, Range("A" & rFind.Row))
最终,这将导致尝试将A1与A1(或任意行)合并,并且不会在rDelete范围内创建重复的引用。