您可以通过使用过滤器来做到这一点:
- 通过取消选中(空白)过滤列 BC
- 复制列
- 粘贴到新的工作表或工作簿中
如果它必须是 VBA,这里有两个代码可以根据需要执行。第一个代码使用自动过滤器:
Private Sub CommandButton3_Click()
Dim wsData As Worksheet
Dim wsNew As Worksheet
Set wsData = ActiveSheet
Set wsNew = Sheets.Add
With Intersect(wsData.UsedRange, wsData.Columns("BC"))
.Parent.AutoFilterMode = False
.AutoFilter 1, "<>"
Intersect(.SpecialCells(xlCellTypeVisible).EntireRow, wsData.Range("A:A,B:B,C:C,E:E,BC:BC")).Copy
wsNew.Range("A1").PasteSpecial xlPasteValues
wsNew.Range("A1").PasteSpecial xlPasteFormats
.AutoFilter
End With
wsNew.Move
Set wsData = Nothing
Set wsNew = Nothing
End Sub
第二个替代代码使用查找循环:
Private Sub CommandButton3_Click()
Dim rngFound As Range
Dim rngCopy As Range
Dim strFirst As String
Set rngFound = Columns("BC").Find("*", Cells(Rows.Count, "BC"), xlValues, xlWhole)
If Not rngFound Is Nothing Then
strFirst = rngFound.Address
Set rngCopy = rngFound
Do
Set rngCopy = Union(rngCopy, rngFound)
Set rngFound = Columns("BC").Find("*", rngFound, xlValues, xlWhole)
Loop While rngFound.Address <> strFirst
End If
If Not rngCopy Is Nothing Then
Sheets.Add
Intersect(rngCopy.Parent.Range("A:A,B:B,C:C,E:E,BC:BC"), rngCopy.EntireRow).Copy
Range("A1").PasteSpecial xlPasteValues
Range("A1").PasteSpecial xlPasteFormats
ActiveSheet.Move
End If
Set rngFound = Nothing
Set rngCopy = Nothing
End Sub