Update Macro to auto capture cell range and table name

%3CLINGO-SUB%20id%3D%22lingo-sub-2780461%22%20slang%3D%22en-US%22%3EUpdate%20Macro%20to%20auto%20capture%20cell%20range%20and%20table%20name%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2780461%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20everyone%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENewbie%20here.%20(Office%20365%2C%20Windows%2010%20user)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20run%20a%20lot%20of%20report%20on%20Excel%20files%2C%20and%20want%20to%20automatically%20create%20and%20format%20the%20cell%20range%20as%20a%20table%20and%20apply%20a%20table%20style.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHowever%20the%20macro%20I%20record%20and%20want%20to%20use%20for%20this%20limits%20the%20cell%20range%20to%20the%20spreadsheet%20cell%20range%20for%20which%20I%20record%20it.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20cell%20range%20will%20vary%20for%20different%20Excel%20spreadsheets.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20other%20thing%20it%20does%20is%20include%20the%20'Table2'%20name%20(as%20it%20was%20the%20second%20document%20I%20had%20open).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIs%20there%20an%20edit%20I%20can%20make%20to%20the%20recorded%20Macro%20to%20automatically%20capture%20the%20cell%20range%20and%20name%20of%20table%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20so%20much.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-vbnet%22%3E%3CCODE%3ESub%20Report1_Weekly()%0A'%0A'%20Weekly%20Report%0A'%0A%0A'%0A%20%20%20%20Range(%22A1%22).Select%0A%20%20%20%20Application.CutCopyMode%20%3D%20False%0A%20%20%20%20ActiveSheet.ListObjects.Add(xlSrcRange%2C%20Range(%22%24A%241%3A%24F%246%22)%2C%20%2C%20xlYes).Name%20%3D%20_%0A%20%20%20%20%20%20%20%20%22Table2%22%0A%20%20%20%20Range(%22Table2%5B%23All%5D%22).Select%0A%20%20%20%20ActiveSheet.ListObjects(%22Table2%22).TableStyle%20%3D%20%22TableStyleLight8%22%0A%20%20%20%20Selection.Rows.AutoFit%0A%20%20%20%20Selection.Columns.AutoFit%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2780461%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2780500%22%20slang%3D%22en-US%22%3ERe%3A%20Update%20Macro%20to%20auto%20capture%20cell%20range%20and%20table%20name%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2780500%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20broke%20rule%20number%20one%20in%20forums%3A%20always%20search%20first...%20Think%20I%20found%20my%20answer.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20the%20Macro%20I%20changed%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-visual-basic%22%3E%3CCODE%3ERange(%22%24A%241%3A%24F%246%22)%2C%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eto%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3ERange(%22A1%22).CurrentRegion%2C%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2785928%22%20slang%3D%22en-US%22%3ERe%3A%20Update%20Macro%20to%20auto%20capture%20cell%20range%20and%20table%20name%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2785928%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F668718%22%20target%3D%22_blank%22%3E%40redhandler%3C%2FA%3E%26nbsp%3BSometimes%20I%20need%20the%20name%20and%20range%20in%20the%20workbook.%3C%2FP%3E%3CP%3EAddressing%20the%20table%20and%20then%20parse%20the%20formula%20is%20one%20way%20to%20do%20it.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22bosinander_0-1632652236583.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F312948i37BE80642EE452BD%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22bosinander_0-1632652236583.png%22%20alt%3D%22bosinander_0-1632652236583.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3EG4%20%3DTable1%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EF4%26nbsp%3B%3DMID(FORMULATEXT(G4)%3B2%3B4711)%3C%2FP%3E%3CP%3Ewhere%204711%20just%20is%20high%20enough%20to%20get%20all%20the%20characters.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EF2%20parses%20the%20cell%20references%3B%3CBR%20%2F%3E%3DLET(table%3BTable1%3B%3CBR%20%2F%3EfirstCell%3BINDEX(table%3B1%3B1)%3B%3CBR%20%2F%3Elastcell%3B(INDEX(table%3BROWS(table)%3BCOLUMNS(table)))%3B%3CBR%20%2F%3EfirstCellAddress%3BADDRESS(ROW(firstCell)%3BCOLUMN(firstCell))%3B%3CBR%20%2F%3ElastCellAddress%3BADDRESS(ROW(lastcell)%3BCOLUMN(lastcell))%3B%3CBR%20%2F%3Eoutput%3BfirstCellAddress%20%26amp%3B%20%22%3A%22%20%26amp%3B%20lastCellAddress%3B%3CBR%20%2F%3Eoutput%3CBR%20%2F%3E)%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CP%3E.CurrentRegion%20is%20just%20fine%20and%20this%20gives%20similar%20possibilities%20without%20macros%2C%20eg%20if%20the%20table%20is%20moved%20from%20A1.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2785938%22%20slang%3D%22en-US%22%3ERe%3A%20Update%20Macro%20to%20auto%20capture%20cell%20range%20and%20table%20name%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2785938%22%20slang%3D%22en-US%22%3E%3CP%3EConsidering%20you%20example%3A%3CBR%20%2F%3ETo%20get%20the%20range%20you%20type%3A%3C%2FP%3E%3CP%3E%3DADDRESS(%40ROW(Table1)%2C%40COLUMN(Table1)%2C4)%26amp%3B%22%3A%22%26amp%3BADDRESS(%40ROW(Table1)%2BROWS(Table1)-1%2C%40COLUMN(Table1)%2BCOLUMNS(Table1)-1%2C4)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ELET()%20function%20is%20not%20available%20to%20all%20excel%20users%20thats%20why%20I%20developed%20another%20way%20to%20get%20the%20range.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETo%20get%20the%20table%20name%3A%3CBR%20%2F%3E%3CBR%20%2F%3EOn%20G6%20Type%20%3DTable1%5B%5B%23Headers%5D%2C%5BText%5D%5D%3CBR%20%2F%3EOn%20F6%20Type%3DMID(FORMULATEXT(G6)%2C2%3BFIND(%22%5B%22%2CFORMULATEXT(G6))-2)%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

Hi everyone,

 

Newbie here. (Office 365, Windows 10 user)

 

I run a lot of report on Excel files, and want to automatically create and format the cell range as a table and apply a table style.

 

However the macro I record and want to use for this limits the cell range to the spreadsheet cell range for which I record it.

 

This cell range will vary for different Excel spreadsheets. 

 

The other thing it does is include the 'Table2' name (as it was the second document I had open).

 

Is there an edit I can make to the recorded Macro to automatically capture the cell range and name of table?

 

Thanks so much.

 

 

 

Sub Report1_Weekly()
'
' Weekly Report
'

'
    Range("A1").Select
    Application.CutCopyMode = False
    ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$F$6"), , xlYes).Name = _
        "Table2"
    Range("Table2[#All]").Select
    ActiveSheet.ListObjects("Table2").TableStyle = "TableStyleLight8"
    Selection.Rows.AutoFit
    Selection.Columns.AutoFit
End Sub

 

 

 

7 Replies

 

 

I broke rule number one in forums: always search first... Think I found my answer.

 

In the Macro I changed:

 

 

 

Range("$A$1:$F$6"),

 

 

 

 

to:

 

 

 

Range("A1").CurrentRegion,

 

 

 

 

@redhandler Sometimes I need the name and range in the workbook.

Addressing the table and then parse the formula is one way to do it.

bosinander_0-1632652236583.png

G4 =Table1

 

F4 =MID(FORMULATEXT(G4);2;4711)

where 4711 just is high enough to get all the characters.

 

F2 parses the cell references;
=LET(table;Table1;
firstCell;INDEX(table;1;1);
lastcell;(INDEX(table;ROWS(table);COLUMNS(table)));
firstCellAddress;ADDRESS(ROW(firstCell);COLUMN(firstCell));
lastCellAddress;ADDRESS(ROW(lastcell);COLUMN(lastcell));
output;firstCellAddress & ":" & lastCellAddress;
output
)

.CurrentRegion is just fine and this gives similar possibilities without macros, eg if the table is moved from A1.

Considering you example:
To get the range you type:

=ADDRESS(@ROW(Table1),@COLUMN(Table1),4)&":"&ADDRESS(@ROW(Table1)+ROWS(Table1)-1,@COLUMN(Table1)+COLUMNS(Table1)-1,4)

 

LET() function is not available to all excel users thats why I developed another way to get the range.

 

To get the table name:

On G6 Type =Table1[[#Headers],[Text]]
On F6 Type=MID(FORMULATEXT(G6),2;FIND("[",FORMULATEXT(G6))-2)

@bosinander 

In case you perfer an UDF and get all possible desired outputs.

 

 

 

Function GetTableInfo(AnyCell As Range, InfoType As Integer) As Variant
    Dim TableOutput As String

    TableOutput = vbNullString    
    On Error Resume Next
    
    Select Case InfoType    
    Case 1 ' Table Name
        TableOutput = AnyCell.ListObject.Name
    Case 2 ' Table Range
        TableOutput = AnyCell.ListObject.DataBodyRange.Address
    Case 3 ' Number of Rows
        TableOutput = AnyCell.ListObject.DataBodyRange.Rows.Count
    Case 4 ' Number of Columns
        TableOutput = AnyCell.ListObject.DataBodyRange.Columns.Count
    Case Else
        TableOutput = "Undefined"
    End Select    
    GetTableInfo = TableOutput
End Function

 

 

 

 

JulianoPetrukio_0-1632655768709.png

 

 

One way to use LET is to make long formulas easier to read. Positive for both the builder in the future and for others using the model. Sep 2021 though, only available in 365.

 

Using =Table1 is like using =Table1[#Data] and when still easy to read, I prefer short versions.

 

@Juliano-Petrukio's approach with finding special characters holds more possibilities and is among others a way to get the name of linked files.

 

Two cells linking to different files
='[Open Workbook.xlsx]Sheet1'!$A$1

='C:\Users\bosinander\[Closed Workbook.xlsx]Sheet1'!$A$1

 

=LET(

string;FORMULATEXT(A1);
first;FIND("'";string);
last;FIND("]";string);
output;MID(string;first+1;last-first-1);
SUBSTITUTE(output;"[";"")
)

 

For version not having the LET function, it is the same as

=SUBSTITUTE(MID(FORMULATEXT(A1);FIND("'";FORMULATEXT(A1))+1;FIND("]";FORMULATEXT(A1))-FIND("'";FORMULATEXT(A1))-1);"[";"")

 

Output;

Open Workbook.xlsx
C:\Users\bosinander\Closed Workbook.xlsx

 

I see in your code that you too prefer clean and easy to read coding
When user defined functions are not necessary I prefer to keep the solutions inside the worksheets frames and thus often make it easier for the next user not needing to familiar with macros.

 

@redhandler you

Review what is more applicable considering your environment. New formulas, "global" formulas or UDF (Using VBA Code)

 

@bosinander 

Macro is the last resource to be used. Specially now that excel has tons of different features.

Yes, code must be easy to understand. Thanks