• Zero vs. blank cell check in VBA for Excel

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Zero vs. blank cell check in VBA for Excel

    Author
    Topic
    #483857

    I have an excel spreadsheet that can have a number of rows with a zero in column 10 (“quantity”) and some rows with text subheaders where the quantity cell is BLANK.

    I’m trying to develop a macro to delete the rows where the quantity cell is zero but it also deletes the subheader row. It seems to me that the simple test I’m doing does not discriminate between zero and blank.

    If Worksheets(1).Cells(lnRowCount, 10) = 0 Then
    rnSelection.Rows(lnRowCount).Delete

    Is there a better check method to do this. I guess a couple of nested IFs might work:
    If Isblank then skip else if zero then delete.

    Is there a more elegant way?

    Viewing 0 reply threads
    Author
    Replies
    • #1337006

      Damnlimey,

      Try this code:

      Code:
      Option Explicit
      
      Sub ZeroBlank()
      
         Dim lCntr As Long
         
         For lCntr = 1 To 4
            If Cells(lCntr, 1) = 0 Then
              If Cells(lCntr, 1) = "" Then
                Debug.Print "Cell Row A" & Format(lCntr, "#") & " is BLANK"
              Else
                Debug.Print "Cell Row A" & Format(lCntr, "#") & " = ZERO"
              End If
         End If
         Next lCntr
         
      End Sub
      

      31226-ZeroOrBlank:cheers:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

    Viewing 0 reply threads
    Reply To: Zero vs. blank cell check in VBA for Excel

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: