我使用此代码在同一工作簿中的多个表格中的不同行表格中查找单元格(10,2)中写入的单词,当找到该单词时,代码将删除每个表格中的整行,问题是代码应用于命令按钮所在的第一个工作表,而不是应用于其他工作表,所以请在此帮助。
sub Deletrows_Click()
Dim WS As Worksheet
Dim pattern As String
For Each WS In ThisWorkbook.Worksheets
With WS
pattern = Cells(10, 2) ' delete row if found the word total in it
RowCount = ActiveSheet.UsedRange.Rows.Count
Dim i As Integer
For i = 2 To RowCount
Dim j As Integer
For j = 1 To 3 'find the word within this range
If Cells(i, j) = pattern Then
Cells(i, j).EntireRow.Delete
End If
Next j
Next i
End With
Next WS
End Sub
答案 0 :(得分:2)
您需要通过添加Range
作为前缀,在Cells
语句中完全限定所有With WS
和.
。
E.g。 pattern = Cells(10, 2)
代替pattern = .Cells(10, 2)
使用.Cells(10, 2)
,WS
代表For Each WS In ThisWorkbook.Worksheets
中的Option Explicit
Sub Deletrows_Click()
Dim WS As Worksheet
Dim pattern As String
Dim RowCount As Long, i As Long, j As Long
For Each WS In ThisWorkbook.Worksheets
With WS
pattern = .Cells(10, 2) ' delete row if found the word total in it
RowCount = .UsedRange.Rows.Count
For i = 2 To RowCount
For j = 1 To 3 'find the word within this range
If .Cells(i, j) = pattern Then
.Cells(i, j).EntireRow.Delete
End If
Next j
Next i
End With
Next WS
End Sub
的单元格(10,2)。
<强>代码强>
For
选项2 :您可以使用For
函数替换第二个Application.Match
循环,而不是使用两个Option Explicit
Sub Deletrows_Click()
Dim WS As Worksheet
Dim pattern As String
Dim RowCount As Long, i As Long, j As Long
For Each WS In ThisWorkbook.Worksheets
With WS
pattern = .Cells(10, 2) ' delete row if found the word total in it
RowCount = .UsedRange.Rows.Count
For i = 2 To RowCount
' use the Match function to find the word inside a certain row
If Not IsError(Application.Match(pattern, .Range(.Cells(i, 1), .Cells(i, 3)), 0)) Then '<-- match was successful
.Cells(i, 1).EntireRow.Delete
End If
Next i
End With
Next WS
End Sub
循环,以查找特定值整个行。
匹配代码
Option Explicit
Sub Deletrows_Click()
Dim WS As Worksheet
Dim pattern As String
Dim FirstRow As Long, RowCount As Long, i As Long, j As Long
Dim FirstCol, ColCount As Long
For Each WS In ThisWorkbook.Worksheets
With WS
pattern = .Cells(10, 2) ' delete row if found the word total in it
FirstRow = .UsedRange.Row
RowCount = .UsedRange.Rows.Count
FirstCol = .UsedRange.Column
ColCount = .UsedRange.Columns.Count
For i = 2 To RowCount + FirstRow
' use the Match function to find the word inside a certain row
If Not IsError(Application.Match(pattern, .Range(.Cells(i, 1), .Cells(i, ColCount + FirstCol)), 0)) Then '<-- match was successful
.Cells(i, 1).EntireRow.Delete
End If
Next i
End With
Next WS
End Sub
编辑2 :
from heapq import heapify, heappush
n = 35000 # input size
# way A: using heapify
dist = []
for i in range(n):
dist.push(distance) # distance is computed in O(1) time
heapify(dist)
# way B: using heappush
dist = []
for i in range(n):
heappush(dist, distance) # distance is computed in O(1) time
答案 1 :(得分:0)
Sub Deletrows_Click()
Dim WS As Worksheet
Dim pattern As String
Dim FirstRow As Long, RowCount As Long, i As Long, j As Long
Dim FirstCol, ColCount As Long
For Each WS In ThisWorkbook.Worksheets
With WS
pattern = Sheets("Sheet1").Cells(10, 2) ' delete row if found the word in this source sheet
FirstRow = .UsedRange.Row
RowCount = .UsedRange.Rows.Count
FirstCol = .UsedRange.Column
ColCount = .UsedRange.Columns.Count
For i = 2 To RowCount + FirstRow
' use the Match function to find the word inside a certain row
If WS.Name <> "Sheet1" Then 'I added this to exclude the said sheet as a source page
If Not IsError(Application.Match(pattern, .Range(.Cells(i, 1), .Cells(i, ColCount + FirstCol)), 0)) Then '<-- match was successful
.Cells(i, 1).EntireRow.Delete
End If
End If
Next i
End With
Next WS
End Sub