简体   繁体   English

Excel VBA:当另一个单元格包含特定文本或字符串时,如何清除指定单元格的内容

[英]Excel VBA: How to clear contents for specified cells when another cell contains specific text or string

I'm having some trouble trying to find VBA code to delete multiple specific cells if a certain cell contains a specific text.如果某个单元格包含特定文本,我在尝试查找 VBA 代码以删除多个特定单元格时遇到一些麻烦。 This spreadsheet can run close to 100k rows as well, but will vary depending on the data pull.此电子表格也可以运行接近 100k 行,但会因数据拉取而异。

The specific VBA would be able to do the following:特定的 VBA 将能够执行以下操作:

If Cell J3 equals #N/A, Blank, or 0, then clear contents of cells J3:K3 and P3:X3, and then repeat til it reaches the bottom of column J.如果单元格 J3 等于 #N/A、空白或 0,则清除单元格 J3:K3 和 P3:X3 的内容,然后重复直到到达 J 列的底部。

Thanks in advance提前致谢

How to clear contents for specified cells when another cell contains specific text or string当另一个单元格包含特定文本或字符串时如何清除指定单元格的内容

Dim cellToClear As Range
Dim cellToCheck As Range
Dim specificText As String
If cellToCheck.Value = specificText Then cellToClear.ClearContents

"I'm having some trouble trying to find VBA code " “我在查找 VBA 代码时遇到了一些麻烦”

These links contain VBA code that you can use when you no longer have trouble trying.这些链接包含 VBA 代码,您可以在尝试不再遇到问题时使用这些代码。 They contain examples you can paste into your project and modify for your needs.它们包含您可以粘贴到项目中并根据需要进行修改的示例。

This link has examples of how to read the contents of a cell .此链接包含有关如何读取单元格内容的示例

A range is a group of one or more cells in a worksheet.范围是工作表中的一组一个或多个单元格。 You can perform an operation on a range and it will affect all the cells inside the range.您可以对范围执行操作,它将影响范围内的所有单元格。 This link has examples of how to work with a range .此链接包含有关如何使用范围的示例

A loop is when the program repeats the same sequence of steps, usually until a specific condition is met.循环是程序重复相同的步骤序列时,通常直到满足特定条件。 You can find examples of different loops here .您可以在此处找到不同循环的示例

I prefer placing values into an array if you are going to be changing a bunch of cells in a routine.如果您要在例程中更改一堆单元格,我更喜欢将值放入数组中。 This generally makes the process much quicker.这通常会使该过程更快。

Start out by setting your worksheet and range objects.首先设置您的工作表和范围对象。 Please take note that the below code is currently using index 1 for the worksheet here: Set ws = ThisWorkbook.Worksheets(1) .请注意,以下代码当前在此处为工作表使用索引 1: Set ws = ThisWorkbook.Worksheets(1) If this is not the worksheet you are personally needing, then you will need to change this.如果这不是您个人需要的工作表,那么您将需要更改它。

Then place the cell contents of the entire range into an array.然后将整个范围的单元格内容放入一个数组中。 As I mentioned earlier, this process is quicker than making adjustments to individual cells 1 at a time.正如我之前提到的,此过程比一次调整单个单元格 1 更快。

Loop the array, checking for either the specific error value #N/A or the other criteria.循环数组,检查特定错误值#N/A或其他条件。 If this criteria is a match, you will enter another loop that quickly loops through the 'columns' in the row that will delete the values from only the columns you specified.如果此条件匹配,您将进入另一个循环,该循环快速遍历行中的“列”,该行将仅删除您指定的列中的值。

Once finished, rewrite the array back to the worksheet.完成后,将数组重写回工作表。

Sub main()

    Dim ws As Worksheet, rng As Range, dataArr() As Variant
    Set ws = ThisWorkbook.Worksheets(1)
    Set rng = ws.Range("J3:X" & ws.Cells(ws.Rows.Count, "J").End(xlUp).Row)

    ' Place the entire contents of worksheet range into an array
    dataArr = rng.Value

    Dim i As Long, x As Long, clearRow As Boolean
    For i = LBound(dataArr) To UBound(dataArr)

        If IsError(dataArr(i, 1)) Then
            If dataArr(i, 1) = CVErr(xlErrNA) Then clearRow = True
        ElseIf dataArr(i, 1) = vbNullString Or dataArr(i, 1) = 0 Then
            clearRow = True
        End If

        ' Loop thru the columns (x) of the current row (i)
        If clearRow Then
            For x = 1 To 15
                Select Case x
                Case 1, 2, 7 To 15
                    dataArr(i, x) = ""
                End Select
            Next x
            clearRow = False
        End If

    Next i

    ' Re-write the entire array back to the worksheet in one step
    rng.Value = dataArr

End Sub

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

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