Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Macro to automatically delete row with data with specfic criteria

Posted on 2014-10-01
8
Medium Priority
?
227 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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
 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

This article describes a serious pitfall that can happen when deleting shapes using VBA.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
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…
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…

963 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