我试图使用vba插入一个文本框并提示用户键入一个选项,即1-3,并使用select case执行相应的代码,这将改变文本框填充和字体颜色,如我所述。
我不太确定在Select Case之后我应该放什么,目前我把Select case y放在我认为可能是我的代码不起作用的原因。
欣赏是否有人可以尝试发现我的错误并帮助我将其转换为正常工作的代码。
Sub Insert_Textbox()
Dim sld As slide, sh As Shape, cursh As Shape
Dim y As Long
Dim chtcolor01 As Long, chtcolor02 As Long, chtcolor03 As Long, w As Long, b As Long
w = RGB(250, 250, 250)
b = RGB(0, 0, 0)
chtcolor01 = RGB(250, 0, 0)
chtcolor02 = RGB(0, 250, 0)
chtcolor03 = RGB(0, 0, 250)
Set sld = Application.ActiveWindow.View.slide
Set sh = sld.shapes.AddShape(Type:=msoShapeRectangle, _
Left:=50, Top:=50, Width:=150, Height:=50) 'this shape will now be called sh
With sh
y = InputBox("Please select a number between 1 to 3 for the color for your textboxes", "Choose your color!")
Select Case y
Case y = 1
.Fill.ForeColor.RGB = chtcolor01
.textFrame.TextRange.Font.Color.RGB = b
.Line.Visible = msoFalse 'remove border
y = 2
.Fill.ForeColor.RGB = chtcolor02
.textFrame.TextRange.Font.Color.RGB = b
.Line.Visible = msoFalse 'remove border
y = 3
.Fill.ForeColor.RGB = chtcolor02
.textFrame.TextRange.Font.Color.RGB = w
.Line.Visible = msoFalse 'remove border
End With
End Sub
以下是史蒂夫帮助的修订代码。希望其他人也觉得它也很有用。
Sub Insert_Textbox()
Dim sld As slide, sh As shape, cursh As shape
Dim Y As String
Dim chtcolor01 As Long, chtcolor02 As Long, chtcolor03 As Long, w As Long, b As Long
w = RGB(250, 250, 250)
b = RGB(0, 0, 0)
chtcolor01 = RGB(250, 0, 0)
chtcolor02 = RGB(0, 250, 0)
chtcolor03 = RGB(0, 0, 250)
Set sld = Application.ActiveWindow.View.slide
Set sh = sld.Shapes.AddShape(Type:=msoShapeRectangle, _
Left:=50, Top:=50, Width:=150, Height:=50) 'this shape will now be called sh
With sh
Y = InputBox("Please select a number between 1 to 3 for the color for your textboxes", "Choose your color!")
Select Case Y
Case Is = 1
.Fill.ForeColor.RGB = chtcolor01
.TextFrame.TextRange.Font.Color.RGB = b
.Line.Visible = msoFalse 'remove border
Case Is = 2
.Fill.ForeColor.RGB = chtcolor02
.TextFrame.TextRange.Font.Color.RGB = b
.Line.Visible = msoFalse 'remove border
Case Is = 3
.Fill.ForeColor.RGB = chtcolor02
.TextFrame.TextRange.Font.Color.RGB = w
.Line.Visible = msoFalse 'remove border
Case Else
MsgBox ("The number you typed is invalid, please try again")
End Select
End With
End Sub