vba - 在 Excel 中的图表中将文本框定位到顶部/左侧 -4

标签 vba excel ms-office

我在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/

相关文章:

sql - 如何在 Excel 工作表中的命名范围上运行 SQL 语句?

winforms - MS Office PIA "backward compatibility"

windows - 如何从 Inno Setup 安装程序的注册表中获取 Office 的安装目录

excel - 从 Excel VBA 打印到 DYMO 打印机

vba - 根据背景颜色计算excel中文本的实例

excel - 无错误时显示“出现错误时转到”(针对特定情况)

vba - 为什么我的 for 循环没有加紧?

vba - Excel VBA 优化

php - excel如何读取XML文件?

database - 收集 80 个用户的数据,隐藏其他用户的数据