-1

迅速でエレガントな解決策を探している VBA の問題があります。

問題はチェックボックスに関するものです: それらの選択と選択解除。

1 行と 4 列のデータ (セル A1:A4:

A   B   Star    D

、 、 の3 つのチェックされていないボックス があります。GalaxyUniversePlanet

チェックするGalaxyと、行は動的に (次の空の行に) コピーされます。

A   B   Star    D
A   B   Galaxy  D

3つすべてをチェックすると、次のようになります。

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 に答える