gpt4 book ai didi

vba - Excel VBA : When adding shapes to worksheet, 位置有点偏

转载 作者:行者123 更新时间:2023-12-04 21:04:46 26 4
gpt4 key购买 nike

所以我正在创建一个电子表格,它将查找列标题并将形状添加到列标题下方某一行的单元格中。但是,当我运行代码时,具有较小列号(更接近原点)的形状非常接近,但列 30+ 中的形状有一些错误,并且稍微偏离了它们应该在的位置。这是我的代码。

Public Function Drawshape(ByVal shaperow As Integer, ByVal thisrow As Integer, shapecase As      Integer)
Dim shpcol As Integer
Dim shpleft As Double
Dim shptop As Double
Dim shpwidth As Double
Dim shpheight As Double

'the columns are the day part of a date, so I find the cell that is "x" right of
'the first date, where "x" is calculated based on another sheet.
shpcol = DateDiff("d", Sheet1.Cells(2, 2), Sheet1.Cells(thisrow, 9 + shapecase)) + 2
shpleft = Sheet2.Cells(shaperow, shpcol).Left
shptop = Sheet2.Cells(shaperow, shpcol).Top
shpwidth = Sheet2.Cells(shaperow, shpcol).Width
shpheight = Sheet2.Cells(shaperow, shpcol).Height

'depending on what shapecase is, the date I get is different, and the output shape is different
Select Case shapecase
Case 0
Sheet2.Shapes.AddShape(msoShapeOval, shpleft, shptop, shpwidth, shpheight).Fill.ForeColor.RGB = RGB(255, 255, 255)
Case 1
Sheet2.Shapes.AddShape(msoShapeIsoscelesTriangle, shpleft, shptop, shpwidth, shpheight).Fill.ForeColor.RGB = RGB(255, 255, 255)
Case 2
Sheet2.Shapes.AddShape(msoShapeOval, shpleft, shptop, shpwidth, shpheight).Fill.ForeColor.RGB = RGB(0, 0, 0)
Case 3
Sheet2.Shapes.AddShape(msoShapeIsoscelesTriangle, shpleft, shptop, shpwidth, shpheight).Fill.ForeColor.RGB = RGB(0, 0, 0)
Case Else
MsgBox "Shapes failed"
End Select
End Function

我似乎无法在任何地方找到答案,我真的希望这不是 Excel 错误......

当我尝试:
Debug.Print Sheet2.Shapes("Isosceles Triangle 1744").Left
Debug.Print Sheet2.Range("BG10").Left 'The cell where the shape is supposed to live

我明白了
1425.333
1416

最佳答案

所以我想我可能已经想出了答案,即使它不是一个非常令人满意的答案。感谢 GSerg 提供的测试建议,我不得不摆弄一些东西。我发现当我选择所有单元格(通过单击电子表格的左上角)并同时调整它们的大小时,我可能会影响偏移量。如果我把单元格做得太窄,形状就会偏离它们应该在的位置。如果我把单元格做得太宽,形状就会偏离它们应该在的位置。垂直维度也发生了类似的影响。

有趣的是,如果我以这种方式调整所有单元格的大小,直到形状匹配,然后选择单个列来调整大小,这对偏移没有影响。

我不知道为什么会发生这种情况,也不知道它是如何解决的。但是我的问题现在没有了吗?所以……大家干得好?我猜?如果有人能回答为什么会发生这种情况,我仍然很乐意宣布他们是这个问题的胜利者。

关于vba - Excel VBA : When adding shapes to worksheet, 位置有点偏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26150993/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com