[英]Excel if cell contain then write it in another
所以基本上我需要創建學生在一個單元格中完成的課程列表(1,10,15,16)。 課程的最大數量為50.課程由其編號確認
我想要做的是創建一個公式,顯示在另一個單元格中完成的所有課程。 我的意思是:在一個單元格中,我寫了一個學生已完成的課程,例如1,5,7,而在另一個單元格中,結果將自動為所有數字,最多50個除了完成的那個,因此單元格將是2,3, 6,8 ....
我試過了
=ISNUMBER(SEARCH(substring,text))
但這並沒有給我帶來好結果。
要做到這一點,我們需要將字符串拆分為,
然后用什么都替換thos值。
這個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)
這是另一種選擇:
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
斯科特解決方案的一點改進:
讓用戶可選擇指定課程總數(默認= 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.