Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 542
Default Updating links to external files using macros

Hi all

I have a file which contains links to data in an external file. Every week I
need to update the link to a new file that is created. So for example, at the
moment the link goes to a file called "Sales Firm 2010 2009-11-12", next week
there will be a new file called "Sales Firm 2010 2009-11-19" which I will
want to change this link to. The following week yet another external file
will be created which I will want to change the links to again. All the
external files are the same i.e. format, where the data is located, etc it's
just new data.

At the moment I just go into "edit" then "links" and update from there but I
want to pass this job on to other users who can just update at the push of a
button i.e. I don't want to teach them all how to update links!

Is this possible?


--
James.
  #2   Report Post  
Posted to microsoft.public.excel.programming
dan dan is offline
external usenet poster
 
Posts: 866
Default Updating links to external files using macros

Please paste the following code in the ThisWorkbook module:

'---------------------------------------------------------------------------------------
' Procedure : Workbook_Open
' Author : Dan
' Date : 11/19/2009
' Purpose : Assign Keyboard shortcut Ctrl+Shift+U to the macro
'---------------------------------------------------------------------------------------
'
Private Sub Workbook_Open()

On Error GoTo Workbook_Open_Error

Application.OnKey "^+U", "UpdateLinks"

Workbook_Open_Exit:
On Error GoTo 0
Exit Sub

Workbook_Open_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure
Workbook_Open of VBA Document ThisWorkbook"
GoTo Workbook_Open_Exit

End Sub


Please insert a module and paste this code:

Option Explicit
'---------------------------------------------------------------------------------------
' Procedure : UpdateLinks Macro
' Author : Dan
' Date : 11/19/2009
' Purpose : Update links to the new external file Keyboard Shortcut:
Ctrl+Shift+U
'---------------------------------------------------------------------------------------
'
Sub UpdateLinks()
Dim dlgOpen As FileDialog
Dim aLinks As Variant, i As Integer
Dim oldLink As String, newLink As String, FileFolder As String

On Error GoTo UpdateLinks_Error

aLinks = ActiveWorkbook.LinkSources(xlExcelLinks)

If Not IsEmpty(aLinks) Then
For i = 1 To UBound(aLinks)
If InStr(1, aLinks(i), "Sales Firm") 0 Then oldLink = aLinks(i)
Next i
Else
Err.Number = -99
Err.Description = "There are no links in this workbook!"
GoTo UpdateLinks_Error
End If

FileFolder = Left(oldLink, InStr(1, oldLink, "Sales Firm") - 1)

Set dlgOpen = Application.FileDialog(msoFileDialogOpen)

With dlgOpen
.InitialFileName = FileFolder
.AllowMultiSelect = False
.Title = "Select the new file"
If .Show = -1 Then
newLink = .SelectedItems(1)
Else
Err.Number = -99
Err.Description = "You must select a file!"
GoTo UpdateLinks_Error
End If
End With

For i = 1 To UBound(aLinks)
If InStr(1, aLinks(i), "Sales Firm") 0 Then
ActiveWorkbook.ChangeLink aLinks(i), newLink, xlExcelLinks
Next i


UpdateLinks_Exit:
On Error GoTo 0
Err.Clear
Set dlgOpen = Nothing
Set aLinks = Nothing
Exit Sub

UpdateLinks_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure
UpdateLinks"
GoTo UpdateLinks_Exit

End Sub

Let me know if it works out.

Dan


"James" wrote:

Hi all

I have a file which contains links to data in an external file. Every week I
need to update the link to a new file that is created. So for example, at the
moment the link goes to a file called "Sales Firm 2010 2009-11-12", next week
there will be a new file called "Sales Firm 2010 2009-11-19" which I will
want to change this link to. The following week yet another external file
will be created which I will want to change the links to again. All the
external files are the same i.e. format, where the data is located, etc it's
just new data.

At the moment I just go into "edit" then "links" and update from there but I
want to pass this job on to other users who can just update at the push of a
button i.e. I don't want to teach them all how to update links!

Is this possible?


--
James.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 542
Default Updating links to external files using macros

Hi Dan

Thank you for all your help. I got an error message which said "Compile
Error: Next without For" under

For i = 1 To UBound(aLinks)
If InStr(1, aLinks(i), "Sales Firm") 0 Then
ActiveWorkbook.ChangeLink aLinks(i), newLink, xlExcelLinks
Next i



--
James.


"Dan" wrote:

Please paste the following code in the ThisWorkbook module:

'---------------------------------------------------------------------------------------
' Procedure : Workbook_Open
' Author : Dan
' Date : 11/19/2009
' Purpose : Assign Keyboard shortcut Ctrl+Shift+U to the macro
'---------------------------------------------------------------------------------------
'
Private Sub Workbook_Open()

On Error GoTo Workbook_Open_Error

Application.OnKey "^+U", "UpdateLinks"

Workbook_Open_Exit:
On Error GoTo 0
Exit Sub

Workbook_Open_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure
Workbook_Open of VBA Document ThisWorkbook"
GoTo Workbook_Open_Exit

End Sub


Please insert a module and paste this code:

Option Explicit
'---------------------------------------------------------------------------------------
' Procedure : UpdateLinks Macro
' Author : Dan
' Date : 11/19/2009
' Purpose : Update links to the new external file Keyboard Shortcut:
Ctrl+Shift+U
'---------------------------------------------------------------------------------------
'
Sub UpdateLinks()
Dim dlgOpen As FileDialog
Dim aLinks As Variant, i As Integer
Dim oldLink As String, newLink As String, FileFolder As String

On Error GoTo UpdateLinks_Error

aLinks = ActiveWorkbook.LinkSources(xlExcelLinks)

If Not IsEmpty(aLinks) Then
For i = 1 To UBound(aLinks)
If InStr(1, aLinks(i), "Sales Firm") 0 Then oldLink = aLinks(i)
Next i
Else
Err.Number = -99
Err.Description = "There are no links in this workbook!"
GoTo UpdateLinks_Error
End If

FileFolder = Left(oldLink, InStr(1, oldLink, "Sales Firm") - 1)

Set dlgOpen = Application.FileDialog(msoFileDialogOpen)

With dlgOpen
.InitialFileName = FileFolder
.AllowMultiSelect = False
.Title = "Select the new file"
If .Show = -1 Then
newLink = .SelectedItems(1)
Else
Err.Number = -99
Err.Description = "You must select a file!"
GoTo UpdateLinks_Error
End If
End With

For i = 1 To UBound(aLinks)
If InStr(1, aLinks(i), "Sales Firm") 0 Then
ActiveWorkbook.ChangeLink aLinks(i), newLink, xlExcelLinks
Next i


UpdateLinks_Exit:
On Error GoTo 0
Err.Clear
Set dlgOpen = Nothing
Set aLinks = Nothing
Exit Sub

UpdateLinks_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure
UpdateLinks"
GoTo UpdateLinks_Exit

End Sub

Let me know if it works out.

Dan


"James" wrote:

Hi all

I have a file which contains links to data in an external file. Every week I
need to update the link to a new file that is created. So for example, at the
moment the link goes to a file called "Sales Firm 2010 2009-11-12", next week
there will be a new file called "Sales Firm 2010 2009-11-19" which I will
want to change this link to. The following week yet another external file
will be created which I will want to change the links to again. All the
external files are the same i.e. format, where the data is located, etc it's
just new data.

At the moment I just go into "edit" then "links" and update from there but I
want to pass this job on to other users who can just update at the push of a
button i.e. I don't want to teach them all how to update links!

Is this possible?


--
James.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 542
Default Updating links to external files using macros

Sorry ignore that! It's saying "Complie Error: Invalid outside procedure"
when I run it which is off the "Update Links" module.
--
James.


"Dan" wrote:

Please paste the following code in the ThisWorkbook module:

'---------------------------------------------------------------------------------------
' Procedure : Workbook_Open
' Author : Dan
' Date : 11/19/2009
' Purpose : Assign Keyboard shortcut Ctrl+Shift+U to the macro
'---------------------------------------------------------------------------------------
'
Private Sub Workbook_Open()

On Error GoTo Workbook_Open_Error

Application.OnKey "^+U", "UpdateLinks"

Workbook_Open_Exit:
On Error GoTo 0
Exit Sub

Workbook_Open_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure
Workbook_Open of VBA Document ThisWorkbook"
GoTo Workbook_Open_Exit

End Sub


Please insert a module and paste this code:

Option Explicit
'---------------------------------------------------------------------------------------
' Procedure : UpdateLinks Macro
' Author : Dan
' Date : 11/19/2009
' Purpose : Update links to the new external file Keyboard Shortcut:
Ctrl+Shift+U
'---------------------------------------------------------------------------------------
'
Sub UpdateLinks()
Dim dlgOpen As FileDialog
Dim aLinks As Variant, i As Integer
Dim oldLink As String, newLink As String, FileFolder As String

On Error GoTo UpdateLinks_Error

aLinks = ActiveWorkbook.LinkSources(xlExcelLinks)

If Not IsEmpty(aLinks) Then
For i = 1 To UBound(aLinks)
If InStr(1, aLinks(i), "Sales Firm") 0 Then oldLink = aLinks(i)
Next i
Else
Err.Number = -99
Err.Description = "There are no links in this workbook!"
GoTo UpdateLinks_Error
End If

FileFolder = Left(oldLink, InStr(1, oldLink, "Sales Firm") - 1)

Set dlgOpen = Application.FileDialog(msoFileDialogOpen)

With dlgOpen
.InitialFileName = FileFolder
.AllowMultiSelect = False
.Title = "Select the new file"
If .Show = -1 Then
newLink = .SelectedItems(1)
Else
Err.Number = -99
Err.Description = "You must select a file!"
GoTo UpdateLinks_Error
End If
End With

For i = 1 To UBound(aLinks)
If InStr(1, aLinks(i), "Sales Firm") 0 Then
ActiveWorkbook.ChangeLink aLinks(i), newLink, xlExcelLinks
Next i


UpdateLinks_Exit:
On Error GoTo 0
Err.Clear
Set dlgOpen = Nothing
Set aLinks = Nothing
Exit Sub

UpdateLinks_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure
UpdateLinks"
GoTo UpdateLinks_Exit

End Sub

Let me know if it works out.

Dan


"James" wrote:

Hi all

I have a file which contains links to data in an external file. Every week I
need to update the link to a new file that is created. So for example, at the
moment the link goes to a file called "Sales Firm 2010 2009-11-12", next week
there will be a new file called "Sales Firm 2010 2009-11-19" which I will
want to change this link to. The following week yet another external file
will be created which I will want to change the links to again. All the
external files are the same i.e. format, where the data is located, etc it's
just new data.

At the moment I just go into "edit" then "links" and update from there but I
want to pass this job on to other users who can just update at the push of a
button i.e. I don't want to teach them all how to update links!

Is this possible?


--
James.

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
Calculate *without* updating external links Stuart[_2_] Excel Programming 1 November 28th 07 03:20 PM
Updating external links Al Links and Linking in Excel 1 May 30th 06 07:41 AM
Problem with updating external links ChrisN Excel Discussion (Misc queries) 1 April 2nd 06 06:05 PM
Updating External Links DCSwearingen Excel Discussion (Misc queries) 1 January 25th 06 12:57 AM
External Links not updating Lou Di Pietro Excel Discussion (Misc queries) 0 September 15th 05 09:48 AM


All times are GMT +1. The time now is 11:25 AM.

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

About Us

"It's about Microsoft Excel"