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

时间:2016-05-13 10:16:14

标签: excel vba excel-vba

enter image description here

当想要红色边框(列AX)中的单元格时,我想创建一个宏来清除蓝色边框(~40.000行)中单元格的内容包含文本" NoBO" (= No Backorder)而不会丢失AP:AX列中的公式。

GROUP BY

出于某种原因:

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

给我一​​个错误"溢出"。搜索后,我知道这个错误意味着什么,但我无法找到解决方案。

tl; dr - 我想删除范围B6:B #####(直到最后一行)到AN6:AN #### *(直到最后一行)如果单元格AX #####含有NoBO

5 个答案:

答案 0 :(得分:2)

使用Integer进行溢出太容易了。替换:

Dim x As Integer 

使用:

Dim x As Long

答案 1 :(得分:0)

尝试:

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

我认为Union功能最多只能存储30个范围,因此可能无法满足您的需求。

答案 2 :(得分:0)

嗨,如果您正在删除行,那么最好使用For Each Loop或从列的底部开始并进行操作。

UILabel

答案 3 :(得分:0)

试试这个

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

答案 4 :(得分:0)

你可以尝试

假设AX列中没有空白单元格

public class GameScreen implements Screen{

private State state;  //this keeps track of where 


public static enum State{
    RUNNING, PAUSE, SAVEGAME
}


public MenuPause(MyGdxGame game, int era){
    this.game=game;
    state = State.RUNNING

}

@Override
public void show() {
    //do the loading and creating here
}


@Override
public void render(float delta) {

    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    switch(state){
        case RUNNING:   running();
                        break;

        case PAUSE:     pause();
                        break;

        case SAVEGAME:  savegame();
                        break;

        default:        break;
    }

}


private void running(){

}

private void pause(){

}

private void savegame(){

}

我在40k行上进行了测试,效果很好。由于没有,执行需要一段时间。行可能。