2016-07-28 45 views
0

VBA非常新,通常使用Matlab和Python。我有以下代碼段如何在鏈接的單元格引用中使用變量?

Sub Example 
    Dim num As Integer 

    num = 62 
    ActiveSheet.CheckBoxes.Add(23.25, 595.5, 101.25, 18.75).Select 

    With Selection 
     .Name = "NewCheckBox" 
     Sheets("IPT data").Select 
     .Caption = Cells(num, "C") 
    End With 

    Sheets("IPT Chart").Select 
    ActiveSheet.CheckBoxes("NewCheckBox").Select 

    With Selection 
     .Value = xlOff 
     .LinkedCell = "'IPT data'!$A$num" 
     .Display3DShading = False 
    End With 
End Sub 

我想鏈接的單元格引用Anum。有沒有辦法做到這一點?後面的num將用於循環,所以我不能單獨使用它。

就像我說的,很新,這可能是基本的東西,所以我apoligse。我有一個搜索,並試圖使用單元格和範圍無濟於事。

感謝

+2

這有什麼做用Excel對象模型。當然,如果你使用Python你知道字符串連接? '=「'IPT數據'!$ A $」&#'。另請參閱[如何避免在Excel VBA宏中使用選擇](http://stackoverflow.com/q/10714251/11683)。 – GSerg

回答

0

使用

.LinkedCell = "'IPT data'!$A$" & num 

,但你能避免所有的選擇和寫:

Option Explicit 
Sub Example2() 
    Dim num As Integer 

    num = 62 
    With Worksheets("IPT Chart") 
     With .CheckBoxes.Add(23.25, 595.5, 101.25, 18.75) 
      .Name = "NewCheckBox" 
      .Caption = Worksheets("IPT data").Cells(num, "C") 
      .Value = xlOff 
      .LinkedCell = "'IPT data'!$A$" & num 
      .Display3DShading = False 
     End With 
    End With 
End Sub 
+0

@ user6649163你通過它了嗎? – user3598756

+0

@ user6649163:你會很高興給予幫助你的人的反饋 – user3598756

+0

非常有幫助,只是我需要它。非常感謝 – user6649163

相關問題