Caleeco
Well-known Member
- Joined
- Jan 9, 2016
- Messages
- 980
- Office Version
- 2010
- Platform
- Windows
Hello,
I am trying to import data from a workbook that is closed into my currently open workbook. I found some code online which I chopped up, However, I am having problems with the statement below:
I still get the prompt to enable or disable macros. I want to be able to diable them and open the file as read-only. Then copy out data from the opened workbook.
Thanks in advance!
Calecco</some>
I am trying to import data from a workbook that is closed into my currently open workbook. I found some code online which I chopped up, However, I am having problems with the statement below:
Code:
Application.EnableEvents = False
Workbooks.Open FileName:=strFilepath & strFilename, ReadOnly:=True
Application.EnableEvents = True
I still get the prompt to enable or disable macros. I want to be able to diable them and open the file as read-only. Then copy out data from the opened workbook.
Code:
Sub UpdateSRelData()
'------Variables-------
Dim strFilepath As String 'Filepath for the file that needs opening
Dim strFilename As String 'Filename
Dim wbKPI As Workbook 'Wb that is currently open
Dim wbSRR As Workbook 'wb to oen
strFilepath = "S:\<some path="" directory="">\"
strFilename = "S-Release Requests Sheet.xlsm"
'--------- Check if S-Release Requests Spreadsheet is already open
Dim Ret
Ret = IsWorkBookOpen(strFilepath & strFilename)
If Ret = True Then
MsgBox "The Excel File " & strFilename & " is already opened by yourself or another user. Please close it and re-run this macro to obtain the latest data." & vbLf & "This Macro will now run on the read-only version of the available file.", vbCritical, "Error"
'Exit Sub
End If
If Dir(strFilepath & strFilename) = "" Then
MsgBox "The File named " & strFilename & " does not exist." & vbLf & "The P Parts List has not been updated.", vbCritical, "Error"
Exit Sub
Else
Application.EnableEvents = False
Workbooks.Open FileName:=strFilepath & strFilename, ReadOnly:=True
Application.EnableEvents = True
End If
Set wbSRR = Workbooks(strFilename)
Set wbKPI = Workbooks("Offload's Request for Material Summary KPIs.xlsm")
End Sub
Function IsWorkBookOpen(FileName As String)
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
Thanks in advance!
Calecco</some>
Last edited: