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

标签 vba excel excel-2016

所以基本上我需要创建学生在一个单元格中完成的类(class)数量列表,例如(1,10,15,16)。最大类(class)数为 50。类(class)通过其编号进行识别

我想要做的是创建一个公式,显示另一个单元格中未完成的所有类(class)。我的意思是:在一个单元格中,我编写学生已完成的类(class),例如 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)

enter image description here

关于vba - Excel 如果单元格包含,则将其写入另一个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42211703/

相关文章:

arrays - VBA WorksheetFunction.Sum 不适用于数组,但适用于范围?

excel - 具有动态范围和 customOrder excel 的排序宏

sql - MS Access Form - 文本框中的字符串,用于 LIKE 查询以过滤报告结果

ms-access - 引用全局变量 - MS Access

c# - "Unable to set the FreezePanes property of the Window class"Excel 2016 (办公室 365)

excel - 复制工作表时在图表中维护动态命名范围

使用 VBA 读写后 Excel 日期更改(月份和日期交换)

Excel:如何对每 n 列的单元格值求和

Excel 工作表引用一般和超链接功能

python - Pyvot 需要 Visual Studio 吗?