Solved

Macro to automatically delete row with data with specfic criteria

Posted on 2014-10-01
8
222 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

738 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