Help creating a macro to name ranges

ba8910

New Member
Joined
Oct 14, 2011
Messages
1
I need a macro to name the ranges, name should be the table heading.

Here is the example

<table class="cms_table" width="192"><tbody><tr class="cms_table_tr" valign="top"><td class="cms_table_td" width="64">Table 1</td> <td class="cms_table_td" width="64">
</td> <td class="cms_table_td" width="64">
</td> </tr> <tr class="cms_table_tr" valign="top"><td class="cms_table_td">Name</td> <td class="cms_table_xl65">Age</td> <td class="cms_table_xl65">Sales</td> </tr> <tr class="cms_table_tr" valign="top"><td class="cms_table_td">Prakash</td> <td class="cms_table_xl65">25</td> <td class="cms_table_xl65">20000</td> </tr> <tr class="cms_table_tr" valign="top"><td class="cms_table_td">Rani</td> <td class="cms_table_xl65">30</td> <td class="cms_table_xl65">30000</td> </tr> <tr class="cms_table_tr" valign="top"><td class="cms_table_td">
</td> <td class="cms_table_xl65">
</td> <td class="cms_table_xl65">
</td> </tr> <tr class="cms_table_tr" valign="top"><td class="cms_table_td">Table 2</td> <td class="cms_table_xl65">
</td> <td class="cms_table_xl65">
</td> </tr> <tr class="cms_table_tr" valign="top"><td class="cms_table_td">Name</td> <td class="cms_table_xl65">Age</td> <td class="cms_table_xl65">Sales</td> </tr> <tr class="cms_table_tr" valign="top"><td class="cms_table_td">Prakash</td> <td class="cms_table_xl65">25</td> <td class="cms_table_xl65">20000</td> </tr> <tr class="cms_table_tr" valign="top"><td class="cms_table_td">Rani</td> <td class="cms_table_xl65">30</td> <td class="cms_table_xl65">30000</td> </tr> <tr class="cms_table_tr" valign="top"><td class="cms_table_td">Prakash</td> <td class="cms_table_xl65">25</td> <td class="cms_table_xl65">20000</td> </tr> <tr class="cms_table_tr" valign="top"><td class="cms_table_td">Rani</td> <td class="cms_table_xl65">30</td> <td class="cms_table_xl65">30000</td> </tr> <tr class="cms_table_tr" valign="top"><td class="cms_table_td">Rani</td> <td class="cms_table_xl65">30</td> <td class="cms_table_xl65">30000</td> </tr> <tr class="cms_table_tr" valign="top"><td class="cms_table_td">Prakash</td> <td class="cms_table_xl65">25</td> <td class="cms_table_xl65">20000</td> </tr> <tr class="cms_table_tr" valign="top"><td class="cms_table_td">Rani</td> <td class="cms_table_xl65">30</td> <td class="cms_table_xl65">30000</td> </tr> <tr class="cms_table_tr" valign="top"><td class="cms_table_td">
</td> <td class="cms_table_xl65">
</td> <td class="cms_table_xl65">
</td> </tr> <tr class="cms_table_tr" valign="top"><td class="cms_table_td">Table 3</td> <td class="cms_table_xl65">
</td> <td class="cms_table_xl65">
</td> </tr> <tr class="cms_table_tr" valign="top"><td class="cms_table_td">Name</td> <td class="cms_table_xl65">Age</td> <td class="cms_table_xl65">Sales</td> </tr> <tr class="cms_table_tr" valign="top"><td class="cms_table_td">Prakash</td> <td class="cms_table_xl65">25</td> <td class="cms_table_xl65">20000</td> </tr> <tr class="cms_table_tr" valign="top"><td class="cms_table_td">Rani</td> <td class="cms_table_xl65">30</td> <td class="cms_table_xl65">30000</td> </tr> <tr class="cms_table_tr" valign="top"><td class="cms_table_td">Prakash</td> <td class="cms_table_xl65">25</td> <td class="cms_table_xl65">20000</td> </tr> </tbody></table>



1st name of the range should be "Table1" next "Table2" and so on... it is possible that there are many more columns.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Code:
Sub naming_ranges()

    Dim rng As Range

    For Each rng In ActiveSheet.UsedRange.Columns(2).SpecialCells(2, 2).Areas
        With rng.CurrentRegion
            .Offset(1, 1).Resize(.Rows.Count - 1, .Columns.Count - 1).Name = Replace(.Cells(1).Text, " ", "")
        End With
    Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,081
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top