You are on page 1of 1

Private Sub CommandButton1_Click()

Application.ScreenUpdating = False
If IsFileOpen("c:\book1.xlsx") Then
MsgBox "Maaf, file book1.xlsx sedang dibuka, silahkan tutup file terlebih dahulu.."
Exit Sub
End If

Workbooks.Open FileName:="c:\book1.xlsx"

With Worksheets("Sheet1")
BarisTerakhir = .Cells(.Rows.Count, 1).End(xlUp).Row
For i = 1 To 3
.Cells(BarisTerakhir + 1, i).Value = Me.Cells(2, i).Value
Next i
End With

Workbooks("book1.xlsx").Save
Workbooks("book1.xlsx").Close

Application.ScreenUpdating = True
End Sub

6. Jangan lupa, copy function berikut dibawah commandbutton1 click event. Function
ini berfungsi untuk memeriksa, apakah file telah dibuka atau belum.

Function IsFileOpen(FileName As String)


Dim iFilenum As Long
Dim iErr As Long

On Error Resume Next


iFilenum = FreeFile()
Open FileName For Input Lock Read As #iFilenum
Close iFilenum
iErr = Err
On Error GoTo 0

Select Case iErr


Case 0: IsFileOpen = False
Case 70: IsFileOpen = True
Case Else: Error iErr
End Select

End Function

You might also like