0

我想将来自三个不同下拉列表的数据插入到我的电子表格中,但我遇到了一些困难。共有三个下拉菜单,称为 cbo_fac1、cbo_fac2 和 cbo_fac3。


以下是用例:

用例 1 -No preference插入电子表格的结果

cbo_fac1 - No preference
cbo_fac2 - No preference 
cbo_fac3 - No preference 

用例 2a - 结果value, ,被插入到电子表格中。应该只是value

cbo_fac1 - Value
cbo_fac2 - No preference
cbo_fac3 - No preference 

用例 2b - 结果, value,被插入到电子表格中。应该只是value

cbo_fac1 - No preference
cbo_fac2 - Value
cbo_fac3 - No preference

用例 2c - 结果, , value被插入到电子表格中。应该只是value

cbo_fac1 - No preference 
cbo_fac2 - No preference 
cbo_fac3 - Value

用例 3a - 结果value, value,被插入到电子表格中。应该只是value, value

cbo_fac1 - Value
cbo_fac2 - Value 
cbo_fac3 - No preference

用例 3b - 结果, value, value被插入到电子表格中。应该只是value, value

cbo_fac1 - No preference
cbo_fac2 - Value 
cbo_fac3 - Value  

用例 3c - 结果value, , value被插入到电子表格中。应该只是value, value

cbo_fac1 - Value
cbo_fac2 - No preference
cbo_fac3 - Value      

如何删除用例 2a-3c中的多余逗号?

到目前为止,这是我的代码:

If Me.cbo_fac1 <> "No preference" Then
cbo_fac1Entry = Me.cbo_fac1.Value
Else
cbo_fac1Entry = ""
End If

If Me.cbo_fac2 <> "No preference" Then
cbo_fac2Entry = Me.cbo_fac2.Value
Else
cbo_fac2Entry = ""
End If

If Me.cbo_fac3 <> "No preference" Then
cbo_fac3Entry = Me.cbo_fac3.Value
Else
cbo_fac3Entry = ""
End If

cbo_facEntry = cbo_fac1Entry & ", " & cbo_fac2Entry & ", " & cbo_fac3Entry

If cbo_facEntry = " , ," Then
cbo_facEntry = "No preference"
Else
End If
rng1.Offset(1, 15) = cbo_facEntry

我试过插入这段代码:

if right(cbo_facEntry,1)="," then
   cbo_facEntry= left(len(cbo_facEntry)-1)
end if

if left(cbo_facEntry,1)="," then
   cbo_facEntry= right(len(cbo_facEntry)-1)
end if

后:

If cbo_facEntry = ",," Then
cbo_facEntry = "No Preference"
Else
End If

但这并没有太大区别,因为它不满足所有用例的要求,并且还返回无效的语法错误消息。


提前致谢!

4

2 回答 2

2

这是一个简单的片段:

Dim a(2) As String
a[0] = cbo_fac1
a[1] = cbo_fac2
a[2] = cbo_fac3

Dim I As Integer, result As String
For I = 0 To UBound(a)
    If a(I) <> "No Preference" Then
        If I = 0 Then
            result = a(I)
        Else
            result = result & "," & a(I)
        End If
    End If
Next

我没有测试过这段代码,但你应该明白了。

于 2012-10-30T14:10:08.823 回答
1

您可以将这三行添加到您的代码中,而不是您尝试过的代码:

cbo_facEntry = Replace(cbo_facEntry, ", , ", ", ")
If Left(cbo_facEntry, 1) = "," Then cbo_facEntry = Right(cbo_facEntry, Len(cbo_facEntry) - 2)
If Right(cbo_facEntry, 2) = ", " Then cbo_facEntry = Left(cbo_facEntry, Len(cbo_facEntry) - 2)
于 2012-10-30T15:00:12.947 回答