VBA to find filename and replace

Hi
I just found the answer to "Find and Replace in VBA" and tried to adapt it 
for what I'm doing, but it's more difficult so I need some help please.

I have multiple worksheets in an excel 2007 workbook - a monthly report.
Within the worksheets I have many cells that link to last months workbooks - 
(i.e I compare last month figures to this months figures).

I need the macro to do the following:

- Create an Input box for the user to "enter the name ofTHE OLD months 
workbook"

- Create an Input box for the user to "enter the name of THE NEW months 
workbook"

- Excel finds cells with links containing the OLD workbook name and replaces 
it with the NEW workbook name

The problem:

After the user enters the NEW name excel asks the user to also select the 
new workbook from a window and sometimes requires the worksheet to be 
selected as well.

Is there any way to stop this requirement, but to ensure that the revised 
links work?

I tried including "Application.ScreenUpdating = False" and 
"Application.DisplayAlerts = False", but then the revised formulas turned to 
#REF once all of the old & new workbook names had been entered (9 times each 
for 9 worksheets).

Also - is there a way to enter the Old and New workbook names once instead 
of once for every worksheet in the workbook?

Old worksheet name:
..../[Plan for Restructuring and Rebudgeting as at 06_10_09 XP 
version.xls]/Monthly Detailed P&L

New worksheet name:
..../[Plan for Restructuring and Rebudgeting as at 07_10_09 XP 
version.xls]/Monthly Detailed P&L

No other details in the links change (i.e. locations like the folder names, 
worksheet name and cell etc are in exactly the same place in the new workbook 
so they don't need to change) 

Current code:

Sub Replace()
Dim WS As Worksheet

Application.ScreenUpdating = False
Application.DisplayAlerts = False

For Each WS In Worksheets
  WS.Cells.Replace What:=InputBox("Enter prior workbook words to be replaced 
in links"), Replacement:=InputBox("Enter new workbook words to be entered 
into links"), _
           LookAt:=xlPart, SearchOrder:=xlByColumns, _
           MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Next

Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub


-- 
Thank for your help in Advance
BeSmart
0
Utf
1/27/2010 8:16:08 AM
excel.programming 6508 articles. 1 followers. Follow

2 Replies
1481 Views

Similar Articles

[PageSpeed] 30

Hi BeSmart, If I completely understand you want to loop though the workbook 
adn each sheet in that workbook updating your hyperlinks. Right? If thats the 
case try somthing like this.

Sub UpdateLinks()
  Const FolderPath As String = "\\NetworkShare\YourFolder\YourSubfolder\"
  Dim aWorkbook As Workbook
  Dim aWorksheet As Variant
  Dim OldFile As String
  Dim TargetFile As String
  Dim Link As Hyperlink
  
  Set aWorkbook = ThisWorkbook
  OldFile = InputBox("Enter OldFile Name")
  TargetFile = InputBox("Enter File Name")
    
  'Loop through worksheets
  For Each aWorksheet In aWorkbook.Worksheets
   'Loop through Hyperlinks in worksheet
    For Each Link In Worksheets(aWorksheet.Name).Hyperlinks
     'Check for oldFile Name
      If InStr(1, Link.TextToDisplay, OldFile, vbTextCompare) > 0 Then
        With Link
           'Update Hyperlink info
          .Address = FolderPath + TargetFile
          .TextToDisplay = FolderPath + TargetFile
        End With
      End If
    Next
  Next
End Sub


"BeSmart" wrote:

> Hi
> I just found the answer to "Find and Replace in VBA" and tried to adapt it 
> for what I'm doing, but it's more difficult so I need some help please.
> 
> I have multiple worksheets in an excel 2007 workbook - a monthly report.
> Within the worksheets I have many cells that link to last months workbooks - 
> (i.e I compare last month figures to this months figures).
> 
> I need the macro to do the following:
> 
> - Create an Input box for the user to "enter the name ofTHE OLD months 
> workbook"
> 
> - Create an Input box for the user to "enter the name of THE NEW months 
> workbook"
> 
> - Excel finds cells with links containing the OLD workbook name and replaces 
> it with the NEW workbook name
> 
> The problem:
> 
> After the user enters the NEW name excel asks the user to also select the 
> new workbook from a window and sometimes requires the worksheet to be 
> selected as well.
> 
> Is there any way to stop this requirement, but to ensure that the revised 
> links work?
> 
> I tried including "Application.ScreenUpdating = False" and 
> "Application.DisplayAlerts = False", but then the revised formulas turned to 
> #REF once all of the old & new workbook names had been entered (9 times each 
> for 9 worksheets).
> 
> Also - is there a way to enter the Old and New workbook names once instead 
> of once for every worksheet in the workbook?
> 
> Old worksheet name:
> .../[Plan for Restructuring and Rebudgeting as at 06_10_09 XP 
> version.xls]/Monthly Detailed P&L
> 
> New worksheet name:
> .../[Plan for Restructuring and Rebudgeting as at 07_10_09 XP 
> version.xls]/Monthly Detailed P&L
> 
> No other details in the links change (i.e. locations like the folder names, 
> worksheet name and cell etc are in exactly the same place in the new workbook 
> so they don't need to change) 
> 
> Current code:
> 
> Sub Replace()
> Dim WS As Worksheet
> 
> Application.ScreenUpdating = False
> Application.DisplayAlerts = False
> 
> For Each WS In Worksheets
>   WS.Cells.Replace What:=InputBox("Enter prior workbook words to be replaced 
> in links"), Replacement:=InputBox("Enter new workbook words to be entered 
> into links"), _
>            LookAt:=xlPart, SearchOrder:=xlByColumns, _
>            MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
> Next
> 
> Application.ScreenUpdating = True
> Application.DisplayAlerts = True
> End Sub
> 
> 
> -- 
> Thank for your help in Advance
> BeSmart
0
Utf
1/27/2010 2:27:01 PM
Hi Jeff
Thanks for your code - however I'm not using "Hyperlinks"...
I have formulas that include links in their calculations e.g.
Cell A5 formula is:

='C:\Documents and Settings\Bob.JOB-Bob\My Documents\Bob 
Personal\Inventors\Meetings\[XClient Plan for Restructuring and Rebudgeting 
as at 06_10_09 XP version.xls]Detailed Report'!$U$18+$C83

The macro needs to prompt the user to enter the OLD workbook name (above) as 
and then 
enter the new workbook name e.g. "[XClient Plan for Restructuring and 
Rebudgeting as at 07_10_09 XP version.xls]" (the date changed in the 
filename).

Nothing else changes changes in the formula.

With the code I used before wanted the user to select the file and sometimes 
the worksheet as well - for every worksheet...

We only want to change the workbook name once (when the user enters the two 
names into the input boxes) - without having to also navigate through to the 
actual file in a FILE/OPEN window for each worksheet.

Hopefully this will only be a small change to the code you have already 
written.
-- 
Thank again for your help - I really appreciate it.
BeSmart


"Jeff" wrote:

> Hi BeSmart, If I completely understand you want to loop though the workbook 
> adn each sheet in that workbook updating your hyperlinks. Right? If thats the 
> case try somthing like this.
> 
> Sub UpdateLinks()
>   Const FolderPath As String = "\\NetworkShare\YourFolder\YourSubfolder\"
>   Dim aWorkbook As Workbook
>   Dim aWorksheet As Variant
>   Dim OldFile As String
>   Dim TargetFile As String
>   Dim Link As Hyperlink
>   
>   Set aWorkbook = ThisWorkbook
>   OldFile = InputBox("Enter OldFile Name")
>   TargetFile = InputBox("Enter File Name")
>     
>   'Loop through worksheets
>   For Each aWorksheet In aWorkbook.Worksheets
>    'Loop through Hyperlinks in worksheet
>     For Each Link In Worksheets(aWorksheet.Name).Hyperlinks
>      'Check for oldFile Name
>       If InStr(1, Link.TextToDisplay, OldFile, vbTextCompare) > 0 Then
>         With Link
>            'Update Hyperlink info
>           .Address = FolderPath + TargetFile
>           .TextToDisplay = FolderPath + TargetFile
>         End With
>       End If
>     Next
>   Next
> End Sub
> 
> 
> "BeSmart" wrote:
> 
> > Hi
> > I just found the answer to "Find and Replace in VBA" and tried to adapt it 
> > for what I'm doing, but it's more difficult so I need some help please.
> > 
> > I have multiple worksheets in an excel 2007 workbook - a monthly report.
> > Within the worksheets I have many cells that link to last months workbooks - 
> > (i.e I compare last month figures to this months figures).
> > 
> > I need the macro to do the following:
> > 
> > - Create an Input box for the user to "enter the name ofTHE OLD months 
> > workbook"
> > 
> > - Create an Input box for the user to "enter the name of THE NEW months 
> > workbook"
> > 
> > - Excel finds cells with links containing the OLD workbook name and replaces 
> > it with the NEW workbook name
> > 
> > The problem:
> > 
> > After the user enters the NEW name excel asks the user to also select the 
> > new workbook from a window and sometimes requires the worksheet to be 
> > selected as well.
> > 
> > Is there any way to stop this requirement, but to ensure that the revised 
> > links work?
> > 
> > I tried including "Application.ScreenUpdating = False" and 
> > "Application.DisplayAlerts = False", but then the revised formulas turned to 
> > #REF once all of the old & new workbook names had been entered (9 times each 
> > for 9 worksheets).
> > 
> > Also - is there a way to enter the Old and New workbook names once instead 
> > of once for every worksheet in the workbook?
> > 
> > Old worksheet name:
> > .../[Plan for Restructuring and Rebudgeting as at 06_10_09 XP 
> > version.xls]/Monthly Detailed P&L
> > 
> > New worksheet name:
> > .../[Plan for Restructuring and Rebudgeting as at 07_10_09 XP 
> > version.xls]/Monthly Detailed P&L
> > 
> > No other details in the links change (i.e. locations like the folder names, 
> > worksheet name and cell etc are in exactly the same place in the new workbook 
> > so they don't need to change) 
> > 
> > Current code:
> > 
> > Sub Replace()
> > Dim WS As Worksheet
> > 
> > Application.ScreenUpdating = False
> > Application.DisplayAlerts = False
> > 
> > For Each WS In Worksheets
> >   WS.Cells.Replace What:=InputBox("Enter prior workbook words to be replaced 
> > in links"), Replacement:=InputBox("Enter new workbook words to be entered 
> > into links"), _
> >            LookAt:=xlPart, SearchOrder:=xlByColumns, _
> >            MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
> > Next
> > 
> > Application.ScreenUpdating = True
> > Application.DisplayAlerts = True
> > End Sub
> > 
> > 
> > -- 
> > Thank for your help in Advance
> > BeSmart
0
Utf
1/28/2010 8:14:58 AM
Reply:

Similar Artilces:

VBA code to say Yes or No if any VBA code is present in ActiveWork
Using XL 2003 & 97 Would like hit Ctrl+Shift+V and have an answer either yes or no as to the presence of Any VBA code in the Active Workbook. I do know that about the VBA editor/Explorer or to press the Run Macro button and get a list from various sources . I would like to stay at the keyboard and process a series of macros including one to let me know if I even need to evoke the VBA explorer. TIA Dennis There is no bootstrap macro to tell you whether you have macros or not. If there were there would be no limit to virus writers. --- HTH, David McRitchie, Microsoft MVP - Excel ...

Use VBA to update Access table or Query from Excel
Can I use VBA to update Access table or Query from Excel? Thanks in advance Hi Leungkong, > Can I use VBA to update Access table or Query from Excel? Of course, using ADO or DAO. See: http://www.erlandsendata.no/english/index.php?d=envbadacexportado Regards, Jan Karel Pieterse Excel MVP http://www.jkp-ads.com Hi Jan, Thanks. I think ADO is what I want. But I am not only want to export from excel to access. I want to edit some data in access table. For example, Access has a table "ProductList" I want to use Excel to call the product by "Pro...

Find e-mail account info.
I need to know how to get the pop3 and stmp info from both a hotmail and msn account to set up my mail retrevial in outlook. You cannot pop email from hotmail, you need to use HTTPMail. When you setup an account, use the HTTP account type. >-----Original Message----- >I need to know how to get the pop3 and stmp info from both >a hotmail and msn account to set up my mail retrevial in >outlook. >. > ...

Print, Merged Cells, and VBA
Hello I'm looking for a code that will help me with a print job for a worksheet. There are a lot of merged cells and a lot of "IF"s to be used in the VBA for range selection. Thanks in advance, Sarr ...

Where do I find the email address?
I'm trying to create an email address J&L@JLRX.COM.AU as an SMTP email address to my test user, but the system tells me that the email address already exists. How do I find which of my users has that email address? Exchange 2003. -- | +-- JDMils | There are loads of ways - I detailed some of them here: http://hellomate.typepad.com/exchange/2003/08/finding_smtp_ad.html -- Neil Hobson Exchange MVP http://www.msexchange.org/Neil_Hobson/ "JDMils" <admin@jdmils.com> wrote in message news:OiXOb9mcGHA.4312@TK2MSFTNGP05.phx.gbl... > I'm trying to create an...

VBA and Scheduled task in Outlook
Hi, how can I: a) pull the query from somewhere and send it by e-mail automatically. E.g. I want to take a temperature table from http://www.wunderground.com/history/airport/EHAM/2009/7/10/DailyHistory.html?req_city=NA&req_state=NA&req_statename=NA where 2009/7/10 is the date of yesterday put it into an e-mail and send it automatically each day at 8 o'clock? b) if this is not possible, how can I send an e-mail with the attachment c:\Documents\query20090710.xls each day at 8 o'clock? my boss wants this automatized, I know how to write the VBA code for Excel, but don'...

how replace comma with point in import data
I import some stocks prices from the web. They come in with a comma as decimal limitator and I need them to appear with a point instead. how can I convert it ? Set up your Windows Regional Options (in Control Panel) for the Format in which you want to read your input data. Format Cells/ Number for the way that you want to display the output. -- David Biddulph "Gepetto" <Gepetto@discussions.microsoft.com> wrote in message news:2D214513-6C9B-4E14-8AAD-EF7012728197@microsoft.com... >I import some stocks prices from the web. They come in with a comma as > ...

can find internet connection
Just finished installing a new cpu (old one died a month ago) and now when starting M07, I get a message "you must be connected to the internet to sign in online." I am definitely connected to the internet. I noticed that Money was able to add a recent update and I've tried reinstalling it. Any thoughts? Thanks. I just insatlled IE 7 and now my Money 2005 won't connect to the internet either. Obviously the IE 7 is blocking the connection. Does anyone know how to work around this problem? "Gus" wrote: > Just finished installing a new cpu (old one died...

vba select field
Hi, having a button on a form, is it possible to select only some fields (all from the same table) of the current record instead of all fields? I've tried this code: RunCommand acCmdSelectRecord RunCommand acCmdCopy Forse dipende dei dati nel record. but it selects ALL fields in the form. Thank you. Remigio hi, On 10.04.2010 18:01, remigio wrote: > having a button on a form, is it possible to select only some fields > (all from the same table) of the current record instead of all fields? > I've tried this code: > > RunCommand acCmdSelectRecord &...

VLOOKUP in VBA
On the worksheet I can insert in a cell =VLOOKUP(C5, Hobokee.xls!AcsLow, 2) and it works perfectly, looks up the value in column 2 of the range named AcsLow in the same workbook. But elswhere I want in a macro to lookup the same table and assign the result to a variable BalAmt. BalAmt = VLOOKUP(AccNum, Hobokee.xls!AcsLow, 2) does not work. It gives a function not defined error on Hobokee. If instead of Hobokee I put Workbooks("Hobokee"), it gives function not defined error for VLOOKUP which it changes to VLookup. I have been overVLOOKUP in the Help file and see nothing wrong. W...

Excel VBA/Macro programming for beginners
Hi all, I know a bit VB 6.0 programming. Will it help me to write exce vba/macro(same thing?).can anybody help me by providing some fre websites where i can have informations/codes for VBA/macro. Please tell me how could i make a *command Button * in a cell in th excel sheet -- sdebu_200 ----------------------------------------------------------------------- sdebu_2000's Profile: http://www.officehelp.in/member.php?userid=430 View this thread: http://www.officehelp.in/showthread.php?t=118812 Posted from - http://www.officehelp.i Try David McRitchie's "getting started with mac...

Turn pc speaker on/off with vba code
Is this possible? Thanks I found something here on how to do it withVBScript. You can probably modify it to work in VBA http://www.pcreview.co.uk/forums/thread-1468591.php -- HTH, Barb Reinhardt "David" wrote: > Is this possible? > Thanks Thanks Barb I'll have a go (I need to improve my web searching skills) "Barb Reinhardt" wrote: > I found something here on how to do it withVBScript. You can probably modify > it to work in VBA > > http://www.pcreview.co.uk/forums/thread-1468591.php > -- > HTH, > ...

get change operation on coCustomLinks cannot find table
Hello, I have this client that has deployed their server in Singapore but has a branch in Thailand. Since Thailand also needs to use Great Plains, they have installed a GP 8.0 application in a workstation in Thailand and connects to the database deployed in Singapore. But when the staff in thailand logs onto her workstation, she gets the error "get/change first operation on coCustomLinks cannot find table" then all suceeding document processing (i.e. GL entries), go haywire - the next journal entry number doesn't generate. They went through the same installation procedures ...

DevCon
I'm running Win 7 64-bit on an AMD processor. Most times, when I boot, my network adapter fails to initialize properly. I've found if I disable and enable it through the device manager, it will work fine from then on. To automate this, I picked up the WDK and DevCon.exe, and thought I'd set up a batch file to prep the adapter after boot. I can determine status as follows: C:\WinDDK\7600.16385.1\Tools\devcon\amd64>devcon status =net *81411043* PCI\VEN_10DE&DEV_0057&SUBSYS_81411043&REV_A3\3&2411E6FE&1&50 Name: NVIDIA nForce Networking Co...

Replacing Characters
I have a column (lets say column A) that has values like the following: 15467 12099 2099 1312 What I would like to do is make column B replace the last 2 characters with zeros so that column b looks like the following: 15400 12000 2000 1300 Can anyone help? Thanks in advance! Hi in B1 place =ROUNDDOWN(A1,-2) and copy this down for all rows -- Regards Frank Kabel Frankfurt, Germany "Jenn" <Jenn@discussions.microsoft.com> schrieb im Newsbeitrag news:98032363-0DD8-4C4A-8C18-1C86C6DC2F8C@microsoft.com... > I have a column (lets say column A) that has values like the follow...

How to find the associated FrameWnd from a view ?
In a MDI application, how can I find the associated FrameWnd of a CEditView? The code fragment below works, but there must be a simpler and direct method. Any suggestions? CMainFrame* pFrame = (CMainFrame *)AfxGetApp()->m_pMainWnd; CFrameWnd* pNextWnd=pFrame->GetActiveFrame(); // then check every FrameWnd until a match is found: pNextWnd = (CFrameWnd*) pNextWnd->GetNextWindow(); ... Use GetParentFrame: ASSERT_VALID(pView); CFrameWnd* pFrameWnd = pView->GetParentFrame(); ASSERT_VALID(pFrameWnd); --------- Ajay Kalra ajaykalra@yahoo.com Wow, I am impresse...

Referencing dates against Products, in order to find the Qty. With out using Column referencing
if i have 1 sheet which has the master data for all customers and then i have several workbooks with customer sales Master sheet B2 Sales Date - this row has every day of the year A3 product B3 is sales Qty 1 1 2 55 3 55 Customer sheet has a similar layout but because this is made from a pivot table its only based on sales so when it comes to christmas day when there are no sales i cannot simply do a vlookup using column references as my data will be out of sync How do i reference the product and the date ...

What's the best way to find out if user is currently using mailbox ?
on Exch2000Sp3, I go to Systems Manager and I see mydomain\certain user mailbox. Last logoff time = blank. As an example, I compare my logoff information: my Outlook is currently opened but my logoff time = 10/07/04, 9:00AM Are those info reliable indicators of current mailbox activity ? Please advise. If policy allows, examine the sent items folder. -- regards, Michael Abbaticchio MVP for Microsoft Exchange Server http://mvps.org/exchange "Marlon Brown" <marlon_brown@hotmail.com> wrote in message news:%23%23LdPdMrEHA.4008@TK2MSFTNGP14.phx.gbl... > on Exch2000Sp...

Loading a Form from VBA
Hi all, I'd like a module to open and load a form in my Access 2003 database... How can I do this? Use the OpenForm method. Look in the Help file for details. -- Doug Steele, Microsoft Access MVP http://I.Am/DougSteele (no e-mails, please!) "Mike" <michael.leon.schwartz@gmail.com> wrote in message news:1184770076.109526.207900@o11g2000prd.googlegroups.com... > Hi all, I'd like a module to open and load a form in my Access 2003 > database... How can I do this? > On Jul 18, 10:58 am, "Douglas J. Steele" <NOSPAM_djsteele@NOSPAM_canada.com&g...

Pls assist How 2 find Mystery in complex pivottable data source base on access
Hi there, I was asked to maintain a complex system in which pivottable are base on access object (MDB) I went into the wizard returned to the fields making the pivottable Went through all the queries and tables in the MDB but could not find out where the pivottable (excel) is getting the (wrong) data. Is there a way to see the SQL which makes the pivottable?? Any advice will be appreciated You could run a macro to view the pivot cache's command text. For example, the following code would print the text in the Immediate window of the Visual Basic Editor: Sub ShowCommandText() 'show...

Excel and VBA
I am very interested in learning more about using VBA in office, especially Excel. Can you post some resources (online, books, etc) that can be used to help learn about using Visual Basic in office. Thanks! Here's a good place to start: http://www.contextures.com/xlbooks.html Does that help? *********** Regards, Ron XL2002, WinXP "Bob Sinclair" wrote: > I am very interested in learning more about using VBA in office, especially > Excel. Can you post some resources (online, books, etc) that can be used to > help learn about using Visual Basic in office. >...

VBA Customization
Is there a way to get the name of the current company through VBA in Great Plains 8.0? Thanks for all your help. open GreatPlains. Add the current window, it will add the Toolbar then add fields choose the company name on the top left after the user id. >-----Original Message----- >Is there a way to get the name of the current company through VBA in Great >Plains 8.0? > >Thanks for all your help. > > > >. > Thanks. I tried, but I couldn't add company name to the field list. It opens up the login screen instead. Any thoughts? Thanks again. "ger...

Run VBA without show
I want excel to run the VBA code without showing me what it is doing. Which code should i use for that? Sub RunWithoutShow() Application.ScreenUpdating = False 'Paste the vba code here 'end of code 'Change screenupdating property to true Application.ScreenUpdating = True End Sub I hope this helps... Selva V Pasupathy For more on Excel, VBA, & other Resources Please visit: http://socko.wordpress.com You may also (inserting into quote from code example bellow from Socko) Use Application.Visible property to hide the application entirely Sub RunWithoutShow() Appli...

Form Controls vs VBA controls
I am new to VBA so just finding my feet, can anyone help me with th differences between an excel control (i.e. one from the form toolbox lets say a checkBox and a vba checkBox? From trying the two controls the I have noticed is that you canno capture the change event of the VBA checkbox in the code module, it ha to be done via the sheet (i.e. checkbox on sheet1, code goes in chang event on sheet1) the control is placed on, is this correct? What woul you do if you have three sheets with the same controls just differen data, do you really have to code the control 3 times? The excel control ho...

Comment Cards where can I find them to print and change
Comment Cards where can I find them to print and change Did you try Google? If none are available, create your own. -- JoAnn Paules MVP Microsoft [Publisher] ~~~~~ How to ask a question http://support.microsoft.com/KB/555375 "ET" <ET@discussions.microsoft.com> wrote in message news:6C1EBFCF-D36E-4E31-8092-DCDCD672206A@microsoft.com... > Comment Cards where can I find them to print and change ...