+ Reply to Thread
Results 1 to 6 of 6

Delete row if all cells in a certain range are 0 or blank

Hybrid View

excelgeek1234 Delete row if all cells in a... 11-02-2017, 10:45 AM
davesexcel Re: Delete row if all cells... 11-02-2017, 10:57 AM
johnnymac Re: Delete row if all cells... 11-02-2017, 11:54 AM
davesexcel Re: Delete row if all cells... 11-02-2017, 12:02 PM
excelgeek1234 Re: Delete row if all cells... 11-02-2017, 12:15 PM
johnnymac Re: Delete row if all cells... 11-02-2017, 12:28 PM
  1. #1
    Registered User
    Join Date
    06-20-2017
    Location
    New York, NY
    MS-Off Ver
    2016
    Posts
    5

    Delete row if all cells in a certain range are 0 or blank

    Hello,

    I am trying to write a code which looks at rows 13-33 and deletes an entire row if ALL the cells in Columns B-M are 0 or blank and Column A is not blank.

    Right now my code deletes the entire row if Column B is 0; doesn't look at the other columns.

    I know I need to insert a loop somewhere.

    Any help would be greatly appreciated.

    Following is the code:



    Sub scheduleA()

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    Sheets("Schedule A Template").Select

    Dim RowstoDelete As Long
    x = 33
    For RowstoDelete = Cells(x, 2).End(xlUp).Row To 13 Step -1

    If (Cells(RowstoDelete, 2).Value = "0") And (Cells(RowstoDelete, 1).Value <> "") Then
    Rows(RowstoDelete).Delete Shift:=xlUp
    End If


    Next RowstoDelete
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

    End Sub

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,529

    Re: Delete row if all cells in a certain range are 0 or blank

    You have it looking at column A
    ` (Cells(RowstoDelete, 1).Value <> "") Then`
    change the 1 to the "M" column

  3. #3
    Forum Contributor
    Join Date
    08-16-2012
    Location
    Pendleton, OR
    MS-Off Ver
    Excel 2013
    Posts
    129

    Re: Delete row if all cells in a certain range are 0 or blank

    Excelgeek did want to check column A. It is the first part of the And statement that only looks at column B, when the B:M range is desired.

    Depending on the type of values that might be found in this range there are several methods than would work. The simplest in my mind would be to SUM the range, but this would fail if you had values of -1 and 1, for example, in that range. This would not require a loop, just a sum where you have (Cells(RowstoDelete, 2).Value = "0"). Let us know what values might be in those columns.

  4. #4
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,529

    Re: Delete row if all cells in a certain range are 0 or blank

    Ah yes, you are correct @jonnymac

  5. #5
    Registered User
    Join Date
    06-20-2017
    Location
    New York, NY
    MS-Off Ver
    2016
    Posts
    5

    Re: Delete row if all cells in a certain range are 0 or blank

    The values in these columns are monetary values..$0 and above. I only want to delete the entire row if ALL columns (B-M) have $0. I want to keep the row if it is $0 for the first few columns but say in column F we see a number ($250).

  6. #6
    Forum Contributor
    Join Date
    08-16-2012
    Location
    Pendleton, OR
    MS-Off Ver
    Excel 2013
    Posts
    129

    Re: Delete row if all cells in a certain range are 0 or blank

    OK then, since all values are positive a SUM function would work. The Sum of columns B:M would return 0 only if all were blank or 0. I am fairly new to VBA (it is my 12th programming language, so I tend to mess up syntax and VBA has many ways to do the same task) but I think you can replace the Cells(RowstoDelete, 2) with WorksheetFunction.Sum (Cells(RowstoDelete,2), Cells (RowstoDelete, 13))

    Or maybe you don't need the "WorksheetFunction." Sum () may be a native function you can use/

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 2
    Last Post: 02-25-2015, 09:44 AM
  2. vba delete blank cells in range B:B,J:J,O:O so as not affect next column values cells.
    By breadwinner in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-14-2014, 10:44 PM
  3. [SOLVED] Delete special blank cells not recognising blank cells
    By anrichards22 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 11-04-2013, 03:54 AM
  4. [SOLVED] Select variable range; identify & delete rows with blank cells
    By alter54 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-28-2013, 04:40 PM
  5. Delete adjacent cells when range is blank
    By Buchli in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-29-2013, 05:29 PM
  6. [SOLVED] How to Delete Entire Row if Range of Cells are Blank?
    By mrodrigues in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-10-2012, 09:42 PM
  7. Delete Blank Rows-Blank Row between my data range
    By Bob@Sun in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-27-2010, 02:52 PM

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