How do I select a dynamic range of cells in an Excel work book and use the selection to create a table and/or a named range

Hello Excel Experts!

We have a workbook that pulls data from an application.
The workbook is used to create reports.
I can create a table in the remote workbook and define the names however the count of the contiguous rows of data change each time.
(column count is usually the same)

I need to define the active range of cells when the report is ran.

This is what I have so far....
I need to define the value for "activecells" on line 22.....


Sub CreatetblTTbyDate()

  'Activate the remote sheet
    Windows("MyWorkbook.xlsx").Activate

  'select a sheet in the active workbook
          Sheets("Inventory").Select

  'look for a cell value
          Cells.Find(What:="Table 3", After:=activecell, LookIn:=xlFormulas, _
              LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
              MatchCase:=False, SearchFormat:=False).Activate

   'move to the top left corner of the contiguous data
          activecell.Offset(2, 1).Select

   'select the contiguous range
          range(selection, selection.End(xlToRight)).Select
          range(selection, selection.End(xlDown)).Select

   'Name the active range of cells
      ActiveSheet.ListObjects.Add(xlSrcRange, range(activecells), , xlYes).name = _
        "tblTTbyDate"

    'Create Names in top row
          range("tblTTbyDate[#All]").Select
           selection.createNames Top:=True, Left:=False, Bottom:=False, Right:= _
              False

 End Sub
Allen PrinceTraining DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Roy CoxGroup Finance ManagerCommented:
An example of the data would be useful
Rgonzo1971Commented:
Hi,

pls try

Sub CreatetblTTbyDate()

   'Activate the remote sheet
     Windows("MyWorkbook.xlsx").Activate

   'select a sheet in the active workbook
           Sheets("Inventory").Select

   'look for a cell value
           Set foundCell = Cells.Find(What:="Table 3", After:=ActiveCell, LookIn:=xlFormulas, _
               LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
               MatchCase:=False, SearchFormat:=False)

    'move to the top left corner of the contiguous data
           Set rngOffset = foundCell.Offset(2, 1)

    'select the contiguous range
           Set rngLO = Range(rngOffset, rngOffset.End(xlToRight).End(xlDown))
    'Name the active range of cells
       ActiveSheet.ListObjects.Add(xlSrcRange, rngLO, , xlYes).Name = _
         "tblTTbyDate"

     'Create Names in top row
           Range("tblTTbyDate[#All]").Select
            Selection.CreateNames Top:=True, Left:=False, Bottom:=False, Right:= _
               False

  End Sub

Open in new window

Regards

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Roy CoxGroup Finance ManagerCommented:
I have added some erro handling to the code

Option Explicit
Sub CreatetblTTbyDate()

'declare variables
    Dim MyCell As Range, rTbl As Range
    'check required workbook is open, if not open it

    If Not wbOpen("MyWorkbook.xlsx") Then
        Workbooks.Open "MyWorkbook.xlsx"
    End If
    'Activate the remote sheet
    Workbooks("MyWorkbook.xlsx").Activate

    'use sheet in the active workbook
    With Sheets("Inventory")
        'look for a cell value
        Set MyCell = Cells.Find(What:="Table 3", After:=.Range("A1"), LookIn:=xlFormulas, _
                                LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                                MatchCase:=False, SearchFormat:=False)

        'move to the top left corner of the contiguous data
        If Not MyCell Is Nothing Then
            Set MyCell = MyCell.Offset(2, 1).CurrentRegiom
        Else: MsgBox "No matching cell found", vbCritical, "Quitting"
            Exit Sub
        End If
        'select the contiguous range
        Set rTbl = MyCell.CurrentRegion
        'Name the active range of cells
        .ListObjects.Add(xlSrcRange, rTbl, , xlYes).Name = _
        "tblTTbyDate"

        'Create Names in top row
        .Range("tblTTbyDate[#All]").CreateNames Top:=True, Left:=False, Bottom:=False, Right:= _
                                                False
    End With
End Sub


Function wbOpen(wbName As String) As Boolean
' returns TRUE if the workbook is open
    wbOpen = False
    On Error GoTo wbNotOpen
    If Len(Application.Workbooks(wbName).Name) > 0 Then
        wbOpen = True
        Exit Function
    End If
wbNotOpen:
    MsgBox "The workbook is not available", vbCritical, "Error"
End Function
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Allen PrinceTraining DeveloperAuthor Commented:
Rgonzo1971:

Would these  Dim Statements be correct?

Dim foundCell As Range
Dim rngOffset As Range
Dim rngLO As Range
Allen PrinceTraining DeveloperAuthor Commented:
Roy_Cox:

I tried your error trapped solution first
would not execute
will try again and advise as to reason
Rgonzo1971Commented:
the dims are correct
Roy CoxGroup Finance ManagerCommented:
You can test the code by removing the error traps although the traps should display an alert if there is an error.
Allen PrinceTraining DeveloperAuthor Commented:
The simple solution is best today!
Thanks People!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.