看到excel中呈现了很多表的结构,看到VBA可以编写代码自动生成表结构于是就心动了,幻想如果创表能够自动生成脚步就很棒了。每次看着excel中的表字段,精度,注释。用VBA编写好标准化的代码,可以提高工作效率,发挥excel的开发功能。
打开excel vba编程工具,在ThisWorkbook中添加如下代码:
(该代码是用于调用之前写好的VBA代码所用,类似Java用到方法的定义)
Private Sub Workbook_BeforeClose(Cancel As Boolean)
'关闭工作簿前,删除新创建的工具条先
Dim bar_name As String
bar_name = "HRBSJ"
Application.CommandBars(bar_name).Delete
On Error GoTo Exception
Exit Sub
Exception:
End Sub
Private Sub Workbook_Open()
'打开工作簿时创建工具条
Dim bar_name As String
Dim new_bar As Office.CommandBar
bar_name = "HRBSJ"
Set new_bar = Application.CommandBars.Add(bar_name)
new_bar.Visible = True
new_bar.Position = msoBarLeft
With new_bar.Controls.Add(Type:=msoControlButton, before:=1)
.BeginGroup = True
.Caption = "生成建表脚本"
.TooltipText = "生成建表脚本"
.Style = msoButtonCaption
.OnAction = "Create_TS_Table_Script"
End With
On Error GoTo Exception
Exit Sub
Exception:
End Sub
'关闭工作簿前,删除新创建的工具条先
Dim bar_name As String
bar_name = "HRBSJ"
Application.CommandBars(bar_name).Delete
On Error GoTo Exception
Exit Sub
Exception:
End Sub
Private Sub Workbook_Open()
'打开工作簿时创建工具条
Dim bar_name As String
Dim new_bar As Office.CommandBar
bar_name = "HRBSJ"
Set new_bar = Application.CommandBars.Add(bar_name)
new_bar.Visible = True
new_bar.Position = msoBarLeft
With new_bar.Controls.Add(Type:=msoControlButton, before:=1)
.BeginGroup = True
.Caption = "生成建表脚本"
.TooltipText = "生成建表脚本"
.Style = msoButtonCaption
.OnAction = "Create_TS_Table_Script"
End With
On Error GoTo Exception
Exit Sub
Exception:
End Sub