怎样使用VBA在功能区建立自己的工具按钮?
1、首先在开发工具中打开VBA编辑器

2、在单元格区域当中输入一些内容作为例子

3、在VBA编辑器中插入模块

4、在模块当中输入如下代码,然后运行
Private Declare Function GetTickCount Lib "kernel32" () As Long
'电脑使用时间的声明
Sub auto_open()
'添加第一个菜单按钮
With Application.CommandBars(1).Controls.Add(msoControlButton, 1, , , True)
.Caption="显示磁盘空间(&Space)"
.OnAction="显示磁盘卷标及空间"
.Style=msoButtonIconAndCaption
.FaceId=1185
End With
'添加第二个菜单按钮
With Application.CommandBars(1).Controls.Add(msoControlButton, 1, , , True)
.Caption="电脑使用时间(&Times)"
.OnAction="电脑使用时间"
.Style=msoButtonIconAndCaption
.FaceId=487
End With
'添加第三个菜单按钮
With Application.CommandBars(1).Controls.Add(msoControlButton, 1, , , True)
.Caption="查电脑IP(&IP)"
.OnAction="查电脑IP"
.Style=msoButtonIconAndCaption
.FaceId=481
End With
End Sub
Sub auto_close()
Application.CommandBars(1).Reset
End Sub
Sub电脑使用时间()
MsgBox "您的电脑已使用:" & Chr(10) & Round(GetTickCount / 1000 / 60, 0) &
"分钟", vbOKOnly+64, "请注意休息"
End Sub
Sub显示磁盘卷标及空间()
On Error Resume Next
Dim磁盘, 磁盘与卷标As String, 卷标
Set卷标=CreateObject("Scripting.FileSystemObject").Drives
For Each磁盘In卷标
Set fs=CreateObject("Scripting.FileSystemObject")
Set drive=fs.GetDrive(fs.GetDriveName(磁盘 & ":"))
MsgBox "磁盘" & UCase(磁盘) & Chr(10) & "卷标名:" & drive.VolumeName & Chr(10) _
& "剩余空间:" & FormatNumber(drive.FreeSpace / 1024 / 1024, 0) & "
MB", 64, "磁盘空间"
Next
End Sub
Sub查电脑IP()
Dim OpSysSet, OpSys
Set OpSysSet=GetObject _
("winmgmts:{impersonationLevel=impersonate}//" & ComputerName). _
ExecQuery("SELECT index, IPAddress FROM
Win32_NetworkAdapterConfiguration")
For Each OpSys In OpSysSet
If TypeName(OpSys.IPAddress) <> "Null" Then
For Each IP In OpSys.IPAddress
MsgBox IP, 64, "IP地址"
Next
End If
Next
End Sub

5、用快捷键F5运行程序,或者重启工作簿,【加载项】功能区出现三个菜单按钮,如图

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
阅读量:93
阅读量:179
阅读量:141
阅读量:132
阅读量:44