将 Excel 中图表中的文本框定位到顶部/左侧 -4
Position TextBox in Chart in Excel to the top / left -4
我在 excel 中有一张图表。在此图表内有一个文本框。我想把这个文本框放在图表的左上角。
当我手动执行此操作并读出属性 .Top
和 .Left
时,我得到 -4
作为响应。
如果我尝试将这两个属性设置为 -4
,文本框将转到位置 0
而不是 -4
。
为什么我不能将文本框定位到图表的左上角?
这里是文本框的一些外边距还是图表/图表区域的内边距?
这里有一些代码可以用任何图表对其进行测试,您在其中插入了一个文本框:
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。
我在 excel 中有一张图表。在此图表内有一个文本框。我想把这个文本框放在图表的左上角。
当我手动执行此操作并读出属性 .Top
和 .Left
时,我得到 -4
作为响应。
如果我尝试将这两个属性设置为 -4
,文本框将转到位置 0
而不是 -4
。
为什么我不能将文本框定位到图表的左上角?
这里是文本框的一些外边距还是图表/图表区域的内边距?
这里有一些代码可以用任何图表对其进行测试,您在其中插入了一个文本框:
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。