473,395 Members | 2,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

VBA to Replace "-" to "_" in excel sheet name

I am trying the use VBA to create a named range of each Sheet Name. The problem is the Sheet Names contain a hyphen and I guess that can't be used in a named range.

How can I use VBA to rename or replace the existing portion of the sheet name that contains "-" to "_"
May 13 '11 #1

✓ answered by Stewart Ross

Hi. There is no problem with using a hyphen in a worksheet name. Although it is not an allowable character in a named range within a workbook, this is where you have named a block within a worksheet (for example, by giving a name to a table used for reference purposes). This is not the same as referring to a range within a worksheet.

For example, if you had two worksheets in a workbook, one named ABC-123 and the other ABC-456, you could place a link to cell A2 of worksheet ABC-123 into worksheet ABC-456 just by using the following formula:

Expand|Select|Wrap|Line Numbers
  1. ='ABC-123'!A2
Anyhow, if you need to replace occurrences of a substring in a text entry the VBA Replace function will do the job for you:

Expand|Select|Wrap|Line Numbers
  1. <new text> = Replace(<old text>, <what you want to replace>, <what you replace it with>)
  2.  
  3. Dim lngCount as Long
  4. For lngCount = 1 to Worksheets.Count
  5. With Worksheets(lngcount)
  6.   .Name = Replace(.Name, "-", "_")
  7. End With 
  8.  
-Stewart

3 13536
Try something like this replacing sheet name with variable that gives the sheet name:

Expand|Select|Wrap|Line Numbers
  1. dim l as integer
  2. dim x as integer
  3. dim a as string
  4.  
  5. for sheet = sheet 1 to sheet x
  6. a = sheet name
  7. l = len(a)
  8. x = instr(1,a,"-")
  9. if x > 0 then
  10. right((left(a,x),1) = "_"
  11. end if
  12. next sheet
May 13 '11 #2
Stewart Ross
2,545 Expert Mod 2GB
Hi. There is no problem with using a hyphen in a worksheet name. Although it is not an allowable character in a named range within a workbook, this is where you have named a block within a worksheet (for example, by giving a name to a table used for reference purposes). This is not the same as referring to a range within a worksheet.

For example, if you had two worksheets in a workbook, one named ABC-123 and the other ABC-456, you could place a link to cell A2 of worksheet ABC-123 into worksheet ABC-456 just by using the following formula:

Expand|Select|Wrap|Line Numbers
  1. ='ABC-123'!A2
Anyhow, if you need to replace occurrences of a substring in a text entry the VBA Replace function will do the job for you:

Expand|Select|Wrap|Line Numbers
  1. <new text> = Replace(<old text>, <what you want to replace>, <what you replace it with>)
  2.  
  3. Dim lngCount as Long
  4. For lngCount = 1 to Worksheets.Count
  5. With Worksheets(lngcount)
  6.   .Name = Replace(.Name, "-", "_")
  7. End With 
  8.  
-Stewart
May 13 '11 #3
Thanks so much.......This does the trick
May 13 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: daniroy | last post by:
Hello everybody, your help will be very very welcome on a very classic question... I have some Excell Macro background, a little bit of VBA knowledge. I import daily from Internet series of...
0
by: ChadDiesel | last post by:
Hello Everyone, I have a field set up in a table that is an OLE object with a link to an external Excel spreadsheet. My question is whether you can use a file path that contains wildcards. ...
3
by: | last post by:
I wrote a class in VB.NET to export the contents of a datagrid to Excel. It works perfectly on my machine, but it fails on my customers' PCs that have identical versions of Win XP (SP1) and Excel...
1
by: Boomessh | last post by:
Hi, I am trying to open an existing excel workbook. It pops up an error stating that “Errors were detected in 1598.xls but Microsoft Office Excel was able to open the file by making the repairs...
0
by: KK | last post by:
Hi, I am using Response object in vb.net to download an excel sheet with data. It worked well and I could download excel file with data. But suddenly It started downloading only blank excel...
5
by: Simon | last post by:
Dear reader, With the export command you can export a query to Excel. By activate this command a form pop's up with the following text:
1
by: sunil kamboj | last post by:
i have a code to use the excel sheet .one excel sheet is already there ,i just replace there name by new excel sheet ,but it is not running ,just give asolution of this ,....and i wana to use...
1
by: gpgp | last post by:
I have a excel macro like this: 1. Range("C2").Select Range(Selection, Selection.End(xlDown)).Select Selection.Replace What:=".", Replacement:="/", LookAt:=xlPart, _ ...
2
hemantbasva
by: hemantbasva | last post by:
Note We need to have a template on server for generating report in multiple sheet as we do not had msoffice on server moreover this require a batch job to delete excel file created by the...
1
by: tarekz | last post by:
I have a table on an SQL server 2005, I would like to change the value of one field to another based on an excel sheet value. In other words, if the value in the SQL table matches the value in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.