我如何获得一个范围内的所有值

我有一个列(缺lesscol),从我的id列中获取所有缺less的数字。 所以从整个单元格区域大部分都是空的,我想创build另外一个只是非空值的列 – 主要是因为有成千上万的行,我需要在顶部看到这些数字。 我没有看到连续丢失了两个,但是这会使逗号分隔的id单元 – 即PW140000024,PW140000025 – 可能需要在某些时候处理。

使缺less的col的函数:

 =IF(MID(E2,3,99)-MID(E1,3,99)>2,"PW"&MID(E1,3,99)+1&",PW"&MID(E2,3,99)-1,IF(MID(E2,3,99)-MID(E1,3,99)>1,"PW"&MID(E1,3,99)+1,"")) 

例:

 id column | missing col | missing without empty rows PW140000023 | | PW140000024 PW140000025 | PW140000024 | PW140000027 PW140000026 | PW140000027 | PW140000029 PW140000028 | PW140000029,PW140000030 | PW140000030 PW140000031 | | PW140000032 | | 

谢谢

假设你的数据看起来像第1行的标题行,而你的数据从第2行开始,id列是E列,你希望“Missing”列是F列:

tigeravatar的例子

在单元格F2中复制下来的是这个公式:

 =IFERROR("PW"&INDEX(SUBSTITUTE($E$2,"PW","")+ROW(INDIRECT("1:"&MAX(INDEX(--(SUBSTITUTE($E$2:$E$7,"PW","")),))-SUBSTITUTE($E$2,"PW","")+1))-1, MATCH(1, INDEX( (COUNTIF(F$1:F1,"*"&INDEX(SUBSTITUTE($E$2,"PW","")+ROW(INDIRECT("1:"&MAX(INDEX(--(SUBSTITUTE($E$2:$E$7,"PW","")),))-SUBSTITUTE($E$2,"PW","")+1))-1,))=0) * (COUNTIF($E$2:$E$7,"*"&INDEX(SUBSTITUTE($E$2,"PW","")+ROW(INDIRECT("1:"&MAX(INDEX(--(SUBSTITUTE($E$2:$E$7,"PW","")),))-SUBSTITUTE($E$2,"PW","")+1))-1,))=0) ,) ,0)) ,"") 

这个公式对于像这个样本这样的一小组数据非常有效,但是我可以看到它将如何开始大幅减慢Excel的数据集。 由于这个复杂性,我会build议创build一个VBA子程序来获得结果给你。

像这样的东西应该工作:

 Sub tgr() Const strIDcol As String = "E" 'Change to the actual column containing the id's Const strOPcol As String = "F" 'Change to the actual column you want to OutPut results to Const lStartRow As Long = 2 'Change to the row your data actually starts on (NOT the header row) Dim ws As Worksheet Dim rngIDs As Range Dim IDCell As Range Dim arrMissing() As String Dim MissingIndex As Long Dim IDMin As Long Dim IDMax As Long Dim i As Long Set ws = ActiveWorkbook.ActiveSheet 'Assuming we're working with the active workbook and active worksheet Set rngIDs = Range(strIDcol & lStartRow, ws.Cells(Rows.Count, strIDcol).End(xlUp)) IDMin = Val(Replace(rngIDs.Cells(1).Value, "PW", vbNullString)) IDMax = Val(Replace(rngIDs.Cells(rngIDs.Cells.Count).Value, "PW", vbNullString)) ReDim arrMissing(1 To IDMax - IDMin + 1 - rngIDs.Cells.Count, 1 To 1) For i = IDMin To IDMax If WorksheetFunction.CountIf(Columns(strIDcol), "*" & i) = 0 Then MissingIndex = MissingIndex + 1 arrMissing(MissingIndex, 1) = "PW" & i End If Next i Range(strOPcol & lStartRow).Resize(MissingIndex).Value = arrMissing End Sub 

通过创build一个起始于PW140000023 ="PW"&140000022+row()在Row1中用="PW"&140000022+row() )开始的id column ,将这些匹配到id column (比如用=MATCH(A1,Sheet1!A:A,0)复制下来以适应,固定这些结果作为值,然后过滤删除数字结果匹配。