Solved

Macro to automatically delete row with data with specfic criteria

Posted on 2014-10-01
8
221 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

685 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