1

I am adding a new row to the table I have in the active sheet using the following method:

ActiveSheet.ListObjects("MyDataTable1").ListRows.Add(AlwaysInsert:=True)

The problem is that it is dependent on the table name "MyDataTable1". If I change the active sheet to the one in which the name of table is "MyDataTable2", I have to change my code too. Is there anyway to find the table in the active sheet automatically so that I do not need to change the code every time I switch between the sheets?

1 Answer 1

0

You could do something like this pseudo code

dim name as string 
name = Screen.ActiveDatasheet.Name

dim dataTableName as string

Select(name)

case "Sheet1"
    dataTableName "MyDataTable1"

end select

ActiveSheet.ListObjects(dataTableName).ListRows.Add(AlwaysInsert:=True)

Or you can loop through all the controls with the following (NOT tested)

dim dataTableName as string
dim myControl As Control


For Each myControl In Me.Controls

    If TypeName(myControl) = "DataTable" Then

        dataTableName = myControl.Name

    End If

 Next myControl 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .