简体   繁体   中英

How do I tell an excel macro to delete the entire row if cell in a specific column have data?

So i have a spreadsheet. One of the columns has cells that have numbers, text and some are blank. how do i tell the macro to delete the whole row where the cells in the column has numbers? I want to keep the rows that have text or are blank.

i know some people use code, but Im not familiar with visual basic. I know how to start recording a macro. I know how to assign a shortcut key. I know that when a macro messes up, it usually is because i forgot to rename the tab. I know you can copy and paste a code into vba but I dont know how to actually do that. I know where to open vba but once open, I wouldn't know where to paste a code. I would prefer to figure out how to do this manually. Thanks

Sub Macro1()

Dim N As Integer
Dim i As Integer
Dim M As Integer
Dim contents

N = Cells(Rows.Count, "A").End(xlUp).Row
M = 0

For i = 1 To N 
  contents = Cells(i, "A").Value
  type_in_cell = VarType(contents)
   
  If type_in_cell = 8 Or type_in_cell = 0 Then
    
   Else
     Rows(i).EntireRow.Delete
       i = i - 1
  End If
     
  M = M + 1
        
  If M = N Then Exit Sub    
   Next i
End Sub

This should work provided your last cell is not empty and your column in question is column 'A'. It checks to see if the cell is text or blank, and deletes the row otherwise.

This gets put into a new module. In the 'Developer' tab , go to 'Visual Basic', then 'Insert', 'New Module' and paste the code above. Then click the green play button to test it out. Do note though that you cannot undo the actions of a macro, so I highly recommend saving a copy of the file first before editing it.

If you don't have the developer tab available please see the documentation below:

https://support.microsoft.com/en-us/office/show-the-developer-tab-e1192344-5e56-4d45-931b-e5fd9bea2d45

Someone answered while I was working on my answer, but I will post mine anyway.

Here is a macro that looks at each cell in column A of the active sheet starting at row 1. If the cell has a numeric value, the entire row is deleted. This continues for all rows containing a value in column A. Adjust the column and first row as required.

Press Alt+F11 to open the Visual Basic Editor (VBE), then press Ctrl+R to view Project Explorer. Right-click VBAProject and pick Insert > Module. If necessary, press F7 to view the Code pane, then copy and paste the following VBA code into the new module. Press Alt+F11 to restore the active sheet, then press F8 to run the macro. Save the workbook as *.xlsm or *.xlsb.

Sub DeleteNumericRows()
' delete rows that have numeric values in keyCol
    Const keyCol = 1 ' adjust this: A is 1, B is 2, etc.
    Const firstRow = 1 ' adjust this
    msg = "Do you want to delete each row containing a " _
        & "numeric value in column " & Chr(64 + keyCol) _
        & "? (This cannot be Undone by using Ctrl+Z.)"
    ans = MsgBox(msg, (vbQuestion + vbYesNo))
    If ans <> vbYes Then Exit Sub
    lastRow = Cells(Rows.Count, keyCol).End(xlUp).Row
    nRow = firstRow
    Do
        If IsNumeric(Cells(nRow, keyCol).Value) Then
            Rows(nRow).Delete
            lastRow = lastRow - 1
        Else
            nRow = nRow + 1
        End If
    Loop Until nRow > lastRow
End Sub

You might also be interested in wellsr VBA Q&A for similar questions and answers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM