Solved

Apply a user-defined table style to named ranges in one go

Posted on 2012-03-28
6
291 Views
Last Modified: 2012-09-18
Dear Experts:

I got a whole bunch of named ranges where I need to apply a user-defined table style in one go.

The ranges to which the user-defined table style has to be applied are named range_01, range_02, range_03 and so forth.

So, how can I apply this user-defined table style in one go to all these named ranges (workbook level scope names) ?

Thank you very much in advance.

I have attached a sample file with sample ranges / data tables along with user-defined table styles for your convenience.

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

Regards, Andreas

Regards, Andreas
ApplyTableStyle.xlsx
0
Comment
Question by:AndreasHermle
  • 3
  • 3
6 Comments
 
LVL 12

Expert Comment

by:kgerb
Comment Utility
We can probably do this at the same time we are searching and replacing :-)

Kyle
0
 
LVL 12

Accepted Solution

by:
kgerb earned 500 total points
Comment Utility
Try this.  I think it will apply the formatting like you want.
Sub StyleAllNamedRanges()
Dim n As Name, sht As String
For Each n In ThisWorkbook.Names
    sht = Replace(Mid(n, 2, InStr(1, n, "!") - 2), "'", "")
    Sheets(sht).ListObjects.Add(xlSrcRange, Range(n), , xlYes).TableStyle = "styl_red_grey"
Next n
End Sub

Open in new window

Kyle

Correction:
Since your tables have headers I modified the final parameter to be xlYes instead of xlNo.
0
 

Author Comment

by:AndreasHermle
Comment Utility
Hi Kyle,

this seems to work just fine. I'll do some more testing and then let you know.

Thank you very much for your swift and professional support.

Regards, Andreas
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:AndreasHermle
Comment Utility
Hi Kyle,

I slightly changed/tweaked the code so that only specific named ranges are taken care of

Sub StyleAllNamedRanges()
Dim n As Name, sht As String
For Each n In ThisWorkbook.Names

If InStr(n.Name, "range_") > 0 Then
    sht = Replace(Mid(n, 2, InStr(1, n, "!") - 2), "'", "")
    Sheets(sht).ListObjects.Add(xlSrcRange, Range(n), , xlYes).TableStyle = "styl_red_grey"
End If
Next n
End Sub
0
 

Author Closing Comment

by:AndreasHermle
Comment Utility
Concise, sophisticated coding.

Thank you very much for your professional help.

Regards, Andreas
0
 
LVL 12

Expert Comment

by:kgerb
Comment Utility
You're welcome.  Glad to help.
Kyle
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
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 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 demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

762 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

10 Experts available now in Live!

Get 1:1 Help Now