简体   繁体   English

如果单元格包含特定文本,则清除范围的内容

[英]Clear content of a range if cell contains a specific text

在此处输入图片说明

I want to make a macro that clears the content of the cells in the blue border (~40.000 Rows) when the cells in the red border (column AX) contain the text "NoBO" (=No Backorder) without losing the formulas in the columns AP:AX. 我想制作一个宏,以清除红色边框 (第AX列)中的单元格包含文本“ NoBO”(=“无延期交货”)时清除蓝色边框 (〜40.000行)中单元格的内容,而不会丢失列AP:AX。

Sub clear_ranges()
Dim ws As Worksheet
Dim x As Integer
Dim clearRng As Range

Application.ScreenUpdating = False

Set ws = ThisWorkbook.Sheets("Input")

For x = 6 To ws.Range("B" & Rows.Count).End(xlUp).Row
    If (ws.Range("AX6" & x).Value = "NoBO") Then
        If clearRng Is Nothing Then
            Set clearRng = ws.Range("B6" & x & ":" & "AN6" & x)
        Else
            Set clearRng = Application.Union(clearRng, ws.Range("B6" & x & ":" & "AN6" & x))
        End If
    End If
Next x
clearRng.Clear
End Sub

And for some reason: 由于某些原因:

For x = 6 To ws.Range("B" & Rows.Count).End(xlUp).Row

gives me a error "Overflow". 给我一个错误“溢出”。 After searching I know what this error means but I can't find a solution for this. 搜索后,我知道此错误是什么意思,但我无法找到解决方案。

tl;dr - I want to delete the range B6:B##### (till last row) to AN6:AN####*(till last row) if cell AX##### containts NoBO tl; dr-如果单元格AX #####包含NoBO,我想删除范围B6:B #####(至最后一行)到AN6:AN #### *(至最后一行)

It is too easy to get an overflow using Integer . 使用Integer太容易溢出。 Replace: 更换:

Dim x As Integer 

with: 与:

Dim x As Long

Try: 尝试:

Sub clear_ranges() 

Dim ws As Worksheet 
Dim x As Integer 
Dim clearRng As Range

Application.ScreenUpdating = False

Set ws = ThisWorkbook.Sheets("Input")

For x = 6 To ws.Range("B" & Rows.Count).End(xlUp).Row
    If ws.Range("AX" & x).Value = "NoBO" Then
        ws.Range("B" & x & ":" & "AN" & x).Clear
    End If
Next x

Application.ScreenUpdating = True

End Sub

I think the Union function can only store up to 30 ranges so it might not suit your needs. 我认为“ Union功能最多只能存储30个范围,因此可能不适合您的需求。

Hi if you are Deleting Rows it's the Best to use a For Each Loop or start from the bottom of the column and work up. 您好,如果您要删除行,最好使用For Each循环,或者从列的底部开始进行处理。

'Loop through cells A6:Axxx and delete cells that contain an "x."
   For Each c In Range("AX6:A" & ws.Range("B" & Rows.Count).End(xlUp).Row)
       If c.Value2 = "NoBo" Then

             Set clearRng = ws.Range("B" & c.Row & ":" & "AN" & c.Row)

       End If
       clearRng.Clear
   Next    

try this 尝试这个

Option Explicit

Sub clear_ranges()
Dim ws As Worksheet

Application.ScreenUpdating = False
Set ws = ThisWorkbook.Sheets("Input")
With ws
    With .Range("B5:AX" & .Cells(.Rows.Count, "B").End(xlUp).Row) 'take all data
        .AutoFilter Field:=49, Criteria1:="NoBO" 'filter to keep only rows with "NoBO" in column "AX" (which is the 49th column from column "B"
        With .Offset(1).Resize(.Rows.Count - 1) 'offset from headers
            If Application.WorksheetFunction.Subtotal(103, .Columns(1)) > 0 Then Intersect(.SpecialCells(xlCellTypeVisible), ws.Columns("B:AN")).ClearContents 'clear cells in columns "B:AN" of filtered rows
        End With
        .AutoFilter 'remove autofilter
    End With
End With

Application.ScreenUpdating = True
End Sub

You can try 你可以试试

Assuming that there are no blank cells in AX Column 假设“ AX列”中没有空白单元格

Sub clr_cell()

For i = 6 To ActiveSheet.Range("AX6", ActiveSheet.Range("AX6").End(xlDown)).Rows.Count
'counts the no. of rows in AX and loops through all

If ActiveSheet.Cells(i, 50).Value = "NoBo" Then

ActiveSheet.Range(Cells(i, 2), Cells(i, 40)).ClearContents
'clears range B to AN

End If

Next i

End Sub

I tested this on 40k rows and it worked fine. 我在40k行上对此进行了测试,效果很好。 It takes a while to execute due to the no. 由于没有,执行需要一段时间。 of rows maybe. 行。

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

相关问题 如果活动单元格包含特定文本,则清除其内容 - Clear contents of active cell if it contains specific text VBA如果工作表1上的单元格(或范围)包含任何值,则清除工作表2上的单元格范围内容 - VBA If cell (or range) on sheet1 contains any value then clear content of cell range on sheet 2 Excel VBA:如果单元格包含某些文本,则输入具有该内容的单元格范围 - Excel VBA: If cell contains certain text then input range of cells with that content Excel VBA选择一个单元格区域,直到一个单元格包含特定文本 - Excel VBA Select a Range of Cells Until a Cell Contains Specific Text 查找范围以查找名称,如果单元格包含名称,则返回特定文本 - Lookup range to find name, return specific text if cell contains the name 如果单元格包含文本 VBA Excel,则清除单元格 - Clear Cell if Cell Contains Text VBA Excel 如果 Excel 2010 中单元格范围内的单元格包含特定文本,请将整个单元格文本移动到不同的列中 - If a cell in range of cells in Excel 2010 contains specific text, move whole cell text into a different column VBA 清除单元格范围内的内容,工作表 1 除外 - VBA clear content in cell range, except for sheet 1 如果值为负,则清除范围内的单元格内容 - Clear cell content in a range if value is negative Excel-如果单元格包含特定文本 - Excel - If cell contains specific text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM