1

I have a sheet that I use an Excel VBA macro to automatically fill all worksheet page headers (NOT column headers) with relevant information on a button click. This macro uses wSheet.Name to populate the header title. However, wSheet.Name often contains leading numbers, periods, and spaces that I don't want to appear in the header.

Note the following sample worksheet names:

Cover Page
1a. Test Page
1b. Sample Page
2. Another Test Page
3. Yet Another Test Page
4. Almost the Last Example Page
998.  Last Example Page

I would like to remove these leading numbers, periods, and spaces using a regular expression, yet I'm unsure how to code it using VBA in Excel. I would like it to be as flexible as possible. Here is an example of how I would like the worksheet names to appear:

Cover Page
Test Page
Sample Page
Another Test Page
Yet Another Test Page
Almost the Last Example Page
Last Example Page

Here is my existing code that populates the headers:

Sub FillHeaders()
'
' Auto_Fill_Project_Name Macro
'
For Each wSheet In ActiveWorkbook.Worksheets
    If wSheet.Name <> "Cover Page" Then
        wSheet.PageSetup.CenterHeader = _
            "&16&KFF0000" & ActiveSheet.Range("J1") & "&10&K000000 &16" & " " & _
            wSheet.Name & Chr(13) & "&10 &11 Revision Date: "
    End If
    Next wSheet
'
End Sub

How can I modify this to accomplish my goal?

Thank you!

EDIT -- I have taken the following approach:

Function remleaddig(str As String)
    Set regEx = CreateObject("VBScript.RegExp")
    regEx.Pattern = "^\S*\."
    str = regEx.Replace(str, "")
    remleaddig = Trim(str)
End Function
Community
  • 1
  • 1
  • 1
    `^\d+\w\.\s` matches `1.b Another Test Title`? Unless that's not a type `^\S*\. ` was still able to match all your examples. Are you sure it's not working? – Brad Nov 05 '12 at 16:42

3 Answers3

2

Update: -- Added UDF

Function StripChars(strIn As String) As String
Dim objRegex As Object
Set objRegex = CreateObject("vbscript.regexp")
 With objRegex
 .Pattern = "^.+\.\s+"
.ignorecase = True
StripChars = .Replace(strIn, vbNullString)
End With
End Function

Initial Post`

I would combine the with a variant array to reduce code runtime. The code below uses

  1. Late bindging to set up the regexp
  2. The values in column A of the ActiveSheet are read into a variable array X (Note that the code will handle a 2D range, ie you could use X = Range([a1], Cells(Rows.Count, "B").End(xlUp)).Value2 to work on column A and B
  3. The regexp strips out the unwanted characters - it will handle more than one space if it is presnt before the text you want to retain
  4. The cleaned text is dumped to the active sheet starting in C1. Change this line to move the dump - you could make it dump back over the orginal values starting in A1. [c1].Resize(UBound(X, 1), UBound(X, 2)).Value2 = X

enter image description here

Code

Sub QuickUpdate()
Dim X()
Dim objRegex As Object
Dim lngRow As Long
Dim lngCol As Long

X = Range([a1], Cells(Rows.Count, "A").End(xlUp)).Value2
Set objRegex = CreateObject("vbscript.regexp")
 With objRegex
 .Pattern = "^.+\.\s+"
.ignorecase = True
For lngRow = 1 To UBound(X, 1)
For lngCol = 1 To UBound(X, 2)
X(lngRow, lngCol) = .Replace(X(lngRow, lngCol), vbNullString)
Next lngCol
Next lngRow
End With

[c1].Resize(UBound(X, 1), UBound(X, 2)).Value2 = X

End Sub
brettdj
  • 54,857
  • 16
  • 114
  • 177
1

Well, you can actually use regular expressions via the RegEx object of VBScript. See https://stackoverflow.com/a/13041710/1756702 for an example.

Community
  • 1
  • 1
A. Webb
  • 26,227
  • 1
  • 63
  • 95
1

I'll elaborate on my comment.

This required a reference to Microsoft VBScript Regular Expressions 5.5 (or whatever the number is on your machine)

Dim r As RegExp
Set r = New RegExp
r.Pattern = "^\S*\. "

Dim c As Range, rng As Range
Set rng = Range("A1:A7")   ' <-- Set this to your range
For Each c In rng
    c.Value = r.Replace(c.Value, "")
Next c

If all that text is in one cell then change it to

Dim r As RegExp
Set r = New RegExp
r.Pattern = "^\S*\. "
r.MultiLine = True
r.Global = True

Dim c As Range
Set c = Range("J1")
    c.Value = r.Replace(c.Value, "") '<--or place the result where ever you want

The regex explained

  • "^ This says that the pattern specified must start at the beginning of the string. we don't want to match numbers that exist in the middle of your header title.
  • \S This matches everything except white space ([^a-zA-Z0-9]). i.e. it will match letters and numbers.
  • * This matches 0 or more of the previous pattern
  • \. This matches a period (period must be escaped with a \ because the . itself means it wants to match 1 or more of the previous pattern.
  • " This matches a single space.
Brad
  • 11,934
  • 4
  • 45
  • 73
  • Thank you Brad. I'm trying to understand this. First, the variable is initialized as a new regular expression with a pattern matching one or more numbers, a decimal point, then a space? Next, a new range is initialized and given a range of cells. Then this code loops through each cell in the range and replaces the value with a new value. What cells are we trying to replace though, as I am trying to fill a page header, not just a column header? I'm confused. I assume I could use this regex to create a new substring based off of wSheet.Name? –  Nov 05 '12 at 15:59
  • Does all of that live in the cell `J1`? and you are just grabbing it from there to put in the header? – Brad Nov 05 '12 at 16:14
  • I apologize, that J1 cell was irrelevant to my question and I phrased the question vaguely. Please see the edit I made to my original post. I almost have it working, the regex pattern needs some modification, though. –  Nov 05 '12 at 16:37