Solved

Created ranges and name them

Posted on 2011-02-25
2
226 Views
Last Modified: 2012-05-11
Dear Experts:

this nice code, courtesy by rorya, performs the following task ...
(1) looking for the word 'Sales' in the current worksheet,
(2) if found, creates a table with rngFound.CurrentRegion
(3) assigns/applies names and stylesfor these ListObjects
(4) looping

I would like to have a second version of this code ...
... where the created ranges 'just' get named with the following naming convention: range1, range2, range3 and no 'ListObjects' are created.

I hope this is feasible.

Thank you very much in advance. Regards, Andreas

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
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
2 Comments
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 34979837
As I said before:
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
 

Author Closing Comment

by:AndreasHermle
ID: 34980053
Rorya:
great. Thank you very much for your excellent and swift help.

Regards, Andreas
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

740 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