Excel 如果单元格包含则将其写入另一个单元格
Excel if cell contain then write it in another
所以基本上我需要在一个单元格中创建学生完成的课程数量列表 ex.g (1,10,15,16)。最大课时数为50节课。课时以课时数来识别
我想做的是创建一个公式来显示在另一个单元格中未完成的所有课程。我的意思是:在一个单元格中,我写了学生已经完成的课程,例如 1、5、7,而在另一个单元格中,结果将自动是所有数字,直到 50,除了完成的那个单元格将是 2,3, 6,8....
我试过
=ISNUMBER(SEARCH(substring,text))
但这并没有给我很好的结果。
为此,我们需要拆分 ,
上的字符串,然后将这些值替换为空值。
这个 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
Scott 解决方案的一点改进:
让用户可选择指定总课时数(默认 = 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
所以基本上我需要在一个单元格中创建学生完成的课程数量列表 ex.g (1,10,15,16)。最大课时数为50节课。课时以课时数来识别
我想做的是创建一个公式来显示在另一个单元格中未完成的所有课程。我的意思是:在一个单元格中,我写了学生已经完成的课程,例如 1、5、7,而在另一个单元格中,结果将自动是所有数字,直到 50,除了完成的那个单元格将是 2,3, 6,8....
我试过
=ISNUMBER(SEARCH(substring,text))
但这并没有给我很好的结果。
为此,我们需要拆分 ,
上的字符串,然后将这些值替换为空值。
这个 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
Scott 解决方案的一点改进:
让用户可选择指定总课时数(默认 = 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