File location refence disappearing

rrenis

Board Regular
Joined
Oct 22, 2004
Messages
120
Hi - I've got the following in a Sub that opens Summary.xls, copies in a row that contains a reference to template_cost_breakdown and replaces this refence with myFile so that a link to myfile is created. Unfortunately when the Summary.xls sheet has been opened with template_cost_breakdown.xls the E:\Summary\Costs\ part of the refence is lost.

This doesn't happen when running the VBA code as I rename template_cost_breakdown.xls prior to opening the Summary.xls - it's just if they happen to be opened at the same time for any reason. :(

Does anyone know if you can stop excel from altering the link to template_cost_breakdown .xls in the Summary spreadsheet if they are both open? Failing that could anyone help with suggesting a way of using something like If E:\Summary\Costs\ exists then run the following code else run the code excluding the reference to E:\Summary\Costs\ :confused:

Code:
Selection.Replace What:="E:\Summary\Costs\[template_cost_breakdown.xls]", Replacement :=myFile, LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False

Many Thanks,
rrenis :)
 
Do me a favor: add the following line before the one that errors:
Code:
a = Replace(Item.Formula, Actual_Old_File, myfile, , , vbTextCompare)

Then execute and go into the debugger when you get the error. Make sure your Locals window is visible (View->Locals Window), then copy the value for A and post it here so I can see it... my guess is that there is a syntax error in the formula when the replacement happens. Should be a quick fix, but I need to see the EXACT formula string after the replacement. You know, it wouldn't hurt to also paste the value of Myfile and Actual_Old_File, they may help me.

Don't worry about taking up too much of my time... if I suddenly need to stop helping, I'll tell you nicely. Until then, it bothers me that I can't get this right for you... it should be pretty straight-forward, I want to know what I may have forgotten to take into consideration in building this code...
 
Upvote 0

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
btw, if it's any help here's the full code in the module...

Code:
Sub Links()

' warning box
Dim Name As String
Name = ActiveSheet.Range("Q84")

Dim Resp As Integer
Resp = MsgBox("Click 'Yes' to automatically save this spreadsheet and set up links to the Summary.   " & vbNewLine & _
"Your valuation spreadsheet will be saved to the following directory...    " & vbNewLine & _
" " & vbNewLine & _
"E:\Summary\Costs\" & Name & vbNewLine & _
" " & vbNewLine & _
"If you are not connected to the Network please click 'No' and set up the links at a  " & vbNewLine & _
"later date. " & vbNewLine & _
" " & vbNewLine & _
"Do you want to Continue? ", 36, "Auto Save and Summary Links")
If Resp = 6 Then

'On Error GoTo ErrorMessage

' setup String
Dim myfile As String
Dim openFile As String
Dim OrderNumber As String

OrderNumber = ActiveSheet.Range("A28")

' autosave 
ActiveWorkbook.SaveAs FileName:=Range("A4"), FileFormat _
:=xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:= _
False, CreateBackup:=False

' setup myFile 
myfile = ActiveWorkbook.Path & "\" & "[" & ActiveWorkbook.Name & "]"
openFile = ActiveWorkbook.Name

'open summary and auto update links
Workbooks.Open FileName:= _
"E:\Summary\Costs\Summary.xls", UpdateLinks:=3

 ' insert links > unhide, copy cells, hide and rename using myFile
Dim sht As Object
For Each sht In ActiveWorkbook.Sheets
  sht.Visible = xlSheetVisible
Next sht
ActiveWorkbook.Sheets("Info").Visible = xlSheetVeryHidden

' check to see whether project has already been added to summary
Sheets("SUMMARIES").Select
ActiveSheet.Unprotect ""
Application.ScreenUpdating = False
Application.DisplayAlerts = False
If Application.WorksheetFunction.CountIf(Sheets("SUMMARIES").Range("A6:A65536"), OrderNumber) > 0 Then GoTo RefExists Else
ActiveSheet.Protect ""

Sheets("SUMMARIES").Select
ActiveSheet.Unprotect ""
ActiveSheet.Calculate
Rows("7:7").Select
Selection.Insert Shift:=xlDown
Rows("5:7").Select
Range("A7").Activate
Selection.EntireRow.Hidden = False
Range("B6:BH6").Select
Selection.Copy
Range("B7").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Rows("6:6").Select
Range("B6").Activate
Selection.EntireRow.Hidden = True
Range("B7:BH7").Select

    Dim Item As Range 
    Dim File_Strt As Long 
    Dim Old_File As String 
    Dim Strt 
    Dim item2 
    Dim Strt_Num As Long 
    Dim Op_Num As Long 
    Dim Actual_Old_File As String 
    Dim bang As Long 
    
    Strt = Array("=", "-", "+", "*", "/", "<", ">", "(", ",", "^", "&") 
      
    Actual_Old_File = "" 
    
    Old_File = "[template_cost_breakdown.xls]" 
    
    For Each Item In Selection 
    
        File_Strt = InStr(1, Item.Formula, Old_File, vbTextCompare) 
        
        If File_Strt > 0 Then 
        
            If Actual_Old_File = "" Then 
        
                Strt_Num = 0 
            
                For Each item2 In Strt 
                
                    Op_Num = InStrRev(Item.Formula, item2, File_Strt) 
                    
                    If Op_Num > Strt_Num Then 
                    
                        Strt_Num = Op_Num 
                        
                    End If 
                
                Next 
                
                Actual_Old_File = Mid(Item.Formula, Strt_Num + 1, File_Strt - Strt_Num + Len(Old_File) - 1) 
                
                bang = InStr(1, Item.Formula, "!") 
                
                If Mid(Item.Formula, bang - 1, 1) = "'" And Left(myfile, 1) <> "'" Then 
                
                    myfile = "'" & myfile 
                    
                End If 
                
            End If 
            
            Item.Formula = Replace(Item.Formula, Actual_Old_File, myfile, , , vbTextCompare) 
            
        End If 
    
    Next Item 

' save and close valuation summary workbook
ActiveWorkbook.Sheets("Info").Visible = xlSheetVisible
For Each sht In ActiveWorkbook.Sheets
  If sht.Name <> "Info" Then sht.Visible = xlSheetVeryHidden
Next sht
ActiveWorkbook.Close SaveChanges:=True


Application.Workbooks(openFile).Activate
Sheets("Report").Select

Range("G25").Select

MsgBox "This Project has been successfully saved and added to the Summary.      " & vbNewLine & _
" " & vbNewLine & _
"Click OK to Continue.", 0, "Summary Links"
        
End If

MacroExit:

   Exit Sub
   
ErrorMessage:
   MsgBox "An unexpected error has occurred." _
      & vbCrLf & "" _
      & vbCrLf & "Please report the following information:   " _
      & vbCrLf & " " _
      & vbCrLf & "Macro Name: Links" _
      & vbCrLf & "Error Number: " & Err.Number _
      & vbCrLf & "Error Description: " & Err.Description _
      , vbCritical, "Error!"
      Resume MacroExit
      
RefExists:
    ActiveSheet.Protect ""
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    MsgBox "The following Project has either already been added to the Summary       " & vbNewLine & _
    "" & vbNewLine & _
    "Valuation Filename:" & vbNewLine & _
    "" & openFile & "   " & vbNewLine & _
    "", 64, "Project Previously Added"
    
    ActiveWorkbook.Close SaveChanges:=False
    Application.Workbooks(openFile).Activate
      
    MsgBox "This Project has been successfully saved.      " & vbNewLine & _
    " " & vbNewLine & _
    "Click OK to Continue.", 0, "Auto Save Valuations"
      
End Sub

Cheers,
rrenis :biggrin:
 
Upvote 0
Well, I see one possible problem at a glance: you have protected your worksheet before running the code to change the formulas... you can't change the formula when the sheet is protected (unless the cells are not locked).
 
Upvote 0
Hi hatman - here's what was returned...

: a : "=E:\Summary\Costs\Select Name\[Test 1234.xls]Costs 1'!$C$16" : Variant/String

cheers,
rrenis :biggrin:
 
Upvote 0
Doh!! I'll remove the protect line and try again! Thanks - Can't see the wood for the trees!... :rolleyes:
 
Upvote 0
Hi hatman - still getting an error I'm afriad :oops:

Just edited my code above as I'd accidently overwritten the unhide row code that I'd recorded when I pasted in the new replace link code. Also tidied a couple of things up where I'd copied code in from other modules.

cheers,
rrenis :)
 
Upvote 0
Okay, the problem is obvious: look at the value of A "=E:\Summary\Costs\Select Name\[Test 1234.xls]Costs 1'!$C$16" there needs to be an open quote before the E:.

My assumption is that you have links to multiple sheets, some with spaces in the name, some without... which means that some of the path strings need to be enclosed in single quotes, and others not. Try this:

1) Delete this code
Code:
                bang = InStr(1, Item.Formula, "!") 
                
                If Mid(Item.Formula, bang - 1, 1) = "'" And Left(myfile, 1) <> "'" Then 
                
                    myfile = "'" & myfile 
                    
                End If

2) Replace the array definition with this instead:
Code:
Strt = Array("=", "-", "+", "*", "/", "<", ">", "(", ",", "^", "&", "'")

I don't really have time test this myself right now, so please try it and let me know... I think it will take care of all possibilities by EXCLUDING the leading quote from the replacement process altogether... if it is present, it will be left where it is, if it is not there, it won't be added where it doesn't belong. I should have thought of this in the first place :oops:
 
Upvote 0
hi hatman - thanks again for the reply - I really appreciate it. Your code works flawlessley if I don't change the myfile line but if I delete that and the Dim myfile AsString so it picks up the myfile reference from earlier in my orginal code it errors on the same line of your code as before... :confused:

Code:
Item.Formula = Replace(Item.Formula, Actual_Old_File, myfile, , , vbTextCompare)

Cheers,
rrenis
 
Upvote 0
do the same trick as before and paste in the value of "a" when it errors out, so I can see the actual string that is being assigned after the replacement.
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,332
Members
449,077
Latest member
jmsotelo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top