Solved

Macro to automatically delete row with data with specfic criteria

Posted on 2014-10-01
8
216 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
 

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now