[英]Excel VBA 4 step macro not executing last step - Range Error
I have the below which is made of several queries that I want to run in order but it appears that when I get to the last one it doesn't delete (tho the delete by itself works) can anyone help with this? 我有下面的几个查询,我想按顺序运行,但似乎当我到达最后一个它不删除(由于删除本身工作)任何人都可以帮忙吗?
Desired Behavior: Take some data on multiple sheets Then Freeze the first row Then Format it as a Table Then resize, center and wrap text Then search through all sheets and delete any row where the word "completed" exists. 期望的行为:在多张纸上获取一些数据然后冻结第一行然后将其格式化为表然后调整大小,居中并换行文本然后搜索所有工作表并删除存在单词“completed”的任何行。
Specific Problem: It appeared that it was not doing the final step (removing all rows with the word completed) Actually it was erring on the row rDelete.EntireRow.Delete by stating "range error" 具体问题:它似乎没有做最后一步(删除所有行已完成的单词)实际上它在行rrdlete.EntireRow.Delete错误地指出“范围错误”
Shortest Code to Reproduce: I think the below is the shortest code, other than just eliminating all but the last macro, but I am not sure if that would create other errors when trying to reproduce results. 最短代码重现:我认为下面是最短的代码,除了消除除最后一个宏以外的所有代码,但我不确定在尝试重现结果时是否会产生其他错误。
Hope this addresses Mat's Mug's Comment below and is in line with the Minimal, Complete, and Verifiable example. 希望这可以解决下面的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
It seems like you are working out a solution in the comments already. 您似乎已经在评论中找到了解决方案。 However, I just thought I'd mention the following: 但是,我只是想提到以下内容:
Excel does not like it when you overlap selections and attempt to delete them. 当您重叠选择并尝试删除它们时,Excel不喜欢它。 If you have the word "Completed" in multiple cells on the same row, you end up with an overlap with rDelete.EntireRow.Delete
. 如果在同一行的多个单元格中有“已完成”一词,则最终会与rDelete.EntireRow.Delete
重叠。 Instead of creating a union of each cell that has "Complete" you should simple create a union of each ROW. 您应该简单地创建每个ROW的并集,而不是创建具有“完成”的每个单元格的并集。
This can be done easily by changing 这可以通过改变轻松完成
Set rDelete = Application.Union(rDelete, rFind)
to 至
Set rDelete = Application.Union(rDelete, Range("A" & rFind.Row))
This ultimately results in trying to union A1 with A1 (or whichever row) and does not create a duplicated reference in the range rDelete. 这最终会导致尝试将A1与A1(或任何一行)联合起来,并且不会在rDelete范围内创建重复引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.