Solved

Excel VBA Macro - Find and Replace all based on RegEx

Posted on 2015-02-09
13
296 Views
Last Modified: 2016-02-10
I need to update ALL prices within a PDF based on a percentage.  To start though I will obviously need to be able to successfully find ALL prices first.  Prices are in the format such as:

5.55
12.35
125.55
etc.

I found the below VBA code courtesy from www.TheSpreadsheetGuru.com however, I do not know how to modify it to search ALL worksheets and to search for a pattern/regex.

Sub FindReplaceAll()

'PURPOSE: Find & Replace text/values throughout entire workbook
'SOURCE: www.TheSpreadsheetGuru.com

Dim sht As Worksheet
Dim fnd As Variant
Dim rplc As Variant

fnd = "April"
rplc = "May"

For Each sht In ActiveWorkbook.Worksheets
  sht.Cells.Replace what:=fnd, Replacement:=rplc, _
    LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
    SearchFormat:=False, ReplaceFormat:=False
Next sht

End Sub

Open in new window

0
Comment
Question by:LouSch7
  • 7
  • 6
13 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 40598887
1. Is the data in a workbook or PDF?
2. What does the regular expression have to do with the problem (given the code you posted)?
3. Do you need to limit your changes to cell text or cell value, which may include the result of a formula?
0
 
LVL 3

Author Comment

by:LouSch7
ID: 40601211
Aikimark,

1. The data is in a Workbook

2. Regex is the best way I know how to perform pattern matching; the code posted is simply a stepping stone of how to perform a find and replace within a given worksheet.  I need to expand on that concept and perform a find and replace based on a pattern for the find and a formula for the replace; against the entire workbook.

3. Changes would be limited to cell text, there are no formulas within the workbook at all.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40601543
so, let's talk about what you perceive to be the pattern of prices.  I know you've given three examples of prices, but I want you to describe what would separate a price numeric value from a non-price numeric value.
0
 
LVL 3

Author Comment

by:LouSch7
ID: 40601630
Akimark,

Solid question.  A price is defined as any number of numeric values, followed by a decimal and two trailing numeric values.  There are no commas and no dollar signs.  There are no other numeric values that match this pattern.

examples:

#####.##
###.##
#.##
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40602144
Will these numeric values be the only contents of the cell or might they be part of a text string in a cell?
0
 
LVL 3

Author Comment

by:LouSch7
ID: 40602204
The cell has no additional text, only the price value.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 45

Expert Comment

by:aikimark
ID: 40602219
You can do pattern matching without regular expressions:
Sub Q_28613166()
    Dim rngCell As Range
    Dim rngNumbers As Range
    Dim wks As Worksheet
    For Each wks In ActiveWorkbook.Sheets
        Set rngNumbers = wks.Cells.SpecialCells(xlCellTypeConstants, xlNumbers)
        For Each rngCell In rngNumbers
            If rngCell.Value Like "*#.##" Then
                Debug.Print rngCell.Address, rngCell.Value
            End If
        Next
    Next
End Sub

Open in new window

0
 
LVL 3

Author Comment

by:LouSch7
ID: 40602369
Aikmark,

That works beautifully however, apparently some of the "numbers" are actually formatted as text... didn't realize this since it is an 80 tab workbook; your script worked wonders however, it cannot account for values that are not numbers.  Assuming this has to do with the SpecialCells and xlNumbers portion of your script?

Is there a way to look for the pattern within all cells (text as well), the convert (val(rngCell.value) the cell to a number prior to updating it?
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40602403
please post a workbook with representative data
0
 
LVL 3

Author Comment

by:LouSch7
ID: 40602911
Attached is an example with the current macro/vb we are working on.
Update-Price-Excel-Sample.xlsm
0
 
LVL 45

Accepted Solution

by:
aikimark earned 500 total points
ID: 40603019
try this:
Sub Q_28613166()
    Dim rngCell As Range
    Dim rngNumbers As Range
    Dim wks As Worksheet
    Application.ScreenUpdating = False
    For Each wks In ActiveWorkbook.Sheets
        Set rngNumbers = wks.Cells.SpecialCells(xlCellTypeConstants, xlNumbers + xlTextValues)
        For Each rngCell In rngNumbers
            If IsNumeric(rngCell.Value) Then
                If rngCell.Value Like "*#.##" Then
                     rngCell.Value = Format(rngCell.Value * 1.1, "0.00")
                End If
            End If
        Next
    Next
    Application.ScreenUpdating = True
End Sub

Open in new window

0
 
LVL 3

Author Comment

by:LouSch7
ID: 40603463
Aikmark,

That works wonderfully.

Here is the final code I put together

Sub Q_28613166()
    Dim rngCell As Range
    Dim rngNumbers As Range
    Dim wks As Worksheet
    
    Dim perUpdate As Integer
    
    perUpdate = InputBox("What percentage would you like to increase ALL prices by? (1.1 = 10%)")
    
    Application.ScreenUpdating = False
    For Each wks In ActiveWorkbook.Sheets
        Set rngNumbers = wks.Cells.SpecialCells(xlCellTypeConstants, xlNumbers + xlTextValues)
        For Each rngCell In rngNumbers
            If IsNumeric(rngCell.Value) Then
                If rngCell.Value Like "*#.##" Then
                     rngCell.Value = Format(rngCell.Value * perUpdate, "0.00")
                End If
            End If
        Next
    Next
    Application.ScreenUpdating = True
End Sub

Open in new window

0
 
LVL 3

Author Closing Comment

by:LouSch7
ID: 40603468
Aikimark communicated well and provided a 100% solution for the question.
0

Featured Post

What Is Threat Intelligence?

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

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

758 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

22 Experts available now in Live!

Get 1:1 Help Now