2016-09-07 184 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