Excel VBA:向工作表添加形状时,位置只是一点点closures

所以我正在创build一个电子表格,它将查找列标题,并将一个形状添加到列标题下方某行中的单元格。 但是,当我运行代码时,具有较小列号(靠近原点)的形状非常接近,但是在列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 

我似乎无法在任何地方find答案,我真的希望这不是一个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提供的testingbuild议,我得到了一些东西。 我发现,当我选中所有单元格(通过单击电子表格的左上angular)并同时对它们进行大小调整时,可能会影响偏移量。 如果我把这些单元格变得太窄,那么它们的形状就会偏移到应该在的位置。 如果我把这些单元做得太宽,这些形状就会抵消它们应该在的位置。 类似的效果也发生在垂直方向上。

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

我不知道为什么发生这种情况,我不知道如何解决这个问题。 但是我的问题现在消失了? 那么…大家好吗? 我猜? 如果有人能够回答这个问题的原因,我很乐意宣布他们是这个问题的胜利者。