最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

excel - I want to create a textbox with a specific background color that sizes to my selected cell range - Stack Overflow

programmeradmin2浏览0评论

I want to create a textbox with a yellow background and that is sized based on my current selected cell range.

I found a solution for the cell range - but can't figure out how to make the textbox it creates not white. Below is the code I currently have:

Sub TextBox2Selection()
    If TypeName(Selection) = "Range" Then
        With Selection
            ActiveSheet.Shapes.AddTextbox _
              msoTextOrientationHorizontal, .Left, _
              .Top, .Width, .Height
        End With
    End If
End Sub

I want to create a textbox with a yellow background and that is sized based on my current selected cell range.

I found a solution for the cell range - but can't figure out how to make the textbox it creates not white. Below is the code I currently have:

Sub TextBox2Selection()
    If TypeName(Selection) = "Range" Then
        With Selection
            ActiveSheet.Shapes.AddTextbox _
              msoTextOrientationHorizontal, .Left, _
              .Top, .Width, .Height
        End With
    End If
End Sub
Share Improve this question edited Mar 18 at 20:12 BigBen 50.2k7 gold badges28 silver badges44 bronze badges asked Mar 18 at 20:07 user29988185user29988185 211 silver badge2 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

For example:

Sub TextBox2Selection()
    Dim sel As Object
    
    Set sel = Selection
    
    If TypeName(sel) <> "Range" Then Exit Sub 'exit if a range is not selected
    
    ' `AddTextbox` returns a reference to the added shape
    With ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, _
                             sel.Left, sel.Top, sel.Width, sel.Height)
        .Fill.Visible = msoTrue
        .Fill.ForeColor.RGB = RGB(255, 255, 0) 'or vbYellow
    End With
End Sub

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论