2016-09-07 185 views
0

我在excel中创建了一个宏,通过键盘命令插入一个红色箭头。我想通过研究如何让它停止选择我编写宏时选择的单元格,但我无法弄清楚如何使它每次都插入我当前选择的旁边的箭头。它目前在我记录宏时的原始行中插入箭头。有没有解决的办法?Excel vba插入箭头宏

下面是代码:

Sub Red_Arrow_Insert() 

Red_Arrow_Insert Macro 
Insert Red Arrow 

Keyboard Shortcut: Ctrl+Shift+A 

    ActiveCell.Select 
    ActiveSheet.Shapes.AddConnector(msoConnectorStraight, 264, 50.25, 353.25, 139.5 _ 
     ).Select 
     Selection.ShapeRange.Line.EndArrowheadStyle = msoArrowheadOpen 
    With Selection.ShapeRange.Line 
     .Visible = msoTrue 
     .ForeColor.RGB = RGB(255, 0, 0) 
     .Transparency = 0 
    End With 
    With Selection.ShapeRange.Line 
     .Visible = msoTrue 
     .Weight = 1.5 
    End With 
    ActiveCell.Select 
End Sub 
+0

阅读此:https://msdn.microsoft.com/en-us/library/office/ff834664.aspx – teddy2

+0

此外,我建议阅读[如何避免使用'.Select'](https:// stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros)它是在同一个单元格中完成的,因为你的'ActiveCell'永远不会改变。 – BruceWayne

+0

对不起,这可能会超出我的水平,我不是一个程序员。这是否意味着我的请求可以完成,因为一切都必须相对于文档的左上角进行定义? –

回答

1

该请求可以通过使用TopActiveCellLeft属性,并使用这些数字来放置的箭头来完成,由于TopLeft属性是针对所测量的文档的左上角,并假定箭头将始终为静态长度。

见下面的重构的代码:

Sub Red_Arrow_Insert() 

'Red_Arrow_Insert Macro 
'Insert Red Arrow 
'Keyboard Shortcut: Ctrl Shift + A 

Dim l As Long, t As Long 

l = ActiveCell.Left 
t = ActiveCell.Top 

ActiveSheet.Shapes.AddConnector(msoConnectorStraight, t + 89.25, l + 89.25, l, t).Select 

With Selection 
    With .ShapeRange.Line 
     .EndArrowheadStyle = msoArrowheadOpen 
     .Visible = msoTrue 
     .ForeColor.RGB = RGB(255, 0, 0) 
     .Transparency = 0 
     .Weight = 1.5 
    End With 
End With 

ActiveCell.Select 'assumes you want to activate the last active cell. 

End Sub 

N.B. - 我使用89.25作为长度,因为它是原始代码中的点数差异。根据需要更改。

+0

斯科特 - 感谢这几乎正是我需要的。那里有什么东西在控制着箭的长度吗?看起来没问题,如果我在网格左上方100左右的单元格中,但是当我走下去说S,200时,线条变得很长。 –

+1

@JimO - 玩't + 89.25,l + 89.25,l,t'的参数。它基本上是箭头的开始和结束点,所以你需要做数学计算出如何使它达到你想要的长度。 –

+0

我想你打算使用'ActiveSheet.Shapes.AddConnector(msoConnectorStraight,l - 89.25,t - 89.25,l,t).Select'。你的当前行有t和l切换到开始位置,并且你有箭头指向离开单元格。 (但是我不知道当单元接近文档的顶部或左侧时,OP会发生什么 - “89.25”或“89.55”可能变为负数。) – YowE3K