Update data in excel via VBA, without opening the file

saurab8

New Member
Joined
Jun 21, 2019
Messages
9
hi there,

I have a pipeline excel report which have a number of macro's.
Currently when a staff update some information in this pipeline report he/she press a button to run the macro.

Macro then:
1) open another file (called database)
2) search the customer number
3) update the row in database
4) save and close the database

The issue I am facing is I have multiple users updating the pipeline report.
So sometime if staff A and staff B press the button at the same time.
One of the update will happen and other will get an error message.

If there a way I could update the database without opening the excel file?
No two staff ever work on same customer number.

or maybe someone could suggest me another way to resolve this issue.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
maybe someone could suggest me another way to resolve this issue.

I created a timesheet Addin about 10 + years ago for charity my daughter worked for where over 250 staff submitted times-sheets each week over the corporate network - as her employer would not allow use of MS Access database, I created an Excel workbook as the database for users to submit their data to & managed read / write access with a Function that first checked workbook open state & informed users if open by another user - it seemed to performed without issues & may work for you

Place following in STANDARD Module

Code:
Function DatabaseOpen(ByVal FileName As String, Optional ByVal UpdateLinks As Variant, Optional ByVal ReadOnly As Boolean, Optional ByVal Password As String) As Workbook
    Dim Response    As VbMsgBoxResult
    Dim FileInUse   As Boolean
    
    'dmt32 June 2022
    If Not Dir(FileName, vbDirectory) = vbNullString Then
        If Not ReadOnly Then
            'check if file already open read/write
            Do
                On Error Resume Next
                Open FileName For Binary Access Read Lock Read As #1
                Close #1
                FileInUse = CBool(Err.Number > 0)
                On Error GoTo 0
                If FileInUse Then
                    'read / write file in use
                    Response = MsgBox("File Is Open For Editing By Another User." & Chr(10) & _
                    "Do You Want To Try Again?", 37, "File In Use")
                    If Response = vbCancel Then Set DatabaseOpen = Nothing: Exit Function
                End If
            Loop Until Not FileInUse
        End If
        
        Set DatabaseOpen = Workbooks.Open(FileName, UpdateLinks:=UpdateLinks, ReadOnly:=ReadOnly, Password:=Password)
        
    Else
        MsgBox FileName & Chr(10) & "File / Folder Not Found", 16, "Not Found"
        Set DatabaseOpen = Nothing
    End If
End Function

Function has same commonly used parameters as Workbook. Open method and should require only minor changes to your current code to call it.

example

Code:
Dim wb As Workbook

Set wb = DatabaseOpen(FileName, 0, False, Password)

If wb Is Nothing Then Exit Sub

‘rest of code

Hope Helpful

Dave
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,239
Members
452,621
Latest member
Laura_PinksBTHFT

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