2017-05-25 64 views
0

我有一個VBA腳本,我使用調用單元格進行一些處理。當我只將它粘貼到一個單元格中時它工作的很好,但如果我粘貼到多個單元格中,ActiveCell始終是該範圍中的第一個選定單元格,因此所有單元格都會得到相同的值。Excel VBA行調用多單元格粘貼的單元格(Not ActiveCell.Row)

如何獲取函數被調用的單元格的行?

例VBA - (在一個小區做= TestCallCell(),然後複製並粘貼到多個單元格)

 Function TestCallCell() As String 
      curRow = ActiveCell.Row 
      TestCallCell = curRow 
     End Function 

回答

2

你不想使用ActiveCell在UDF,因爲它會爲活動單元格更改變化。使用Application.Caller。它將使用從中調用函數的單元而不是活動單元。

Function TestCallCell() As String 
    curRow = Application.Caller.Row 
    TestCallCell = curRow 
End Function 

enter image description here

+0

完美的感謝! – runfastman

相關問題