excelvba

reference to table range in vba


I want to import my data from Excel to SQL. I wrote some code - when I used Set rng = Application.Range"F5:H10") it works. However, I want to use a table since the amount of data changes every time. How can I use an Excel table in this part of my code?

Thanks


Solution

  • One way:

    Sub Table2Range()
        Dim rng As Range
        Set rng = ActiveSheet.ListObjects(1).Range
        MsgBox rng.Address(0, 0)
    End Sub
    

    enter image description here

    Note:

    You get the header as well as the data-body.