VBA获取代码使用者IP+姓名+电脑编号+使用时间

应用背景:比如说你开发了一个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函数时,就会顺带执行本条代码。

以上

以上

以上

以上

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值