【问题标题】:Find the cell location of the text box查找文本框的单元格位置
【发布时间】:2017-12-20 02:26:36
【问题描述】:

我使用以下 VBA 函数创建了一个文本框:

Function DrawPostIt(Left As Single, Top As Single, Width As Single, _
    Height As Single, Text As String) As String
    ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, Left, _
        Top, Width, Height).Select
    With Selection.ShapeRange.Fill
        .Visible = msoTrue
        .ForeColor.RGB = RGB(255, 192, 0) ' Yellow post-it
        .Transparency = 0
        .Solid
    End With
    DrawPostIt = "BottomRightCell"
End Function

现在我想确定 excel 绘制文本框的单元格位置。我特别需要右下角的单元格位置。目标是DrawPostIt() 函数将返回单元格位置/位置。

注意:在这里我找到了如何根据给定的单元格(see)放置一个指示位置的文本框,但这并不是我想要的,因为我不知道预先确定单元格位置。

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    试试这个...

    Sub CallTheFunction()
    Dim Cell As Range
    Set Cell = DrawPostIt(100, 150, 250, 150, "MyTextBox1")
    MsgBox Cell.Address
    End Sub
    
    Function DrawPostIt(Left As Single, Top As Single, Width As Single, _
        Height As Single, Text As String) As Range
        ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, Left, _
            Top, Width, Height).Select
        With Selection.ShapeRange.Fill
            .Visible = msoTrue
            .ForeColor.RGB = RGB(255, 192, 0) ' Yellow post-it
            .Transparency = 0
            .Solid
        End With
        Selection.ShapeRange.TextFrame2.TextRange.Characters.Text = Text
        Set DrawPostIt = Selection.BottomRightCell
    End Function
    

    如果你想在特定的已知单元格中绘制它,你可以试试这个...

    Sub CallTheFunction2()
    Dim Cell As Range
    
    Set Cell = Range("D5")  'Here you can defind the cell
    
    DrawPostIt2 Cell.Left, Cell.Top, 200, 100, "MyTextBox2"
    End Sub
    
    Function DrawPostIt2(Left As Single, Top As Single, Width As Single, _
        Height As Single, Text As String)
        ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, Left, _
            Top, Width, Height).Select
        With Selection.ShapeRange.Fill
            .Visible = msoTrue
            .ForeColor.RGB = RGB(255, 192, 0) ' Yellow post-it
            .Transparency = 0
            .Solid
        End With
        Selection.ShapeRange.TextFrame2.TextRange.Characters.Text = Text
    End Function
    

    【讨论】:

    • 非常感谢,只需简单的复制和粘贴即可按预期工作。
    • 不客气,大卫!很高兴它按预期工作。 :)
    【解决方案2】:

    您可以使用Shape 对象的BottomRightCell 属性。

    Selection.BottomRightCell.Address
    

    设置对文本框的引用比使用Selection 更好。像这样的:

    Dim box as Shape
    Set box = ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, Left, _
            Top, Width, Height)
    With box.ShapeRange.Fill
        .Visible = msoTrue
        .ForeColor.RGB = RGB(255, 192, 0) ' Yellow post-it
        .Transparency = 0
        .Solid
    End With
    

    【讨论】:

    • 我试图重现您的解决方案,但出现错误:“438-Object 不支持此属性或方法”。在With box... 线上。我修改了问题以将解决方案包装成一个函数,其想法是返回右下角的单元格位置。谢谢。
    【解决方案3】:

    请试试这个

    运行 testMe

    Function drawPostIt(Left As Single, Top As Single, Width As Single, Height As Single, Text As String) As Range
    
        Dim aaa As Shape
        Set aaa = ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, Left, Top, Width, Height)
    
        aaa.Title = "my fancy yellow post-it"
        aaa.TextFrame2.TextRange.Text = Text
        aaa.Fill.Visible = msoTrue
        aaa.Fill.ForeColor.RGB = RGB(255, 192, 0) ' Yellow post-it ... lol ... orange
        aaa.Fill.Transparency = 0
        aaa.Fill.Solid
    
    '   aaa.TopLeftCell.Select                ' these two lines are for testing
    '   aaa.BottomRightCell.Select            ' this is the range of interest
    
        Set drawPostIt = aaa.BottomRightCell
    
    '   aaa.Delete                             ' for testing
    
    End Function
    
    Sub testMe()
    
        ActiveSheet.Range("a1").Select         ' move selection box out of the way (not needed though)
    
        Dim bottomRight As Range
        Set bottomRight = drawPostIt(50, 90, 120, 70, "message on postit")    ' drawPostIt() returns a range object
    
        bottomRight.Select                      ' drawPostIt() returns a range object
    
    End Sub
    

    【讨论】:

    • 谢谢,我选择了@sktneer 答案,因为它无需修改即可直接工作,想法与您的相同,但我无法重现您的,可能是一些小东西。
    猜你喜欢
    • 2017-04-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    • 2017-03-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-08
    相关资源
    最近更新 更多