-1

This is my sample data.

enter image description here

And this is the VBA code produced with macro.

Sub Macro1()
'
' Macro1 Macro
'

'
    Range("A1:C3").Select
    Application.CutCopyMode = False
    ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$C$3"), , xlYes).Name = _
        "Table1"
    Range("Table1[#All]").Select
    ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleMedium9"
End Sub

How do I get the macro code?

  1. Developer > Record Macro
  2. Select all (ctrl + A) inside any cells within A1:C3
  3. Home > Format as Table

Output

enter image description here

The problem is my data is dynamic and not necessarily stay at A1:C3.

It could be bigger or smaller.

E.g., let say I've bigger within range A1:C4 in different Ms Excel file.

enter image description here

The code above won't select all, instead it selects only A1:C3.

enter image description here

Last row A4:C4 won't be affected with this code.

How do I change this doesn't matter how big the table is?

3 Answers3

4

Solution 1

You can use Range("A1").CurrentRegion to get the area of continous data (which is the same as Ctrl + A):

Option Explicit

Public Sub SelectCurrentRegion()
    Dim MyData As Range
    Set MyData = Worksheets("Sheet1").Range("A1").CurrentRegion
    
    'don't use .select this is just for illustrating
    MyData.Select
End Sub

enter image description here

Note that the number in cell D6 is not vertically nor horizontally connected with the other data. Therefore it is not selected by Ctrl + A or .CurrentRegion.

Solution 2

Or Worksheets("Sheet1").UsedRange to get the area of all data:

Option Explicit

Public Sub SelectCurrentRegion()
    Dim MyData As Range
    Set MyData = Worksheets("Sheet1").UsedRange
    
    'don't use .select this is just for illustrating
    MyData.Select
End Sub

enter image description here

You might benefit from reading How to avoid using Select in Excel VBA.

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
  • Thanks @Pᴇʜ. I'm new to macro & VBA and have no idea how to implement your code in mine. Could you please put a full code to produce the table as shown in the screenshot above? –  Sep 07 '20 at 09:54
  • 1
    ++ nicely explained – Siddharth Rout Sep 07 '20 at 10:09
  • I copy paste the code and getting the following error: `Run-time error '1004': Select method of Range class failed ` –  Sep 07 '20 at 10:23
  • @user11392987 make sure you reference the correct worksheet (adjust the name in the code) and add your table like following using the definde `MyData` range: `Worksheets("Sheet1").ListObjects.Add(xlSrcRange, MyData, , xlYes).Name = "Table1"` – Pᴇʜ Sep 07 '20 at 11:07
1

@Pᴇʜ has already given you 2 solutions. Here is Solution 3. I would prefer finding last row and column over UsedRange and then construct the range. I have explained here why I do not prefer using UsedRange

Solution 3

Find the last row and last column and then create your range

Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim LastRow As Long
    Dim LastColumn As Long
    Dim rng As Range
    
    '~~> Change this to relevant sheet
    Set ws = Sheet1
    
    With ws
        LastRow = .Cells.Find(What:="*", _
        After:=.Range("A1"), _
        Lookat:=xlPart, _
        LookIn:=xlFormulas, _
        SearchOrder:=xlByRows, _
        SearchDirection:=xlPrevious, _
        MatchCase:=False).Row

        LastColumn = .Cells.Find(What:="*", _
        After:=.Range("A1"), _
        Lookat:=xlPart, _
        LookIn:=xlFormulas, _
        SearchOrder:=xlByColumns, _
        SearchDirection:=xlPrevious, _
        MatchCase:=False).Column
        
        Set rng = .Range(.Cells(1, 1), .Cells(LastRow, LastColumn))
        
        Debug.Print rng.Address
    End With
End Sub
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • Thanks @Siddharth Rout, I put this code on `Module1` and execute it but nothing happen –  Sep 07 '20 at 10:26
  • 1
    I think you are mistaken how this code works... @Pᴇʜ and my code tells you how to get your range and not create a table. You already have the code in your question to create the table. You need to combine both the codes to achieve what you want. I can give you the code in a platter but I would like you to try it first. – Siddharth Rout Sep 07 '20 at 10:29
0

If you are using Excel tables then you can use DataBodyRange or Range Properties to select what you need to select.

Dim lstObj As ListObject
For Each lstObj In ActiveSheet.ListObjects
    lstObj.DataBodyRange.Select ' Will select only data without headers
    lstObj.Range.Select ' Will select complete table
Next lstObj
shrivallabha.redij
  • 5,832
  • 1
  • 12
  • 27
  • 1
    I think his idea was (according to his code) to create an Excel table (ListObject) out of a normal worksheet's data. – Pᴇʜ Sep 07 '20 at 09:12
  • 1
    @Pᴇʜ Oh, got confused with OP's post. I will keep it for now and see what OP says about it. – shrivallabha.redij Sep 07 '20 at 09:18
  • Thanks @Pᴇʜ & shrivallabha.redij. I wanted to create an excel table (my question has been updated). I'm new to macro & VBA and have no idea how to implement your code in mine. Could you please put a full code to produce the table as shown in the screenshot above? –  Sep 07 '20 at 09:54