Macro does not run if file is open

hrayani

Well-known Member
Joined
Jul 23, 2010
Messages
1,539
Office Version
  1. 2016
Platform
  1. Windows
Hello,


I am using the below code to update links


Code:
Sub update()    
    ActiveWorkbook.UpdateLink Name:="\\192.168.0.100\itex share\ITEX\DATA ENTRY.xlsm" _
        , Type:=xlExcelLinks
    
    End Sub


The code works fine when the source file is closed. but gives a debug msg when the source file is open.

I would like the code to run whether source file is open or closed.

Any help would be appreciated

Regards,

Humayun
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
The code works fine when the source file is closed. but gives a debug msg when the source file is open.

What is the debug msg?
 
Upvote 0
Hi Mark,

Sorry for replying late. I was on holidays.

This is the debug msg i am getting when the source file is open

Run-time error '1004':
Method 'UpdateLink' of object'_Workbook' failed
 
Upvote 0
Try putting both codes below in the same regular module and running the code.

Rich (BB code):
Sub update()
    Dim myTest
    myTest = IsWorkBookOpen("\\192.168.0.100\itex share\ITEX\DATA ENTRY.xlsm") 'CHANGE TO FULL WORKBOOK PATH IF INCORRECT
    If myTest = True Then
        Application.Calculate
    Else
        ActiveWorkbook.UpdateLink Name:="\\192.168.0.100\itex share\ITEX\DATA ENTRY.xlsm" _
                                        , Type:=xlExcelLinks
    End If
End Sub

Function IsWorkBookOpen(FileName As String)
    ' Function curtesy of Siddharth Rout
    Dim ff As Long, ErrNo As Long

    On Error Resume Next
    ff = FreeFile()
    Open FileName For Input Lock Read As #ff 
    Close ff
    ErrNo = Err
    On Error GoTo 0

    Select Case ErrNo
        Case 0: IsWorkBookOpen = False
        Case 70: IsWorkBookOpen = True
        Case Else: Error ErrNo
    End Select
End Function
 
Upvote 0
Hello Mark,

Thanks a lot. Working just perfect now.

I just added these two lines in the code

Start of Code
ActiveSheet.Unprotect Password:="xxx"

End Of Code
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFormattingColumns:=True, AllowFormattingRows:=True _
, Password:="xxx"




Thanks once again

Regards,

Humayun
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,148
Members
453,021
Latest member
Justyna P

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