N
nicole0904
I have several workbooks that several users each have a personalized
copy of. Each workbook opens the file txt.dat(used a simple name for
testing) and reads in the number and populates cell B3 with that
number. It then increments the number by one deletes the file and
re-creates the file writing the new number.
I need all of the workbooks to reference this file to get a unique
number from 1-99999. Is there a way to do this with out getting
errors?
With this code if 2 users run the macro at the same time it locks the
file.
Code:
--------------------
Sub GetGNumb()
Dim gnum As Long
On Error GoTo ErrHandler1
Open "x:\txt.dat" For Input As #1
Input #1, gnum
Range("B3").Activate
ActiveCell.FormulaR1C1 = gnum
Close #1
gnum = gnum + 1
If gnum > 99999 Then
gnum = 1
End If
On Error GoTo ErrHandler2
Kill "x:\txt.dat"
On Error GoTo ErrHandler3
Open "x:\txt.dat" For Append As #1
Write #1, gnum
Close #1
Exit Sub
ErrHandler1:
MsgBox ("1")
Close #1
Exit Sub
ErrHandler2:
MsgBox ("2")
Close #1
Exit Sub
ErrHandler3:
MsgBox ("3")
Close #1
End Sub
copy of. Each workbook opens the file txt.dat(used a simple name for
testing) and reads in the number and populates cell B3 with that
number. It then increments the number by one deletes the file and
re-creates the file writing the new number.
I need all of the workbooks to reference this file to get a unique
number from 1-99999. Is there a way to do this with out getting
errors?
With this code if 2 users run the macro at the same time it locks the
file.
Code:
--------------------
Sub GetGNumb()
Dim gnum As Long
On Error GoTo ErrHandler1
Open "x:\txt.dat" For Input As #1
Input #1, gnum
Range("B3").Activate
ActiveCell.FormulaR1C1 = gnum
Close #1
gnum = gnum + 1
If gnum > 99999 Then
gnum = 1
End If
On Error GoTo ErrHandler2
Kill "x:\txt.dat"
On Error GoTo ErrHandler3
Open "x:\txt.dat" For Append As #1
Write #1, gnum
Close #1
Exit Sub
ErrHandler1:
MsgBox ("1")
Close #1
Exit Sub
ErrHandler2:
MsgBox ("2")
Close #1
Exit Sub
ErrHandler3:
MsgBox ("3")
Close #1
End Sub