如果单元格包含Excel,则将其写入另一个

所以基本上我需要在一个单元格(1,10,15,16)中创build学生完成的课程列表。 课程的最大数量是50.课程被其编号所认可

我想要做的是创build一个公式,显示在另一个单元格中没有完成的所有课程。 我的意思是:在一个单元格中,我写了一些学生已经完成的例程1,5,7,而在另一个单元格中,结果将会自动将所有数字都设置为50,除了这样做的单元格是2,3, 6,8 ….

我试着用

=ISNUMBER(SEARCH(substring,text)) 

但是这不会给我很好的结果。

要做到这一点,我们需要分割string,然后用什么都replacethos值。

这个UDF做你想做的事情:

 Function LessonsLeft(rng As Range) As String If rng.Count > 1 Then Exit Function Dim spltStr() As String Dim i As Long spltStr = Split(rng.Value, ",") LessonsLeft = ",1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50," For i = LBound(spltStr) To UBound(spltStr) LessonsLeft = Replace(LessonsLeft, "," & spltStr(i) & ",", ",") Next i LessonsLeft = Mid(LessonsLeft, 2, Len(LessonsLeft) - 2) End Function 

把它放在一个附在工作簿上的模块中,然后用公式调用它:

 =LessonsLeft(A1) 

在这里输入图像说明

这是另一个select:

 Function MissedLessons(str As String) As String Dim resultString As String Dim i As Integer str = "," & str & "," For i = 1 To 50 If InStr(str, "," & i & ",") = 0 Then resultString = resultString & i & "," End If Next i MissedLessons = Left(resultString, Len(resultString) - 1) End Function 

斯科特解决scheme的一点改进:

  • 让用户有select地指定课程总数(默认值= 50)

  • 避免她编码全部数字串

如下:

 Function LessonsLeft(rng As Range, Optional nLessons As Long = 50) As String If rng.count > 1 Then Exit Function Dim spltStr() As String Dim i As Long With CreateObject("Scripting.Dictionary") For i = 1 To nLessons .Add i, i Next spltStr = Split(rng.Value, ",") For i = LBound(spltStr) To UBound(spltStr) .Remove CLng(spltStr(i)) Next LessonsLeft = Join(.keys, ",") End With End Function