简体   繁体   English

如何在 Excel 中使用 VBA 根据换行符过滤单个单元格中的行?

[英]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.

相关问题 如何从单元格中删除换行符,但将地址格式保留在同一单元格VBA中 - How can I remove line breaks from a cell but keep the address format in the same cell VBA VBA Excel替换单元格中的换行符 - VBA Excel replace line breaks in a cell 如何使用换行符将具有换行符的不同单元格拆分为一个单元格(VBA Excel) - How to loop the split of different cells with line breaks into one cell with line breaks (VBA Excel) 我可以在 excel 中使用 VBA 来保持基于单元格中的字母的累积总数,并且该单元格以黄色突出显示吗? - Can I use VBA in excel to keep a cumulative total based on a letter in a cell and that cell being highlighted yellow? 如何使用 VB 或公式从 Excel 单元格中删除空白换行符? - How can I remove blank line breaks from an excel cell with VB or a formula? 如何在Excel VBA中合并或读取带有换行符/回车符(CR)的csv文件? - How can I merge or read csv files with line breaks / carriage return (CR) in Excel VBA? excel或excel VBA:如何让用户单击某个单元格并将其用作与现有单元格的匹配结果 - excel or excel VBA: how can I let user click on a cell and use it as a match result to an existing cell 使用Excel VBA在Excel单元格中创建多个换行符 - Create several line breaks in excel cell using Excel VBA 如何在Excel VBA中移动形状使其中心与单元格左边缘对齐 - How can I move a shape so its centre lines up with a cell left edge in excel VBA VBA Excel-如何根据另一个单元格的值自动在excel中填充系列? - VBA Excel - How can I automatically fill a series in excel, based on the value of another cell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM