Macro to activate cell whose address is referenced elsewhere

℡╲_俬逩灬. 提交于 2019-12-12 02:22:44

问题


I have an array of Months and Years (formatted Jan-16, Mar-17, etc) in a single row. I have a formula that will find the current month (based on the month and year I select in a drop-down menu) and spit out the address of that given month and year:

=CELL("address",INDEX(E3:BL3,MATCH(D1,E3:BL3,0))).

Works like a charm. Now I need to know how I can program a macro to look at the cell that contains the address and activate the cell whose address I am referencing.

So, for example, if the address that it spits out is Z3, the macro will then go and activate cell Z3. It needs to be dynamic because every month I will be referencing a new month and thus a new cell.


回答1:


Assuming that your drop down is linked to cell A1 then you can fire an event on a change on that cell that will move focus to cell address indicated in your formula (I'm assuming here B1). You would place on the Workbook_SheetChange event the following code:

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If Target.Address = "$A$1" Then
        Dim rngAddress As Range
        Set rngAddress = ThisWorkbook.Sheets(1).Range("B1") 'Location of your address formula
        ThisWorkbook.Sheets(1).Range(rngAddress.Value2).Select
    End If
End Sub


来源:https://stackoverflow.com/questions/38085484/macro-to-activate-cell-whose-address-is-referenced-elsewhere

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!