应用背景:比如说你开发了一个excel工具,想统计一下应用实绩。
(在一段时间内,谁在什么时候使用了该工具。)
最后可以统计一年中,该工具一共被使用多少次。
1.先在D盘新建一个excel(user_log.xlsx)。(名字及存储位置自定义)
2.然后使用下面的代码。
Option Explicit
Sub User_Log_Record()
Dim ip_1$, j, strcomputer, objwmi, coliP, IP, computer_name, i
Workbooks.Open "D:\user_log.xlsx"
Windows("user_log.xlsx").Activate
Application.ScreenUpdating = False
j = 1
Do While Cells(j, 1) <> ""
j = j + 1
Loop
Windows("user_log.xlsx").Visible = False
strcomputer = "."
Set objwmi = GetObject("winmgmts:\\" & strcomputer & "\root\cimv2")
Set coliP = objwmi.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
For Each IP In coliP
If Not IsNull(IP.IPAddress) Then
ip_1 = IP.IPAddress(i) 'get the IP
computer_name = Environ("computername") 'get the computer_name
End If
Next
Windows("user_log.xlsx").Activate
Cells(j, 1) = computer_name
Cells(j, 2) = Now()
Cells(j, 3) = ip_1
Cells(j, 4) = Application.UserName
Windows("user_log.xlsx").Visible = True
Workbooks("user_log.xlsx").Close savechanges:=True
Application.ScreenUpdating = True
End Sub
3.即可完成日志log表的自动记入。把这段代码假如到需要执行的sub里面,每次执行sub函数时,就会顺带执行本条代码。
以上
以上
以上
以上