View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Frank Kabel Frank Kabel is offline
external usenet poster
 
Posts: 3,885
Default need logging before closing the workbook.

Hi
put the following code in your workbook module. You may have to change
the workbook/worksheet names and also have to adapt the specific
logging ranges to your needs:

-----

Option Explicit

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim log_wbk As Workbook
Dim log_wks As Worksheet
Dim last_log_row As Long
Dim path As String
Dim log_filename As String
Dim source_wbk As Workbook
Dim source_wks As Worksheet

'Initialization
Application.ScreenUpdating = False
path = "D:\Temp\" 'change this
log_filename = "logging.xls" 'change this
Set source_wbk = ActiveWorkbook
Set source_wks = source_wbk.Worksheets("Files") 'change this

'check if logging workbook is open / if not open it
On Error Resume Next
Set log_wbk = Workbooks(log_filename)
On Error GoTo 0
If log_wbk Is Nothing Then
Workbooks.Open filename:=path & log_filename
Set log_wbk = Workbooks(log_filename)
End If
Set log_wks = log_wbk.Worksheets("sheet1") 'change this

'get last used row
last_log_row = log_wks.Cells(Rows.Count, "A").End(xlUp).row

'log data - change to your needs
With log_wks
.Cells(last_log_row + 1, 1).Value = Application.UserName
.Cells(last_log_row + 1, 2).Value = Format(Now, "MM/DD/YYYY
hh:mm:ss")
.Cells(last_log_row + 1, 3).Value = source_wks.Range("A1").Value
.Cells(last_log_row + 1, 4).Value = source_wks.Range("B1").Value
.Cells(last_log_row + 1, 5).Value = source_wks.Range("C1").Value
End With


' save the changes
Application.DisplayAlerts = True
log_wbk.Save
log_wbk.Close
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub


------


--
Regards
Frank Kabel
Frankfurt, Germany

Martyn wrote:
I have workbook named "test.xls" which has various sheets.
The workbook is not protected and can be accessed by any
user on the network. I need to log spesific cell information
on a seperate xl workbook automatically (log.xls) without
any indication of the process each and every time before
the "test.xls" workbook is closed.
Here are the columns of "test.xls" that needs to be logged:
Sheet: 14 Cell: A4
Sheet: 14 Cell: A10
Sheet: 15 Cell: A9
Sheet: 12 Cell: A9
---------------------------
How should I code?
TIA