Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default mass changes in formulas

How do I change '2003' in all the formulas in my spreadsheet to '2004?


---
Message posted from http://www.ExcelForum.com/

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 66
Default mass changes in formulas

The easiest method is to use the Find and Replace

SuperJas.
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 66
Default mass changes in formulas

"SuperJas" wrote...
The easiest method is to use the Find and Replace.


Except that finding 2003 and replacing it with 2004 will change numbers like
120034 into 120044 and 1.2003 into 1.2004, as well as changing cell
references like AB2003 into AB2004, which are unlikely to be what the OP
wants to do. There are times when easiest isn't correct.

The following is possibly overkill, but it changes only instances of 2003
that appear as distinct, full tokens. One flaw: it changes tokens inside
string constants. No way to prevent that without writing most of a formula
parser.


Function Subst(orig_text As String, _
match_pat As String, _
replace_pat As String, _
Optional instance As Variant _
) As Variant
'------------------------------------------------------
Dim regex As Object, matches As Object, m As Object

Set regex = CreateObject("vbscript.regexp")
regex.Pattern = match_pat
regex.Global = True

If (IsMissing(instance)) Then
Subst = regex.Replace(orig_text, replace_pat)
ElseIf instance 0 Then
Set matches = regex.Execute(orig_text)
If instance matches.Count Then
Subst = orig_text 'matchnum out of bounds - do nothing
Else
Set m = matches.Item(instance - 1)
Subst = Left(orig_text, m.FirstIndex) & _
regex.Replace(m.Value, replace_pat) & _
Right(orig_text, Len(orig_text) - m.FirstIndex - m.Length)
End If
Else
Subst = CVErr(xlErrValue) 'invalid: instance <= 0
End If
End Function


Sub foo()
Dim ws As Worksheet, c As Range, cf As String, nf As Variant

Application.Calculation = xlCalculationManual

For Each ws In ActiveWorkbook.Worksheets
For Each c In ws.UsedRange
If c.HasFormula Then
cf = c.Formula
nf = Subst(cf, "(^|[^.])\b2003\b(?!\.)", "$12004")
If Not IsError(nf) And CStr(nf) < cf Then
If c.HasArray Then
c.FormulaArray = nf
Else
c.Formula = nf
End If
End If
End If
Next c
Next ws

Application.Calculation = xlCalculationAutomatic
Application.Calculate

End Sub


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22,906
Default mass changes in formulas

Hit CRTL + A to select all cells on a worksheet.

EditGo ToSpecialFormulasOK

EditReplace

What: 2003
With: 2004

Replace all.

Gord Dibben Excel MVP

On Wed, 17 Dec 2003 21:20:38 -0600, itsmetisa
wrote:

How do I change '2003' in all the formulas in my spreadsheet to '2004?


---
Message posted from http://www.ExcelForum.com/


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default mass changes in formulas

Just some added
If you can easily select cells that would not incur these type of incorrect
changes, but only do the correct changes, Edit=Replace would still be an
option. Replace will work on only the selected cells if you have at least
two cells selected.

--
Regards,
Tom Ogilvy

"Harlan Grove" wrote in message
...
"SuperJas" wrote...
The easiest method is to use the Find and Replace.


Except that finding 2003 and replacing it with 2004 will change numbers

like
120034 into 120044 and 1.2003 into 1.2004, as well as changing cell
references like AB2003 into AB2004, which are unlikely to be what the OP
wants to do. There are times when easiest isn't correct.

The following is possibly overkill, but it changes only instances of 2003
that appear as distinct, full tokens. One flaw: it changes tokens inside
string constants. No way to prevent that without writing most of a formula
parser.


Function Subst(orig_text As String, _
match_pat As String, _
replace_pat As String, _
Optional instance As Variant _
) As Variant
'------------------------------------------------------
Dim regex As Object, matches As Object, m As Object

Set regex = CreateObject("vbscript.regexp")
regex.Pattern = match_pat
regex.Global = True

If (IsMissing(instance)) Then
Subst = regex.Replace(orig_text, replace_pat)
ElseIf instance 0 Then
Set matches = regex.Execute(orig_text)
If instance matches.Count Then
Subst = orig_text 'matchnum out of bounds - do nothing
Else
Set m = matches.Item(instance - 1)
Subst = Left(orig_text, m.FirstIndex) & _
regex.Replace(m.Value, replace_pat) & _
Right(orig_text, Len(orig_text) - m.FirstIndex - m.Length)
End If
Else
Subst = CVErr(xlErrValue) 'invalid: instance <= 0
End If
End Function


Sub foo()
Dim ws As Worksheet, c As Range, cf As String, nf As Variant

Application.Calculation = xlCalculationManual

For Each ws In ActiveWorkbook.Worksheets
For Each c In ws.UsedRange
If c.HasFormula Then
cf = c.Formula
nf = Subst(cf, "(^|[^.])\b2003\b(?!\.)", "$12004")
If Not IsError(nf) And CStr(nf) < cf Then
If c.HasArray Then
c.FormulaArray = nf
Else
c.Formula = nf
End If
End If
End If
Next c
Next ws

Application.Calculation = xlCalculationAutomatic
Application.Calculate

End Sub






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default mass changes in formulas

If 2003 is part of a linked file reference, use Edit | Links...

If 2003 is part of a worksheet reference, make a copy of the 2003
worksheet, and rename the original as 2004. XL will update the
references correctly.

If 2003 is used elsewhere, you already have solutions from other
posters.

--
Regards,

Tushar Mehta, MS MVP -- Excel
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article ,
says...
How do I change '2003' in all the formulas in my spreadsheet to '2004?


---
Message posted from
http://www.ExcelForum.com/


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need LV MASS formula SilverFox Excel Discussion (Misc queries) 12 February 17th 09 06:17 AM
Mass add in a column 49erfan Excel Discussion (Misc queries) 4 October 24th 07 05:45 PM
How do I mass update formulas? karin Harpering Excel Discussion (Misc queries) 5 November 22nd 06 01:41 PM
i have lost the small + that allows mass copy of formulas in excel Brendan Excel Discussion (Misc queries) 1 January 17th 06 03:29 AM
MASS MAILING chooselife New Users to Excel 1 April 12th 05 10:48 PM


All times are GMT +1. The time now is 10:02 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"