网上找到两种方案,一种利用数据选择其他单元格,也就是在其他单元格建数据。需求是模板,不合适
这里我用的VBA,踩了挺多坑,详细说下
首先更新WPS为最新版 ,确保可用VBA和JSA 确定使用VBA 还是JSA,两种语法不同
- VBA较老,语法可靠些,推荐(本文使用VBA)
- JSA为新引入JS,但支持较少,不太推荐
- 如选择VBA,请跳转至以下链接查看详情。若为JSA,请另寻方案
VBA配置借鉴了csdn一篇博客,以下为相关链接:
CSDN-指南:https://blog.csdn.net/qq_36448758/article/details/134162812
金山文档-多选方案:https://www.kdocs.cn/article/2C6CD5AA30.html
以下为打开方式


默认为空,先创建一个新的宏再编辑

此处为VBA编辑,注意在红色框处写入代码



若不生效的话再切回WPS点击运行

最后附上版本图

以下为VBA代码
不选择值时显示空,选址值后替换空,用、间隔
1 Private Sub Worksheet_Change(ByVal Target As Range)
2 Dim oldVal As String
3 Dim newVal As String
4 Dim delimiter As String
5 delimiter = "、" ' 设置分隔符
6
7 ' 限定只在B列进行处理
8 If Not Intersect(Target, Me.Range("B:B")) Is Nothing Then
9 If Target.Count > 1 Then Exit Sub
10
11 On Error Resume Next
12 Application.EnableEvents = False
13 newVal = Trim(Target.Value) ' 去除新值两侧空格
14 Application.Undo
15 oldVal = Trim(Target.Value) ' 去除旧值两侧空格
16
17 If oldVal <> "" And newVal <> "" Then
18
19 ' 如果最终值为空,保持为空
20 If InStr(Target.Value, "空") Then
21 Target.Value = newVal ' 移除新值(如果它出现在开头)
22 Else
23 If InStr(1, oldVal, newVal) > 0 Then
24 ' 移除新值及其前面的分隔符
25 Target.Value = Replace(oldVal, delimiter & newVal, "")
26 Target.Value = Replace(Target.Value, newVal, "") ' 移除新值(如果它出现在开头)
27 Else
28 ' 新值不在旧值中,则添加它
29 If oldVal <> "" Then
30 Target.Value = oldVal & delimiter & newVal
31 Else
32 Target.Value = newVal
33 End If
34 End If
35 End If
36 End If
37
38 ' 清理多余的分隔符
39 Target.Value = Application.Trim(Target.Value) ' 去掉两侧空格
40 If Left(Target.Value, Len(delimiter)) = delimiter Then
41 Target.Value = Mid(Target.Value, Len(delimiter) + 1)
42 End If
43 If Right(Target.Value, Len(delimiter)) = delimiter Then
44 Target.Value = Left(Target.Value, Len(Target.Value) - Len(delimiter))
45 End If
46
47 ' 如果最终值为空,保持为空
48 If Target.Value = "" Then
49 Target.Value = "空" ' 设置为一个空字符串
50 ElseIf Target.Value = "0" Then
51 Target.Value = "空" ' 设置为一个空字符串
52 ElseIf Target.Value = 0 Then
53 Target.Value = "空" ' 设置为一个空字符串
54 End If
55 End If
56
57 exitHandler:
58 Application.EnableEvents = True
59 End Sub
