find文本框的单元格位置
我已经使用下面的VBA函数创build了一个文本框:
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绘制文本框的单元格位置。 我特别需要右下angular的单元格位置。 目标是DrawPostIt()
函数将返回单元格的位置/位置。
注意:在这里我已经find了如何放置一个文本框来指示基于给定单元格的位置( 请参阅参考资料 ),但这不完全是我想要的,因为我不知道单元格的位置。
尝试这个…
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
您可以使用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
请试试这个
运行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