如果我有一组数据(A1:D8),我如何过滤列以显示一个值是否包含数字VBA



A B

<表类> B C D tbody><<tr>布莱斯N6285M布丽安娜N574515道明>MAXN6652MN46518道明>丹妮N6681F米莉N6561F阳光N565111M罗恩N635412M

我不愿意回答,因为我可以用两种方式解释这个问题:1)如何使用VBA过滤数据;或者2)如果左边的单元格包含&;n6 &;,如何填充空单元格。你的问题提到了过滤,但听起来你的目标实际上是#2。如果情况并非如此,而你的目标真的是设置过滤,请在你的帖子中澄清这一点,并接受我对误解的道歉!

当你说单元格"包含N6",我想你的意思是开始N6,如你的数据。

而不是过滤,假设这不是您的真正目的:),我将只是查看列并根据需要进行替换。我还会指定范围,而不是使用Select。

Sub N6ReplaceBlanks()
Dim rngRow As Range, rngData As Range

'  Specify the range instead of using Select
Set rngData = ActiveSheet.Range("A1:D8")

' Look at every row in the data
For Each rngRow In rngData.Rows
' Test whether columns B and C in the row match our criteria
If Left(rngRow.Cells(1, 2), 2) = "N6" And rngRow.Cells(1, 3) = "" Then
rngRow.Cells(1, 3) = 19
End If
Next

End Sub

try

Sub FilterAndReplace()
Dim lastRow As Long
Dim ws As Worksheet
Dim filterRange As Range

' Set the worksheet object
Set ws = ThisWorkbook.Sheets("Sheet1")

' Find the last row in column A
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

' Set the range to filter
Set filterRange = ws.Range("A1:D" & lastRow)

' Filter column B to show only values starting with "N6"
filterRange.AutoFilter Field:=2, Criteria1:="N6*"

' Replace blank values in column C with 19
filterRange.SpecialCells(xlCellTypeVisible).Offset(0, 1).SpecialCells(xlCellTypeBlanks).Value = 19
End Sub

最新更新