Solved

Macro to automatically delete row with data with specfic criteria

Posted on 2014-10-01
8
219 Views
Last Modified: 2014-10-06
To create a macro to automatically delete data in a specific row where the row is a variable.

It should do the following:
1. Dialog box to pop up " Enter row number"
OK | Cancel

2. Example if 50 is selected, then whatever data found in Row 50 (from Column C to Column GE) should be deleted EXCEPT for
- column AB in file "V.xls"
- column CF and column CG in file "W.xls"

3. Once done, pop up dialog "Completed - Data in cell xx, etc - deleted."
OK

Example if cell E50, G50, O50 has data, then they should be deleted.
"Completed - Data in E50, G50, O50 - deleted"

if no data in that specific row, then "Completed - no data found"
0
Comment
Question by:ceneiqe
  • 4
  • 4
8 Comments
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
ID: 40355728
This code will removed your selected cells on a specified row:
Option Explicit
Sub Remove_Row_Data()
    Dim lngRow As Long
    Dim strWkbkName, strDelList As String
    Dim c As Integer
    
    strWkbkName = ActiveWorkbook.Name
    On Error Resume Next
    lngRow = InputBox("Enter row number", "Remove Row Data")
    If IsNumeric(lngRow) = True And (lngRow > 0) Then
        If strWkbkName = "V.xls" Then
            For c = 3 To 187
                If c <> 28 Then
                    If Cells(lngRow, c).Value <> "" Then
                        Cells(lngRow, c).ClearContents
                        strDelList = strDelList & ", " & Cells(lngRow, c).Address
                    End If
                End If
            Next c
        ElseIf strWkbkName = "W.xls" Then
            For c = 3 To 187
                If c <> 84 And c <> 85 Then
                    If Cells(lngRow, c).Value <> "" Then
                        Cells(lngRow, c).ClearContents
                        strDelList = strDelList & ", " & Cells(lngRow, c).Address
                    End If
                End If
            Next c
        End If
        MsgBox "Completed - Data in " & strDelList & " - deleted."
    End If
End Sub

Open in new window


You would either have to have a workbook with this code open or you would want to insert this code in your Personal Macro Workbook (Personal.xlsb) so that you can run it at any time.  It will only work if the V.xls or W.xls file is visible.

I've attached a workbook with this code for you to test.  Note that any content deleted cannot be recovered with the Undo function, so save a copy of your file before proceeding.

Regards,
-Glenn
EE-Q28529317.xlsm
0
 

Author Comment

by:ceneiqe
ID: 40356251
for Personal.xlsb,

You are refering to the file in ".......Microsoft\Excel\XLSTART\PERSONAL.XLSB" right ?
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40356274
^ Yes, exactly.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:ceneiqe
ID: 40363014
Thanks.

There are 4 separate excel files for this macro operation.
1. P.xls = To delete all data that is in the stated row number
2. T.xls = to delete all data that is in the stated row number
3. V.xls = as mentioned above
4. W.xls = as mentioned above

The macro works but it doesn't work for other files, ie the P.xls and T.xls files.
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40363048
The behavior of the example subroutine is only for the "V" and "W" files and each has different rules as you specified in the original question.  You are now asking for additional functionality beyond the scope of your original question.

Please properly close this question by accepting the answer I provided or explain what issues you have with it.  Then open a new question regarding processing of the "P" and "T" files.  I will be glad to offer assistance.

Regards,
-Glenn
0
 

Author Closing Comment

by:ceneiqe
ID: 40363133
This only apply to 2 files and does not apply to any generic file.
0
 

Author Comment

by:ceneiqe
ID: 40363139
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40363160
Thanks.  I'll get to your new question right away.

-Glenn
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question