搜索特定的字符串并在垂直之间删除所有单元格

发布于 2025-01-19 05:07:56 字数 852 浏览 7 评论 0原文

我想创建一个 VBA 函数来搜索术语 red 并删除 Red 之间的所有剩余单元格空单元格。正如您在照片栏中看到的,c 代表期望的结果。我下面的代码现在以垂直方式删除单元格之间的所有空白空间。我只需要将红色部分的搜索添加到此代码中即可。

输入图片此处描述

Sub collapse_columns()
    Dim x As Integer
    For x = 1 To 4
        collapse_column x
    Next
End Sub


Sub collapse_column(column_number As Integer)

    Dim row As Long
    Dim s As Worksheet
    Dim last_row As Long
    Set s = ActiveSheet ' work on the active sheet
    'Set s = Worksheets("Sheet1") 'work on a specific sheet
    
    last_row = ActiveSheet.Cells(s.Rows.Count, column_number).End(xlUp).row
    
    For row = last_row To 1 Step -1
      If Cells(row, column_number).Value = "" Then Cells(row, column_number).Delete xlUp
    Next

End Sub

I want to create a VBA function that searches for the term red and deletes all remaining cells empty cells between Red. As you can see in the photo column c represents the desired outcome. My code below right now deletes all empty spaces between the cells in a vertical way. I just need to add the search for red part to this code.

enter image description here

Sub collapse_columns()
    Dim x As Integer
    For x = 1 To 4
        collapse_column x
    Next
End Sub


Sub collapse_column(column_number As Integer)

    Dim row As Long
    Dim s As Worksheet
    Dim last_row As Long
    Set s = ActiveSheet ' work on the active sheet
    'Set s = Worksheets("Sheet1") 'work on a specific sheet
    
    last_row = ActiveSheet.Cells(s.Rows.Count, column_number).End(xlUp).row
    
    For row = last_row To 1 Step -1
      If Cells(row, column_number).Value = "" Then Cells(row, column_number).Delete xlUp
    Next

End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

渡你暖光 2025-01-26 05:07:56

使用自动过滤器,您可以避免循环和逐行删除。

    Application.DisplayAlerts = False
    With ActiveSheet
        .Rows(1).EntireRow.Insert 'If you have headers you don't need
        .Cells(1, 1).Value = "Temp" 'If you have headers you don't need
        .Cells(1, 1).AutoFilter 1, "<>red"
        
        'If you have headers start on row 2
        .Range(.Cells(1, 1), .Cells(Rows.Count, 1).End(xlUp)).SpecialCells(xlCellTypeVisible).Delete
        If .FilterMode Then
            .ShowAllData
        End If
    End With
    Application.DisplayAlerts = True

如果您只想修改现有代码,请将此行:更改

If Cells(row, column_number).Value = "" Then Cells(row, column_number).Delete xlUp

为:

If Not Cells(row, column_number).Value Like "red" Then Cells(row, column_number).Delete xlUp

Using autofilter you can avoid looping and deleting rows one by one.

    Application.DisplayAlerts = False
    With ActiveSheet
        .Rows(1).EntireRow.Insert 'If you have headers you don't need
        .Cells(1, 1).Value = "Temp" 'If you have headers you don't need
        .Cells(1, 1).AutoFilter 1, "<>red"
        
        'If you have headers start on row 2
        .Range(.Cells(1, 1), .Cells(Rows.Count, 1).End(xlUp)).SpecialCells(xlCellTypeVisible).Delete
        If .FilterMode Then
            .ShowAllData
        End If
    End With
    Application.DisplayAlerts = True

If you want to just modify your existing code change this line:

If Cells(row, column_number).Value = "" Then Cells(row, column_number).Delete xlUp

to:

If Not Cells(row, column_number).Value Like "red" Then Cells(row, column_number).Delete xlUp
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文