[英]Looping through sheets to delete certain cells
I wish to delete specific cells from the sheets in a workbook. 我希望从工作簿的工作表中删除特定的单元格。 While doing so it should also delete specific cells having formula error in these worksheets of the workbook.
这样做时,还应删除工作簿的这些工作表中具有公式错误的特定单元格。 I used a recent program in stackoverflow by @Blind Seer as per following link which is for similar applications.
我使用@Blind Seer在stackoverflow中使用的最新程序,按照以下链接进行,该链接适用于类似的应用程序。
Sample of workbook sheets before program run are appended below 程序运行之前的工作簿工作表样本附在下面
Code adopted by me as follows. 我采用的代码如下。
Sub DeleteCells()
Dim rng As Range, rngError As Range, delRange As Range
Dim i As Long, j As Long, k As Long
Dim wks As Worksheet
On Error Resume Next
Set rng = Application.InputBox("Select cells To be deleted", Type:=8)
On Error GoTo 0
If rng Is Nothing Then Exit Sub Else rng.Delete
For k = 1 To ThisWorkbook.Worksheets.Count 'runs through all worksheets
Set wks = ThisWorkbook.Worksheets(k)
With wks
For i = 1 To 7 '<~~ Loop trough columns A to G
'~~> Check if that column has any errors
On Error Resume Next
Set rngError = .Columns(i).SpecialCells(xlCellTypeFormulas, xlErrors)
On Error GoTo 0
If Not rngError Is Nothing Then
For j = 1 To 100 '<~~ Loop Through rows 1 to 100
If .Cells(j, i).Text = "#DIV/0!" Then
'~~> Store The range to be deleted
If delRange Is Nothing Then
Set delRange = .Columns(i)
Exit For
Else
Set delRange = Union(delRange, .Columns(i))
End If
End If
Next j
End If
Next i
End With
Next k
'~~> Delete the range in one go
If Not delRange Is Nothing Then delRange.Delete
End Sub
After running the code it deletes the cell input in the input box ie it blanks out the data in the cell and append rest of the data in the row at the end of the last filled row. 运行代码后,它将删除输入框中的单元格输入,即,将单元格中的数据清空,并将其余数据追加到最后填充的行末尾的行中。 It is not blanking out error cells and the program gives the error message: Method 'Union of object_Global failed
它不会清除错误单元格,并且程序会给出错误消息:方法'object_Global的联合失败
on the following code line 在以下代码行上
'Set delRange = Union(delRange, .Columns(i))' '设置delRange = Union(delRange,.Columns(i))'
Sample data after proram run is appended below. 试运行后的样本数据如下。
Please help in locating the error in the program. 请帮助找出程序中的错误。 Result desired is Input cell range should blank out retaining its row position.
所需结果是输入单元格范围应空白以保留其行位置。 Same also for error cells.
对于错误单元格也是如此。
Thanks 谢谢
Option Explicit
Sub DeleteCells()
Dim ws As Worksheet, rng As Range, rngErr As Range
On Error Resume Next
Set rng = Application.InputBox("Select cells to be deleted", Type:=8)
If Not rng Is Nothing Then
rng.Delete
For Each ws In ThisWorkbook.Worksheets
Set rngErr = ws.UsedRange.SpecialCells(xlCellTypeFormulas, xlErrors)
If Not rngErr Is Nothing Then rngErr.Clear
Next
End If
End Sub
Not much of a solution but the reason for the error is that Union does not work across worksheets. 解决方案不多,但出现错误的原因是,Union无法跨工作表工作。 It will work for ranges on a single sheet.
适用于单张纸上的范围。
You could adapt your code to work one sheet at a time: 您可以修改代码以一次处理一张纸:
Sub DeleteCells()
Dim rng As Range, rngError As Range, delRange As Range
Dim i As Long, j As Long, k As Long
Dim wks As Worksheet
On Error Resume Next
Set rng = Application.InputBox("Select cells To be deleted", Type:=8)
On Error GoTo 0
If rng Is Nothing Then Exit Sub Else rng.Delete
For k = 1 To ThisWorkbook.Worksheets.Count 'runs through all worksheets
'Set wks = ThisWorkbook.Worksheets(k)
'With wks
With ThisWorkbook.Worksheets(k) '<<do each sheet individually so that Union functions as expected
For i = 1 To 7 '<~~ Loop trough columns A to G
'~~> Check if that column has any errors
On Error Resume Next
Set rngError = .Columns(i).SpecialCells(xlCellTypeFormulas, xlErrors)
On Error GoTo 0
If Not rngError Is Nothing Then
For j = 1 To 100 '<~~ Loop Through rows 1 to 100
If .Cells(j, i).Text = "#DIV/0!" Then
'~~> Store The range to be deleted
If delRange Is Nothing Then
Set delRange = .Columns(i)
Exit For
Else
Set delRange = Union(delRange, .Columns(i))
End If
End If
Next j
End If
Next i
End With
Next k
'~~> Delete the range in one go
If Not delRange Is Nothing Then delRange.Delete
End Sub
You can use IsNumeric to check if the cells contain numeric values. 您可以使用IsNumeric检查单元格是否包含数字值。 Errors are not numeric values, so IsNumeric(Cell with error) = False.
错误不是数值,所以IsNumeric(有错误的单元格)= False。 I modified your code:
我修改了您的代码:
Set wks = ThisWorkbook.Worksheets(k)
With wks
For i = 1 To 7 '<~~ Loop trough columns A to G
'~~> Check if that column has any errors
On Error Resume Next
Set rngError = .Columns(i).SpecialCells(xlCellTypeFormulas, xlErrors)
On Error GoTo 0
If Not rngError Is Nothing Then
For j = 1 To 100 '<~~ Loop Through rows 1 to 100
If Not IsNumeric(.Cells(j, i)) Then
'~~> Store The range to be deleted
If delRange Is Nothing Then
Set delRange = .Columns(i)
Exit For
Else
Set delRange = Union(delRange, .Columns(i))
End If
End If
Next j
End If
Next i
End With
Next k
'~~> Delete the range in one go
If Not delRange Is Nothing Then delRange.Delete
End Sub
Note: This means that if sometimes text is to be entered, it will count it as an ERROR. 注意:这意味着,如果有时要输入文本,它将被视为错误。 So be careful if that's the case!
因此,请小心!
Also, as per my comment; 另外,根据我的评论; If your cells are quotients of cells on other cells, consider using this instead of code;
如果您的单元格是其他单元格上的单元格的商,请考虑使用此替代代码。 support.microsoft.com/en-us/kb/182188.
support.microsoft.com/en-us/kb/182188。 To simply skip the divisions.
简单地跳过除法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.