2017-02-13 71 views
2

因此,基本上我需要創建一個單元格ex.g(1,10,15,16)中學生完成的課程數量列表。課程的最大數量是50.課程被其編號識別如果單元格包含Excel然後將其寫入另一個

我想要做的是創建一個公式,該公式顯示所有未在另一個單元格中完成的課程。我的意思是:在一個單元格中,我寫的是學生已經完成的例程1,5,7,而在另一個單元格中,結果會自動將所有數字都設置爲50,除了完成的單元格爲2,3, 6,8 ....

我試着用

=ISNUMBER(SEARCH(substring,text)) 

但這並不能給我很好的結果。

+0

因此,一個細胞將爲前。 1,2,3,4,5,6等一個會自動被7,8,9,10,12 ...最多50. –

+1

Excel 2016,最新的一個 –

回答

3

爲此,我們需要將,上的字符串拆分,然後將其替換爲無。

這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) 

enter image description here

+0

非常感謝您的回答 –

3

這是另一種選擇:

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 
+0

尼斯其中一個,但我會做更少的循環8P –

+0

哈哈我注意到,就在我發佈之前,但認爲我還可以繼續。好答案 :) – CallumDA

2

一點點增強斯科特的解決方案:

  • 讓用戶隨意指定的教訓總數(默認值= 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 
相關問題