搜索特定的字符串并在垂直之间删除所有单元格
我想创建一个 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.
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用自动过滤器,您可以避免循环和逐行删除。
如果您只想修改现有代码,请将此行:更改
为:
Using autofilter you can avoid looping and deleting rows one by one.
If you want to just modify your existing code change this line:
to: