InputBox中大量选择之间的动作

时间:2018-11-20 10:08:30

标签: excel vba excel-vba

我正在从InputBox获取字符串数组,

Set productName = Application.InputBox("Select products from list:", _
  "Our dialog", , , , , , 8)

每次选择后,我要为我选择的单元着色。 例如,我要选择10个单元格(用一个InputBox操作)。 我选择一个(在此步骤中,我要为该单元格着色,要记住,我已经选择了它),然后按CTRL键,选择第二个(为单元格着色),选择第三个(为单元格着色),等等。

是否可以使用VBA做到这一点?

2 个答案:

答案 0 :(得分:1)

Sub ran()
    Dim stc As Range
    Dim ytd As String
    Do
        On Error GoTo cc
        Set stc = Application.InputBox("Selection", "Select range", Type:=8)

        Dim cell As Range
        stc.Interior.ColorIndex = 8
    Loop
cc:
    MsgBox ("Done")
    Exit Sub
End Sub

答案 1 :(得分:1)

您的问题“是否可以使用VBA做到这一点” 的答案是:不可以。

您不能仅使用一个 InputBox来执行此操作,但是您可以一直询问范围选择,直到用户按下“取消”为止。

Option Explicit

Public Sub ColorizeSelections()
    Dim SelRange As Range
    Dim ColorRange As Range

    Do
        On Error Goto CANCEL_LOOP 'next line throws error if cancel is pressed
        Set ColorRange = Application.InputBox("Select products from list:", "Our dialog", , , , , , 8)
        On Error GoTo 0 'always re-activate error reporting!!!

        ColorRange.Interior.Color = vbGreen 'color new selected range

        'remember all selected ranges in SelRange
        If SelRange Is Nothing Then
            Set SelRange = ColorRange
        Else
            Set SelRange = Union(SelRange, ColorRange)
        End If

        SelRange.Select 'select all previously selected ranges that were already colored.
    Loop
CANCEL_LOOP:
    'other stuff goes here
End Sub