Solved

Convert Several UsedRanges into Tables

Posted on 2011-02-25
8
288 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

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.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

726 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