-1

我有一个 VBA 问题,我们正在寻找一个快速而优雅的解决方案。

问题与复选框有关:它们的选择和取消选择。

假设我们从一行四列数据开始(单元格 A1:A4:

A   B   Star    D

我们有三个未选中的框: Galaxy, Universe, Planet

检查后Galaxy,该行被动态复制(到下一个空行)

A   B   Star    D
A   B   Galaxy  D

如果我们检查所有三个我们有:

A   B   Star    D
A   B   Galaxy  D
A   B   UniverseD
A   B   Planet  D

现在,如果我们取消选中Universe.. 该行将自动省略为:

A   B   Star    D
A   B   Galaxy  D
A   B   Planet  D

我喜欢这种设置的实时“如你所见”的感觉。有没有一种我不能用来编码的优雅方法?

提前谢谢了。

4

1 回答 1

0
Function GetDataBasedOnSelection(ByVal galaxyChecked As Boolean, ByVal universeChecked As Boolean, _
ByVal planetChecked As Boolean) As Variant
Dim currentRow As Integer
Dim retVal(1 To 3, 1 To 4)

currentRow = 1
If galaxyChecked Then
    GoSub fillInCommonValuesInThisRow
    retVal(currentRow, 3) = "Galaxy"
    currentRow = currentRow + 1
End If

If universeChecked Then
    GoSub fillInCommonValuesInThisRow
    retVal(currentRow, 3) = "Universe"
    currentRow = currentRow + 1
End If

If planetChecked Then
    GoSub fillInCommonValuesInThisRow
    retVal(currentRow, 3) = "Planet"
    currentRow = currentRow + 1
End If

GetDataBasedOnSelection = retVal
Exit Function

fillInCommonValuesInThisRow:
    retVal(currentRow, 1) = "A"
    retVal(currentRow, 2) = "B"
    retVal(currentRow, 3) = ""
    retVal(currentRow, 4) = "D"
Return
End Function

如何使用上述方法:

'Pass the actual selection (true if the checkbox is checked, false otherwise)
Range("A2:D4").Value = GetDataBasedOnSelection(True, False, True)
于 2012-10-30T13:46:49.763 回答