[英]Excel VBA is Finding Every Other Cell not Every Cell From Method
Excel VBA is finding every other cell using a method to check for Empty Cells. Excel VBA正在使用检查空单元格的方法查找所有其他单元格。 On the next time running the same macro, it then finds the cell that it skipped over on the last run while again skipping the next instance of an empty cell.
下次运行相同的宏时,它会找到上次运行时跳过的单元格,同时再次跳过空单元格的下一个实例。 If I cycle through the macro a few times, eventually every row without data is getting deleted, as per the purpose of the macro.
如果我在宏中循环几次,最终根据宏的目的,没有数据的每一行都会被删除。 The rows do shift upward upon deletion of the row one at a time, I will try a Union and delete the Range as stated by @BigBen
一次删除一行后,行确实向上移动,我将尝试联合并删除@BigBen 所述的范围
When a cell that is empty is found, it checks columns A, B, and D to see if formula is applied, and if a formula exists in that row, the entire row gets deleted.当找到空单元格时,它会检查 A、B 和 D 列以查看是否应用了公式,如果该行中存在公式,则整行将被删除。
Dim cel, dataCells As Range
Dim rngBlank, dc As Range
Dim lastRow, cForm, c, blnkRange As String
Dim cycleTimes As Integer
On Error Resume Next
Set dataCells = Range("F2:W2").Cells 'This is header of the table of data
cycleTimes = dataCells.Count 'Number of times to cycle through macro
For Count = 1 To cycleTimes 'I don't want to cycle through macro
lastRow = Range("N" & Rows.Count).End(xlUp).Row 'To find end of column
For Each dc In dataCells
c = Split(Cells(1, dc.Column).Address, "$")(1) 'Column Letter
blnkRange = c & "3:" & c & lastRow 'Range to look over for empty cells
Set rngBlank = Range(blnkRange).SpecialCells(xlCellTypeBlanks).Cells
For Each cel In rngBlank '**Skipping Every other Row**
If Not TypeName(cel) = "Empty" Then
cForm = "A" & cel.Row & ",B" & cel.Row & ",D" & cel.Row 'Formula check
If Range(cForm).HasFormula Then
cel.EntireRow.Delete
End If
End If
Next
Next
Next
I was able to use Intersect to find the rows that matched the criteria I was searching for and delete the EntireRow even though the Selection was in separate Rows.我能够使用 Intersect 找到与我正在搜索的条件匹配的行,并删除 EntireRow,即使选择位于单独的行中。
Set dataCells = Range("F2:W2").Cells
lastRow = Range("N" & Rows.Count).End(xlUp).Row 'To find last row to generate range to look through
For Each dc In dataCells 'Have to perform delete row for every column
c = Split(Cells(1, dc.Column).Address, "$")(1)
blnkRange = c & "3:" & c & lastRow
Set rngBlank = Range(blnkRange).SpecialCells(xlCellTypeBlanks).EntireRow
strFormula = "A2:A" & lastRow & ",B2:B" & lastRow & ",C2:C" & lastRow
Set rngFormula = Range(strFormula).SpecialCells(xlCellTypeFormulas)
Intersect(rngFormula, rngBlank).EntireRow.Delete (xlShiftUp) '**THIS helped in deleting Rows**
Next
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.