如何将字符串用户输入 (RGB) 传递给 VBA

How to pass string user input (RGB) to VBA

我有一个用户窗体,其中 RGB 值要分配给稍后将由宏创建的形状:

Private Sub UserForm_Initialize()

    With lstChosenColor
        lstChosenColor.AddItem "RGB(0, 0, 0)"
        lstChosenColor.AddItem "100, 100, 100" 'different from the above to show one alternative of my trials
    End With

End Sub

但是我不知道如何将选定的值插入宏本身。我尝试了很多方法:

    Dim lstChosenColor As String 'somewhere else i saw it as Long, tried but without success
'   Dim ChosenColor As String
'   ChosenColor = lstChosenColor 'I tried this one, too



        If lstChosenColor = False Then
            MsgBox "No Color Selected"
        Else: New_Shape.Fill.ForeColor.RGB = lstChosenColor.Selected 'I tried also 'New_Shape.Fill.ForeColor.RGB = lstChosenColor and 'New_Shape.Fill.ForeColor.RGB = RGB(lstChosenColor)
        End If



            Set New_Shape = myDocument.Shapes.AddShape(Type:=msoShapeOval, Left:=Shp_Cntr - ((Shp.Width + 2) / 2), Top:=Shp_Mid - ((Shp.Width + 2) / 2), Width:=Shp.Width, Height:=Shp.Width)
'            New_Shape.Fill.ForeColor.RGB = RGB(lstChosenColor) 'I commented this as it is part of a repeating block for a various number of shapes and I thought I could assign the RGB value above

有谁能指点一下吗?

如果您使用(例如)“100、100、100”作为值,您可以执行以下操作:

Dim v, arr

v = lstChosenColor.Value              'get selected value

arr = Split(Replace(v, " ", ""), ",") 'remove any spaces and split to array 

'assign each array element as an argument to RGB()
New_Shape.Fill.ForeColor.RGB = RGB(CLng(arr(0)), CLng(arr(1)), CLng(arr(2)))