我已经实现了一个数据验证单元内下拉列表,该列表用于在单元格列中保留多个值。当前,您可以按任何顺序从下拉列表中进行选择,单元格将按照该顺序进行填充。有没有一种方法可以强制订单与下拉列表的来源保持一致?
例如:我的下拉列表是:
选择的顺序如下:
我希望该单元格显示:
Jim, Tom, Bob
以下是我当前用于数据验证下拉列表的VBA代码:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
' To allow multiple selections in a Drop Down List
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Column = 13 Then
If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
GoTo Exitsub
Else
If Target.Value = "" Then
GoTo Exitsub
Else
Application.EnableEvents = False
Newvalue = Target.Value
Application.Undo
Oldvalue = Target.Value
If Oldvalue = "" Then
Target.Value = Newvalue
Else
If InStr(1, Oldvalue, Newvalue) = 0 Then
Target.Value = Oldvalue & ", " & Newvalue
Else
Target.Value = Oldvalue
End If
End If
End If
End If
End If
Exitsub:
Application.EnableEvents = True
End Sub
基本上,以上代码(由前同事提供给我,不是我自己的发明)使我可以从单元格列表中保留多个选择,并用逗号分隔。效果很好,但是从列表中选择的内容按照选择的顺序显示在单元格中。
我需要它们按照它们在列表中的顺序显示。在示例中,如果有人选择Bob
,然后选择Tom
,然后选择Ryan
,则当前代码将显示Bob, Tom, Ryan
。我需要代码对选择进行重新排序以显示为Tom, Bob, Ryan
。
答案 0 :(得分:0)
尝试一下-对原始版本进行了一些更改,包括如果您选择了已选择的内容,则会将其从选择中删除。
import React, { Component } from 'react';
import { ActivityIndicator } from 'react-native';
import { connect } from 'react-redux';
import * as actions from '../../actions';
class Loading extends Component {
constructor(props){
super(props);
}
componentDidMount(){
this.props.checkSigned();
switch(this.props.isSigned){
case null : return;
case false : this.props.navigation.navigate('Auth');
case true : this.props.navigation.navigate('App')
}
}
render(){
return (
<ActivityIndicator size="large" color="black" />
)
}
}
const mapStateToProps = ({signed}) => {
const {isSigned} = signed;
return {
isSigned
}
}
export default connect(mapStateToProps, actions)(Loading);
答案 1 :(得分:0)
您可以在顶部添加它:
Dim nameArray() As String
Dim sortedArray() As Variant: sortedArray = Array("Tom", "Bob", "Ryan") 'etc whatever order you need
Dim finalArray() As Variant
Dim spot1 As Integer
Dim spot2 As Integer: spot2 = 0
Dim name as String
并且还将此权利包括在Target.Value = Oldvalue & ", " & Newvalue
下:
Target.Value = Replace(Target.Value, ",", "")
nameArray = Split(Target.Value)
For spot1 = 0 To UBound(nameArray)
For Each name in nameArray
If name = sortedArray(spot1)
finalArray(spot2) = name
spot2 = spot2 + 1
End If
Next
Next
Target.Value = ""
For spot1 = 0 To UBound(finalArray)
If spot1 <> UBound(finalArray) Then
Target.Value = Target.Value & finalArray(spot1) & ", "
Else
Target.Value = finalArray(spot1)
End If
Next
无法自行测试,因此请确保您在测试之前保存文件。
祝你好运