我有下面的代碼,它洗掉了 C 列中的值不是命名范圍(“ReferenceLocations”)的所有行。
' Defines variables
Dim Cell As Range, cRange As Range, LastRow As Long, x As Long, TestRange As Range, MyRange As Range
'DELETE IRRELEVANT LOCATIONS
' Defines LastRow as the last row of data based on column C
LastRow = Sheets(Sheet1).Cells(Rows.Count, "C").End(xlUp).Row
' Sets check range as C1 to the last row of C
Set cRange = Range("C2:C" & LastRow)
' For each cell in the check range, working from the bottom upwards
For x = cRange.Cells.Count To 1 Step -1
With cRange.Cells(x)
' If the cell does not contain one of the values listed in "ReferenceLocations" named range, delete rows
If IsError(Application.Match(.Value, Range("ReferenceLocations"), 0)) Then
.EntireRow.Delete
End If
End With
' Check next cell, working upwards
Next x
我現在的目標是在“IsError...Then”行中添加一個 And 條件,它將右側從 (X) 列到 P 列的單元格的值相加,其中 (X) 由 a 中的值給出另一張紙上的單元格(可能是偏移量)。如果滿足上述代碼中的條件并且 (X) 列中的單元格總和到 P = 0,則該函式應僅洗掉整行。
uj5u.com熱心網友回復:
為了得到總和的值,我們只需要給SUM公式一個范圍。我們知道我們是從一個單元格開始的,如果是 4 和8,其右側的一些列cRange.Cells(x)可以寫成cRange.Cells(x).Offset(, y)類似于“K5” 。我們想要到達的第二個單元格是“ P5" 如果是 4。所以我們可以使用該函式創建一個從一個單元格延伸到另一個單元格的范圍。. 如果是 4 和8 ,這將是“K5:P5” 。xycRange.Cells(x).EntireRow.Cells(16)xRangeRange(cRange.Cells(x).Offset(, y), cRange.Cells(x).EntireRow.Cells(16))xy
為了使用該With陳述句,并且不重復 with 塊內的物件,我們可以將其改寫為.Parent.Range(.Offset(, y), .EntireRow.Cells(16))
If IsError(Application.Match(.Value, Range("ReferenceLocations"), 0)) _
And WorksheetFunction.Sum(.Parent.Range(.Offset(, y), .EntireRow.Cells(16))) = 0 _
Then
.EntireRow.Delete
End If
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/518022.html
標籤:擅长vba
上一篇:VBA如何為“每一行”獲取屬性
