Solved

Convert Several UsedRanges into Tables

Posted on 2011-02-25
8
287 Views
Last Modified: 2012-05-11
Dear Experts:

below code converts a defined range (B2:D8) into a table (using ListObject).
The code works just fine.

Is it possible to expand the macro to automatically convert several ranges into tables?
Naming convention is: Table1, Table2, Table3, etc.

The Ranges are not to be hard-coded but the code should detect these 'used ranges' automatically. Is this possible?

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
Sub CreateTable()
    ActiveSheet.ListObjects.Add(xlSrcRange, Range("$B$2:$D$8"), , xlYes).Name = _
        "Table1"
        'No go in 2003
    ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleLight2"
End Sub

Open in new window

0
Comment
Question by:AndreasHermle
  • 4
  • 3
8 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 34978616
"the code should detect these 'used ranges' automatically. Is this possible?"

Not without more information than that. (otherwise it would be magic, not VBA) :)
0
 
LVL 5

Assisted Solution

by:Kannan K
Kannan K earned 100 total points
ID: 34978813
Hi,

rorya comment is absolutely right, But you can make somehow semi dynamic :)

before calling this function you can get parameter for cell ranges using inputbox and do the table formatting

KK,
Sub testmacro()
Dim inCellRange As String

inCellRange = InputBox("Enter the Cell Range", "Cell Range")
Call CreateTable(inCellRange)

End Sub

Sub CreateTable(getCellRange As String)
    ActiveSheet.ListObjects.Add(xlSrcRange, Range(getCellRange), , xlYes).Name = _
        "Table1"
        'No go in 2003
    ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleLight2"
End Sub

Open in new window

0
 

Author Comment

by:AndreasHermle
ID: 34978979
Dear rorya, dear KK,

ok, I understand.

KK: works great, thank you very much. Nevertheless I would like to give this 'automatic range detection' a new chance.

Could the code be adjusted under the condition that all data lists/ranges to be converted into tables all have the same MAKE-UP, i.e. the first cell of the the tables (cell(1,1)) always have got 'Sales' as value (String).

If I do a search operation for the string 'Sales' and then press Ctrl+Shift+Right Arrow + Down Arrow, the whole table gets selected.

Doing above operation repeatedly would result in all the tables get selected.

I hope you know what I mean.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 85

Accepted Solution

by:
Rory Archibald earned 400 total points
ID: 34979043
Try this:
Sub CreateSalesTables()
   Dim rngFound As Range
   Dim strAddy As String
   Dim lngCounter As Long
   Set rngFound = ActiveSheet.UsedRange.Find(what:="Sales", LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
   If Not rngFound Is Nothing Then
      lngCounter = 1
      strAddy = rngFound.Address
      Do
         CreateTable rngFound.CurrentRegion, "Table" & lngCounter
         lngCounter = lngCounter + 1
         Set rngFound = ActiveSheet.UsedRange.FindNext(rngFound)
      Loop While rngFound.Address <> strAddy
   End If
End Sub

Sub CreateTable(rng As Range, strName As String)
    With rng.Worksheet.ListObjects.Add(xlSrcRange, rng, , xlYes)
      .Name = strName
        'No go in 2003
      .TableStyle = "TableStyleLight2"
   End With

End Sub

Open in new window

0
 

Author Comment

by:AndreasHermle
ID: 34979215
Hi rorya:

this is great coding. Thank you very much for your superb and professional help. It is superfast, works like a charm.

Thank you again. Rorya, I am not quite sure how I should split the points: 75% for you and 25% for KK, I suppose since he also came up with a working solution.

I' ll think about it.

Regards, andreas
0
 

Author Comment

by:AndreasHermle
ID: 34979385
Hi rorya:

there is something else. I will award the points on this question and then put a new one right away similiar to this. The code may have to be adjusted only slightly.

Instead of creating ListObjects the 'rngFound.CurrentRegion' should just receive a name (named range) with the following name convention: range1, range2, range3 etc.

Regards, Andreas
0
 

Author Closing Comment

by:AndreasHermle
ID: 34979399
Again, thank you very much for your superb and swift help. I really appreciate it.

Regards, Andreas
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 34979404
No need - it's a small tweak:
Sub CreateSalesTables()
   Dim rngFound As Range
   Dim strAddy As String
   Dim lngCounter As Long
   Set rngFound = ActiveSheet.UsedRange.Find(what:="Sales", LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
   If Not rngFound Is Nothing Then
      lngCounter = 1
      strAddy = rngFound.Address
      Do
         rngFound.CurrentRegion.Name = "Range" & lngCounter
         lngCounter = lngCounter + 1
         Set rngFound = ActiveSheet.UsedRange.FindNext(rngFound)
      Loop While rngFound.Address <> strAddy
   End If
End Sub

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question