[英]How can I use VBA in Excel to filter lines in a single cell based on line breaks?
I have this cell (and a whole column of similar cells) containing part numbers and their statuses and I need to filter out the numbers marked complete while retaining the rest.我有这个包含部件号及其状态的单元格(以及一整列类似单元格),我需要过滤掉标记为完成的数字,同时保留 rest。
I know there's a way to separate the information based on the line breaks but after that I'm at a loss.我知道有一种方法可以根据换行符分隔信息,但在那之后我不知所措。 I've tried creating an array from the cell but the array makes it difficult to remove information.
我尝试从单元格创建一个数组,但该数组很难删除信息。
I feel like there's a simple solution to this but I'm just not seeing it.我觉得有一个简单的解决方案,但我只是没有看到它。 *Edit- I'm using Excel 2016 so I don't have a lot of the newer functions.
*编辑-我使用的是 Excel 2016,所以我没有很多新功能。
Try this formula (data is in cell A1):试试这个公式(数据在单元格 A1 中):
=LET(XML, "<t><s>" & SUBSTITUTE(A1,CHAR(10),"</s><s>") & "</s></t>",
Data, FILTERXML(XML,"//s"),
FILTER(Data,NOT(ISNUMBER(SEARCH("Complete",Data)))))
or if you want it returned in a single cell:或者如果您希望它在单个单元格中返回:
=TEXTJOIN(CHAR(10),TRUE,
LET(XML, "<t><s>" & SUBSTITUTE(A1,CHAR(10),"</s><s>") & "</s></t>",
Data, FILTERXML(XML,"//s"),
FILTER(Data,NOT(ISNUMBER(SEARCH("Complete",Data))))))
colArray = Array("H")
check_col = colArray(0)
ColLastRow = Range(check_col & Rows.Count).End(xlUp).Row
For Each Rng In Range(check_col & "1" & ":" & check_col & ColLastRow)
If InStr(Rng.Value, vbLf) Then
Rng.EntireRow.Copy
Rng.EntireRow.Insert
For i = 0 To UBound(colArray)
c = colArray(i)
Set currentrng = Range(c & Rng.Row)
Set upperRng = currentrng.Offset(-1, 0)
upperRng.Value = Mid(currentrng.Value, 1, InStr(currentrng.Value, vbLf) - 1)
currentrng.Value = Mid(currentrng.Value, Len(upperRng.Value) + 2, Len(currentrng.Value))
Next i
End If
Next
This is what I ended up using.这就是我最终使用的。 Split the cells into separate rows so That I could filter.
将单元格分成单独的行,以便我可以过滤。 Then I used the following code to recombine.
然后我用下面的代码重新组合。
Last_Row = Cells(Rows.Count, 1).End(xlUp).Row
For i = Last_Row To 2 Step -1
If Len(Cells(i, 1)) = 0 Then Rows(i).Delete
If Cells(i, 6) = Cells(i - 1, 6) Then
Cells(i - 1, 9) = Cells(i - 1, 9) & vbLf & Cells(i, 9)
Rows(i).Delete
End If
Next i
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.