Solved

Convert Several UsedRanges into Tables

Posted on 2011-02-25
8
285 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
iPhone excel activation issues 11 66
macro for active word document 4 39
Match formula returns N/A 5 25
Consolidate xl 2010 worksheets with text 2 20
Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

914 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now