object array or collection in VBA Excel

前端 未结 2 671

i would like to have an array of objects in excel that call one event handler. specifically I have multiple buttons which perform the same function to different cells, and

2条回答
  •  [愿得一人]
    2020-12-16 02:26

    Separate the common code into a single method and pass the cell as the parameter. Assign each button it's own event method, which in turn calls the common method with the specific cell to edit as a parameter. Something like this:

    Private Sub CommonMethod(someCell as String)
      ' Do your stuff
      Range(someCell).Value = something
    End Sub
    

    So each button could be assigned to it's own method. This is already built in so don't try to recreate it, keep it simple.

    Private Sub Button1_Click()
      CommonMethod("A1");
    End Sub
    
    Private Sub Button2_Click()
      CommonMethod("A2");
    End Sub
    

提交回复
热议问题