+ Reply to Thread
Results 1 to 6 of 6

How to delete entire row if first cell of it is empty for specific range and worksheets

Hybrid View

  1. #1
    Registered User
    Join Date
    05-06-2013
    Location
    bangalore, india
    MS-Off Ver
    Excel 2010
    Posts
    28

    How to delete entire row if first cell of it is empty for specific range and worksheets

    Hi I have a workbook as attached.
    The work sheets are named as first page,last page, static sheet, dynamic sheet and transfer sheet. the dynamic sheets add up every day for this workbook.
    My macro should Delete the entire row if its cell in column "A" is empty.
    Restrictions
    1. Macro should not work for first and last work sheets
    2. The range for static sheets(should be from "A5" row to the last used row) is different from dynamic sheets(should be from "A7" row to the last used row)
    3. In transfer sheet, the macro should run for rows below the black stripped row and also for the columns after the black stripped column if any cell from D8 is empty delete the entire column.
    I have written the macro as below but when I run the macro, it is only working for static pages and when i select any dynamic page and re-run the macro, it is performing for all the dynamic pages. Also I am not able to select the range for transfer sheet and perform the macro.

    Please help!!!

    Sub DL()
    
     Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
             With ws
             ' Discarding first, last and transfer sheet
             If .Name <> "First Sheet" And .Name <> "Last Sheet" And .Name <> "Transfer sheet" Then
             ' To delete all blank rows in static sheets
             If .Name = "Static Sheet1" Or .Name = "Static Sheet2" Or _
            .Name = "Static Sheet3" Or .Name = "Static Sheet4" Then
             .Range("A5:A" & Range("A" & Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
             Else
             ' To delete all blank rows in dynamic sheets
             .Range("A7:A" & Range("A" & Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
             End If
             'To delete all blank rows in transfer sheet from cells below black row and beside black column
             'Else
             'If .Name = "Transfer sheet" Then
             End If
             On Error Resume Next
             End With
           
        Next ws
    End Sub
    Attached Files Attached Files
    Last edited by roshanvmech; 02-28-2014 at 11:36 AM.

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,026

    Re: How to delete entire row if first cell of it is empty for specific range and worksheet

    Just to clarify ... In the "Transfer sheet", if any cell in column A starting at row 25 is empty, you want to delete the entire row. Also if any cell in the range D8:AD8 is empty, you want to delete the entire column. Is this correct?
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Registered User
    Join Date
    05-06-2013
    Location
    bangalore, india
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: How to delete entire row if first cell of it is empty for specific range and worksheet

    Hi Mumps,
    Yes it is correct. But it is not always row 25 or till column AD8, the rows and columns increase or decrease day by day i.e., they are dynamic. The only mark is that those start after the cell which has the interior color black.

  4. #4
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,026

    Re: How to delete entire row if first cell of it is empty for specific range and worksheet

    Hi roshanvmech. I think that Leith has you covered.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: How to delete entire row if first cell of it is empty for specific range and worksheet

    Hello roshanvmech,

    The macros below work for the static and dynamic sheets and leaves the First and Last sheets alone. It wasn't clear to me from the post or the workbook what to do with the Transfer sheet. If you provide a good example of which cells are to checked, I can then write the code for the macro. Here are the macros' code. These have been added to the attached workbook.

    Macros are in the ThisWorkbook Module of the VBA Project
    Sub Macro1()
    
        Dim Wks     As Worksheet
        Dim WksName As String
        
            For Each Wks In Worksheets
            
                WksName = LCase(Wks.Name)
                
                Select Case WksName
                    Case Is = "first sheet", "last sheet"
                        ' Ignore these sheets.
                    Case Is = "transfer"
                        ' ???
                    Case Else
                        If WksName Like "static*" Then
                            Call DeleteRows(Wks.Range("A5"))
                        End If
                        
                        If WksName Like "dynamic*" Then
                            Call DeleteRows(Wks.Range("A7"))
                       End If
                End Select
            Next Wks
            
    End Sub
    
    Sub DeleteRows(ByRef Rng As Range)
    
        Dim RngEnd  As Range
        Dim row     As Long
        Dim Wks     As Worksheet
        
            Set Wks = Rng.Parent
            
            Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)
            If RngEnd.row < Rng.row Then Exit Sub
            
            Set Rng = Wks.Range(Rng, RngEnd)
            
            Application.ScreenUpdating = False
                For row = Rng.Rows.Count To 1 Step -1
                    If IsEmpty(Rng.Item(row, 1)) Then
                        Rng.Cells(row, 1).EntireRow.Delete
                    End If
                Next row
            Application.ScreenUpdating = True
            
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  6. #6
    Registered User
    Join Date
    05-06-2013
    Location
    bangalore, india
    MS-Off Ver
    Excel 2010
    Posts
    28

    Re: How to delete entire row if first cell of it is empty for specific range and worksheet

    Hi Leith,
    Your code is fantastic.
    For transfer sheet, I want the rows to be deleted from the range starting from the row below the black strip. i.e., in this sheet the black strip is at row 24 and the range from which this macro should run is from row 25 to the last used row. The catch here is the black strip varies from workbook to workbook.
    Similarly, for columns, the no. of columns differ from one workbook to another. I want the macro to delete the columns for which any cell between D8 to the last used column is empty, the entire column should be deleted.

+ 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. [SOLVED] Program a conditional script that will delete entire row if cell in column B is empty
    By Rabbitstew in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-17-2012, 01:54 PM
  2. [SOLVED] Macro to delete entire row (within a specified range) if cell in column A is empty
    By instinctellekt in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-07-2012, 10:01 AM
  3. Macro (Delete Entire row Cell in column R is empty)
    By andrewvt in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-20-2011, 07:06 AM
  4. Delete entire column if all cells in a certain range are empty
    By Andula in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-26-2011, 06:33 AM
  5. find <> empty cell in range, delete entire row
    By Shell_ in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-26-2011, 12:36 PM

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