如果Excel文件已打开,请显示messagebox和结束表单

时间:2014-04-23 14:19:40

标签: excel forms vba excel-vba

我有一个设置,其中多个用户在表单(“输入”工作簿)中输入数据,将数据复制到另一个工作簿(“数据”工作簿)。如果两个用户试图同时更新数据,我会收到错误消息。 “数据”工作簿保持打开状态,从而阻止其他用户的更新选项。

我想要一个消息框说:“正在使用的工作簿,请再试一次”,然后“数据”工作簿关闭,用户再次从表单发送数据,但我似乎无法让它工作。

有人可以就如何解决这个问题给我一些指示吗?

以下是“输入”工作簿中输入表单的代码:

Private Sub CmdButton_add_Click()

Dim wbk As Workbook
Set wbk = Workbooks.Open("G:\afregsyd\KSC SYD\Salgsregistrering\indtastninger2.xlsx")
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Ark1")

'find first empty row in database
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1

'check for a Name number
If Trim(Me.TextBox_Initialer.Value) = "" Then
Me.TextBox_Initialer.SetFocus
MsgBox "Please complete the form"
Exit Sub
End If

'copy the data to the database
ws.Cells(iRow, 1).Value = Date
ws.Cells(iRow, 2).Value = Me.TextBox_Initialer.Value
ws.Cells(iRow, 3).Value = Me.TextBox_Salg.Value
ws.Cells(iRow, 4).Value = Me.TextBox_Liv.Value
ws.Cells(iRow, 5).Value = Me.TextBox_Bank.Value
ws.Cells(iRow, 6).Value = Me.TextBox_Mersalg.Value

'close Workbook
ActiveWindow.Close SaveChanges:=True

MsgBox "Data tilføjet", vbOKOnly + vbInformation, "Data tilføjet"
'clear the data
Me.TextBox_Salg.Value = ""
Me.TextBox_Liv.Value = ""
Me.TextBox_Bank.Value = ""
Me.TextBox_Mersalg.Value = ""
Me.TextBox_Initialer.SetFocus
End Sub



Private Sub Cmdbutton_close_Click()
Unload Me
End Sub

1 个答案:

答案 0 :(得分:0)

查看/分享工作簿/编辑/检查"允许多个用户同时进行更改。"

有关使用VBA的操作,请参阅http://support.microsoft.com/kb/153058

来自链接的

代码:

Sub Example1()

  ' Test to see if the Read-only attribute was assigned to the file.

  If GetAttr("c:\test.xls") And vbReadOnly Then
    MsgBox "File is Read-only"
  Else
    MsgBox "File is not read-only"
  End If

End Sub