我在excel中有一个图表。在这个图表里面有一个文本框。我想将此文本框放置在图表的左上角。
当我手动执行此操作并读出属性 .Top
和 .Left
我得到 -4
作为回应。
如果我尝试将这两个属性设置为 -4
文本框转到位置 0
而不是 -4
.
为什么我不能将 texbox 放在图表的左上角?
这里是文本框的一些外边距还是图表/图表区域的内边距?
下面是一些使用任何图表测试它的代码,您在其中插入了一个文本框:
Sub TextBoxPositionUpperLeftCorner()
Dim cho As ChartObject
Dim sh As Shape
For Each cho In ActiveSheet.ChartObjects
For Each sh In cho.Chart.Shapes
'reading out the position
Debug.Print "Top: " & sh.Top
Debug.Print "Left: " & sh.Left
'setting the position
sh.Top = -4
sh.Left = -4
Next
Next
End Sub
最佳答案
似乎您无法将 Top/Left 设置为负数。
但是您可以增加位置来实现这一点:
Sub TextBoxPositionUpperLeftCorner()
Dim cho As ChartObject
Dim sh As Shape
For Each cho In ActiveSheet.ChartObjects
For Each sh In cho.Chart.Shapes
'reading out the position
Debug.Print "Top: " & sh.Top
Debug.Print "Left: " & sh.Left
sh.IncrementTop -(sh.Top + 4)
sh.IncrementLeft -(sh.Left + 4)
'reading out the position
Debug.Print "Top: " & sh.Top
Debug.Print "Left: " & sh.Left
Next
Next
编辑:经过更多测试后,即使使用这种方法,您也能得到的最负面结果是 -4/-4。
关于vba - 在 Excel 中的图表中将文本框定位到顶部/左侧 -4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43100316/