简体   繁体   English

如果特定列中的单元格有数据,如何告诉 Excel 宏删除整行?

[英]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.我知道有些人使用代码,但我不熟悉 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.我知道您可以将代码复制并粘贴到 vba 中,但我不知道如何实际操作。 I know where to open vba but once open, I wouldn't know where to paste a code.我知道在哪里打开 vba,但是一旦打开,我就不知道在哪里粘贴代码。 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'.如果您的最后一个单元格不为空并且您的相关列是“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.在“开发人员”选项卡中,依次转到“Visual Basic”、“插入”、“新模块”并粘贴上面的代码。 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 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.这是一个宏,它查看活动工作表 A 列中从第 1 行开始的每个单元格。如果单元格具有数值,则整行将被删除。 This continues for all rows containing a value in column A. Adjust the column and first row as required.对于包含 A 列中的值的所有行,这将继续。根据需要调整列和第一行。

Press Alt+F11 to open the Visual Basic Editor (VBE), then press Ctrl+R to view Project Explorer.按 Alt+F11 打开 Visual Basic 编辑器 (VBE),然后按 Ctrl+R 查看项目资源管理器。 Right-click VBAProject and pick Insert > Module.右键单击 VBAProject 并选择插入 > 模块。 If necessary, press F7 to view the Code pane, then copy and paste the following VBA code into the new module.如有必要,按 F7 查看代码窗格,然后将以下 VBA 代码复制并粘贴到新模块中。 Press Alt+F11 to restore the active sheet, then press F8 to run the macro.按 Alt+F11 恢复活动工作表,然后按 F8 运行宏。 Save the workbook as *.xlsm or *.xlsb.将工作簿另存为 *.xlsm 或 *.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.您可能还对wellsr VBA Q&A的类似问题和答案感兴趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel 宏/VBA - 如果一列中的单元格为空,如何删除整行? - Excel Macro / VBA - How do I delete an entire row if a cell in one column is blank? 如何在Excel中告诉单元格包含整个行的总和 - How do i tell a cell to contain the sum of a entire row in excel 如果单元格值等于“”,如何编写 Excel 宏以删除整行 - How to Write Excel Macro to Delete Entire Row if cell Value Equals “” 如果特定列名称下的单元格为空,如何删除整行的空白? - How can I delete empty the entire row if the cell under a specific column name is empty? 如果行中的一个单元格包含错误,如何删除整行? - How do I delete an entire row if a cell in the row contains an error? 如果 A 列中的单元格等于 B 列中的单元格,则 Excel 宏将删除一行 - Excel Macro to delete a row if cell in Column A equals cell in Column B Excel Vba宏删除基于列标题的整个行 - Excel Vba Macro to Delete Entire Row Based on Column Header 如何遍历特定列中的单元格并根据其内容删除整行? - How do I loop through cells in a specific column and delete the entire row based on its contents? 如何在Excel表格中搜索文本,然后删除整个行? - How do I search a excel table for a text and then delete that entire row? 如何在整个列中用括号括住Excel单元格内容? - How do I enclose an Excel cell contents with parentheses for an entire column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM