简体   繁体   中英

If cell value is 0 then delete column, Excel VBA - deletes all columns

I'm trying to delete columns if a certain cell in that column is "0" - I'm looping through all worksheets. I've tried different variations and it all ends up with different results - either nothing is deleted or everything is deleted.

Whole code:

Sub Finalize()
Dim finalform As Worksheet
Dim deletename As String
Dim finalworkbook As Workbook
Dim ws As Worksheet
Dim copyrange As Range
Dim columnloop As Range
Dim finalarray As Variant

Application.DisplayAlerts = False
Application.ScreenUpdating = False

Set finalform = Workbooks(ActiveWorkbook.Name).ActiveSheet

finalarray = Array(finalform.Range("D3").Value, finalform.Range("D4").Value, finalform.Range("D5").Value _
, finalform.Range("D6").Value, finalform.Range("D7").Value, finalform.Range("D8").Value, finalform.Range("D9").Value _
, finalform.Range("D10").Value, finalform.Range("D11").Value, finalform.Range("D12").Value, finalform.Range("D13").Value _
, finalform.Range("D14").Value, finalform.Range("D15").Value)

On Error Resume Next
For a = 3 To 20

If Range("B" & a).Value <> "" Then
    Workbooks.Open finalform.Range("B" & a).Value
    Set finalworkbook = Workbooks(ActiveWorkbook.Name)

        'Find, replace, remove
        For Each ws In finalworkbook.Worksheets

            'Copy paste values
            If Not IsInArray(ws.Name, finalarray) Then
                Set copyrange = ws.Cells
                copyrange.Copy
                copyrange.PasteSpecial xlPasteValues
            End If
            Application.CutCopyMode = False

        Next ws

        For Each ws In finalworkbook.Worksheets
            'Delete sheets
            For b = 3 To 15
                deletename = finalform.Range("D" & b).Value
                If deletename <> "" Then
                    finalworkbook.Worksheets(deletename).Delete
                End If
            Next b

            With ws
                myChar = Chr(10)
                'Enter space
                .UsedRange.Replace What:="~~", Replacement:=myChar, LookAt:=xlPart, _
                SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                ReplaceFormat:=False

                For c = .UsedRange.Columns.Count To 1 Step -1
                    cName = Split(Cells(, c).Address, "$")(1)
                    nom = .Range(cName & "8").Value
                    If nom = 0 Then
                        .Columns(c).EntireColumn.Delete
                    End If
                Next c
            End With
        Next ws

        finalworkbook.Save
        finalworkbook.Close
End If
Next a

Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub

The issue here is how VBA interprets the =0 operation. Here is a quick list. You most likely want to use IsEmpty() as ZygD noted.

IsEmpty(cell.Value) 'Returns true if the cell is empty, or false if it is filled with 0.

cell.Value = 0 'Returns true if the cell is empty and if it is filled with 0, false otherwise.

IsNumeric(cell.Value) 'Returns true if the cell is empty, or if it is filled with any number, false otherwise.

如果要遍历所有工作表,甚至可以使用vbNullString而不是0或“”,因为这样可以更快地运行。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM