簡體   English   中英

Excel VBA 正在從方法中查找每個其他單元格而不是每個單元格

[英]Excel VBA is Finding Every Other Cell not Every Cell From Method

Excel VBA正在使用檢查空單元格的方法查找所有其他單元格。 下次運行相同的宏時,它會找到上次運行時跳過的單元格,同時再次跳過空單元格的下一個實例。 如果我在宏中循環幾次,最終根據宏的目的,沒有數據的每一行都會被刪除。 一次刪除一行后,行確實向上移動,我將嘗試聯合並刪除@BigBen 所述的范圍

當找到空單元格時,它會檢查 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

要刪除的 Excel 范圍

我能夠使用 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM