+ Reply to Thread
Results 1 to 12 of 12

Macro to delete cells containing a formula that results in ""

Hybrid View

  1. #1
    Registered User
    Join Date
    08-17-2012
    Location
    home
    MS-Off Ver
    Excel 2003
    Posts
    26

    Question Macro to delete cells containing a formula that results in ""

    Hi all,
    I've googled left and right about the topic, and all I can find is macros to delete blank cells.

    In my case, I want to delete cells (within my selection) that contain a formula, and the formula results in "".

    This is the code I'm working on:

    Sub DeleteBlankRows1()
    'Deletes the entire row within the selection if the ENTIRE row contains no data.
    'We use Long in case they have over 32,767 rows selected.
    Dim i As Long
        'We turn off calculation and screenupdating to speed up the macro.
        With Application
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
        'We work backwards because we are deleting rows.
        For i = Selection.Rows.Count To 1 Step -1
            If WorksheetFunction.CountA(Selection.Rows(i)) = 0 Then
                Selection.Rows(i).EntireRow.Delete
            End If
        Next i
            .Calculation = xlCalculationAutomatic
            .ScreenUpdating = True
        End With
    End Sub
    Any help is appreciated!

    Cheers,
    Adri

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Macro to delete cells containing a formula that results in ""

    do you want eliminate cells or rows ?
    If solved remember to mark Thread as solved

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Macro to delete cells containing a formula that results in ""

    Hi, Adri,

    change
            If WorksheetFunction.CountA(Selection.Rows(i)) = 0 Then
    to
            If Selection.Cells(i) = 0 Then
    Ciao,
    Holger

  4. #4
    Registered User
    Join Date
    08-17-2012
    Location
    home
    MS-Off Ver
    Excel 2003
    Posts
    26

    Re: Macro to delete cells containing a formula that results in ""

    Hi Patel45,

    Ideally, I would like to check if the row has any data. If so, only the cell is deleted.
    Otherwise, if the row contains no data, it can be completely deleted.

  5. #5
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Macro to delete cells containing a formula that results in ""

    Sub eliminateBlankCells()
    For Each Cell In Selection
        If Cell.Value = "" Then
            Cell.Delete Shift:=xlUp
        End If
    Next
    
    End Sub

  6. #6
    Registered User
    Join Date
    08-17-2012
    Location
    home
    MS-Off Ver
    Excel 2003
    Posts
    26

    Re: Macro to delete cells containing a formula that results in ""

    Hi patel45,
    This won't delete the row if blank, is that right?

  7. #7
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Macro to delete cells containing a formula that results in ""

    Hi, Adri,

    could you please be kind enough to tell me the formula that results in ""? I expect a different way to be pratical as well.

    BTW: The changed line of code should read

            If Selection.Cells(i) = "" Then
    And patel45īs code will only delete the cell, for the delete of the row you would need to change to
            Cell.EntireRow.Delete Shift:=xlUp
    Cia,
    Holger
    Last edited by HaHoBe; 08-23-2012 at 01:37 AM. Reason: typos

  8. #8
    Registered User
    Join Date
    08-17-2012
    Location
    home
    MS-Off Ver
    Excel 2003
    Posts
    26

    Re: Macro to delete cells containing a formula that results in ""

    Hi patel45,
    Your code
    Sub eliminateBlankCells()
    For Each Cell In Selection
        If Cell.Value = "" Then
            Cell.Delete Shift:=xlUp
        End If
    Next
    
    End Sub
    does work, but I need to run the macro 2 or 3 times, any ideas why? It doesn't delete all the cells when it first ran.

  9. #9
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Macro to delete cells containing a formula that results in ""

    Hi, Adri,

    this happens due to the fact that patelīs code starts at the top and after a deletion continues in the next row not condering that a ""-Cell may have been shifted up.

    The code you found wonīt show that because it starts at the bottom and works up. So in total:
    Sub DeleteBlankRows1()
    'Deletes the entire row within the selection if the ENTIRE row contains no data.
    'We use Long in case they have over 32,767 rows selected.
    Dim i As Long
        'We turn off calculation and screenupdating to speed up the macro.
        With Application
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
        'We work backwards because we are deleting rows.
        For i = Selection.Rows.Count To 1 Step -1
            If Selection.Cells(i) = "" Then
                Selection.Rows(i).EntireRow.Delete
            End If
        Next i
            .Calculation = xlCalculationAutomatic
            .ScreenUpdating = True
        End With
    End Sub
    And what do you really want? Cell deleted or EntireRow?

    Ciao,
    Holger

  10. #10
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Macro to delete cells containing a formula that results in ""

    Quote Originally Posted by adrianodl View Post
    does work, but I need to run the macro 2 or 3 times, any ideas why? It doesn't delete all the cells when it first ran.
    I don't find this problem on my file, can you attach a sample of your ?

  11. #11
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Macro to delete cells containing a formula that results in ""

    Hi, patel45,

    you read my post? Apparently not. Just double up the number of "" in your file/selection and - be surpised to find out.

    Ciao,
    Holger

  12. #12
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Macro to delete cells containing a formula that results in ""

    Yes, I did not see with attention, maybe this is solution
    Sub delete()
    Dim rng As Range
    Set rng = Selection
    lastrow = rng.Rows.Count
    lastcol = rng.Columns.Count
    For j = lastrow To 1 Step -1
      For i = lastcol To 1 Step -1
         If rng(j, i).Value = "" Then
            rng(j, i).Delete Shift:=xlUp
         End If
      Next
    Next
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1