0
点赞
收藏
分享

微信扫一扫

excel VBA编程入门,自定义excel数据库模板生成sql语句


文章目录

  • ​​VBA基础​​
  • ​​一.了解VBA​​
  • ​​1.进入vba​​
  • ​​2.认识宏​​
  • ​​二. VBA编程​​
  • ​​1.hello world​​
  • ​​2.调出立即窗口和本地窗口​​
  • ​​3.debug显示​​
  • ​​4.注释​​
  • ​​5.数据类型​​
  • ​​5.变量的生命周期和定义域​​
  • ​​6.判断语句​​
  • ​​7.不等于<> , switch case 条件判断​​
  • ​​8.循环​​
  • ​​do while​​
  • ​​退出循环的语句 exit for​​
  • ​​退出循环的语句 exit do​​
  • ​​9.数组​​
  • ​​10.二维数组​​
  • ​​11.操作单元格​​
  • ​​实战​​
  • ​​自定义vba脚本生成sql​​
  • ​​见证奇迹的时刻(如何使用脚本)​​

最近公司有个需求,提前让数据库表结构在excel模板中设计,然后再建对应的数据库DB。字段少好说,无脑录入即可,但是,我遇到个100多个字段的,实在忍不了,最终入门了VBA编程,自己写脚本生成了sql语句。减少了需要无用重复劳动。

VBA基础

首先学习下vba的基础。

一.了解VBA

1.进入vba

alt + f11

文件 - 选项 - 自定义功能区 - 勾选开发工具

2.认识宏

  • 录制宏
  • 使用相对应用录制宏

二. VBA编程

1.hello world

双击某个sheet页, 把鼠标放在 窗口里面,点击工具栏的插入,选择过程,随便起个过程名,比如 class就生成了代码:

Public Sub class()
End Sub

写个hello world程序:

Sub class()
Dim name
name = "Hello World"
name = "胡老师好"
MsgBox name
End Sub

excel VBA编程入门,自定义excel数据库模板生成sql语句_定义域

2.调出立即窗口和本地窗口

在工具栏中选择 选择立即窗口,和本地窗口。

excel VBA编程入门,自定义excel数据库模板生成sql语句_sql_02

3.debug显示

Sub class()
Dim name
name = "Hello World"
Debug.Print name
name = "胡老师好"
MsgBox name
Debug.Print name
End Sub

excel VBA编程入门,自定义excel数据库模板生成sql语句_字段_03

4.注释

注释有2种形式,一种是rem。一种是 ’ (单引号)

excel VBA编程入门,自定义excel数据库模板生成sql语句_sql_04

5.数据类型

variant 代表任意类型

single double decimal 代表 小数、

rem 代表注释关键字

Const 常量关键字

excel VBA编程入门,自定义excel数据库模板生成sql语句_定义域_05


excel VBA编程入门,自定义excel数据库模板生成sql语句_excel编程_06

Sub class()
Rem variant是任意类型
Dim name As Variant
name = "Hello World"
Rem debug
Debug.Print name
name = "胡老师好"
Debug.Print name
Rem 定义常量
Const num As Integer = 123
Debug.Print num

End Sub

5.变量的生命周期和定义域

  • 定义域: public 和 private
  • excel VBA编程入门,自定义excel数据库模板生成sql语句_sql_07

  • static 是静态变量

Sub class1()
Dim x As Integer
Rem static 是静态变量
Static y As Integer
x = x + 1
y = y + 1
Debug.Print "x=" & x
Debug.Print "y=" & y
End Sub

6.判断语句

Sub class3()
Dim number As Integer
number = 1
If number > 100 Then
Debug.Print "优秀"
ElseIf number > 95 Then
Debug.Print "良好"
Else
Debug.Print "一般"
End If
End Sub

7.不等于<> , switch case 条件判断

  • <> 不等于

Sub class3()
Dim number As String
number = "匹配"
If number > "匹配" Then
Debug.Print "优秀"
ElseIf number <> "匹配" Then
Debug.Print "良好"
End If
End Sub

  • switch case

Sub class4()
Dim number As String
number = "匹配"
Select Case number
Case "匹配"
Debug.Print "匹配"
Case "不匹配"
Debug.Print "不匹配"
End Select

End Sub

  • boolean类型的使用

Sub class5()
Dim count As Integer
Dim if_f As Boolean
count = 11
if_f = (count = 10)
MsgBox if_f
End Sub

8.循环

  • 最简单的循环

Sub class()
Rem
Dim count As Integer
For count = 1 To 10
Debug.Print count
Next
Debug.Print "count 循环结束之后的值是 " & count
End Sub

excel VBA编程入门,自定义excel数据库模板生成sql语句_sql_08

  • 循环控制单元格属性

Sub class()
Rem
Dim count As Integer
For count = 2 To 10
If count Mod 2 = 0 Then
Rem rang
Rem Interior
Range("T" & count).Interior.ColorIndex = 1
Else
Range("T" & count).Interior.ColorIndex = 3
End If
Next
Debug.Print "count 循环结束之后的值是 " & count
End Sub

excel VBA编程入门,自定义excel数据库模板生成sql语句_excel编程_09

  • 循环控制单元格求和

Sub class()
Rem
Dim COUNT As Integer
Dim score As Double
For COUNT = 2 To 20
Rem cells 2
Cells(COUNT, 8) = "=sum(rc[-1]:rc[-6])"
Next
End Sub

excel VBA编程入门,自定义excel数据库模板生成sql语句_sql_10

  • 循环单元格操作+if

Sub class()
Rem 演示循环
Dim COUNT As Integer
Dim score As Double
For COUNT = 2 To 20
Rem cells的参数 第一个参数代表横行,第2个参数代表竖行
Cells(COUNT, 8) = "=sum(rc[-1]:rc[-6])"
score = Cells(COUNT, 8)
If score > 700 Then
Cells(COUNT, 9) = "秀儿"
ElseIf score > 650 Then
Cells(COUNT, 9) = "良好"
Else
Cells(COUNT, 9) = "小垃圾"
End If
Next
End Sub

excel VBA编程入门,自定义excel数据库模板生成sql语句_定义域_11

do while

Sub class()
Rem do while
Dim count As Integer
count = 20
Do While count > 10
Debug.Print count
count = count - 1
Debug.Print count
Loop

Rem do .. loop
Do
Loop While count > 10

End Sub

excel VBA编程入门,自定义excel数据库模板生成sql语句_定义域_12

退出循环的语句 exit for

使用for循环的时候退出用 exit for

Sub class1()
Dim count As Integer
For count = 1 To 10
If count = 5 Then
Debug.Print "count 退出循环的值是: " & count
Exit For
End If
Debug.Print count
Next
End Sub

excel VBA编程入门,自定义excel数据库模板生成sql语句_excel编程_13

退出循环的语句 exit do

使用fo while循环的时候退出用 exit do

Sub class1()
Dim count As Integer
Do While True
count = count + 1
If count > 5 Then
Debug.Print "此时退出循环的值是: " & count
Exit Do
End If
Loop
End Sub

excel VBA编程入门,自定义excel数据库模板生成sql语句_字段_14

9.数组

Sub class()
Dim arr(2) As Variant
Dim i As Integer
arr(0) = "小明"
arr(1) = 2
arr(2) = True
For i = 0 To 2
Debug.Print arr(i)
Next
End Sub

excel VBA编程入门,自定义excel数据库模板生成sql语句_sql_15

  • 下标越界

Sub class()
' Const i As Integer = 10
' Dim arr(i) As Variant
Rem 可以指明数组的范围奥 起始开始限制了也是下标越界
Dim arr(2 To 5) As Variant
arr(1) = 1
Debug.Print arr(1)

End Sub

excel VBA编程入门,自定义excel数据库模板生成sql语句_定义域_16

10.二维数组

Sub class()
Dim arr(2 To 5, 3 To 6) As Variant
arr(2, 3) = 1
Debug.Print arr(2, 3)
End Sub

excel VBA编程入门,自定义excel数据库模板生成sql语句_vba_17

  • 遍历循环

Sub class()
Dim arr(2 To 5, 3 To 6) As Variant
arr(2, 3) = 1
'Debug.Print arr(2, 3)
For x = 2 To 5
For y = 3 To 6
Debug.Print arr(2, 3)
Next
Next

End Sub

excel VBA编程入门,自定义excel数据库模板生成sql语句_sql_18

11.操作单元格

2中方式,一种是range,一种是cells

range

excel VBA编程入门,自定义excel数据库模板生成sql语句_excel编程_19

cells

excel VBA编程入门,自定义excel数据库模板生成sql语句_定义域_20

实战

现在掌握了上面的基础知识,基本上可以满足我们最开始的需求
解决思路: ​​​循环方式获取单元格中的内容,拼接成 sql 的创建脚本语句即可​​。

自定义vba脚本生成sql

  • 原来的excel模板内容:
  • excel VBA编程入门,自定义excel数据库模板生成sql语句_字段_21

  • 目标:输出sql创建表的语句:
  • excel VBA编程入门,自定义excel数据库模板生成sql语句_excel编程_22

  • 具体实现的脚本:

Public Sub class()
Rem row
Const startRow As Integer = 13
Rem row
Const endRow As Integer = 28
Rem
Dim tableName As String
tableName = Range("E" & 6)
Rem
Dim primaryKey As String
primaryKey = Range("F" & 13)
Rem
Dim tableComment As String
tableComment = Range("E" & 7)
Rem
Dim filedMetaNo As String
filedMetaNo = "F"
Rem
Dim commentMetaNo As String
commentMetaNo = "C"
Rem
Dim comment2MetaNo As String
comment2MetaNo = "U"
Rem
Dim typeMetaNo As String
typeMetaNo = "G"
Rem
Dim lengthMetaNo As String
lengthMetaNo = "H"

Rem sql
Dim sqlStr As String
sqlStr = "CREATE TABLE " & Range("E" & 6) & Chr(13)
sqlStr = sqlStr & "(" & Chr(13)
For count = startRow To endRow
Rem
sqlStr = sqlStr & Replace(Range(filedMetaNo & count).Text, " ", "")
Rem ()
sqlStr = sqlStr & " " & Range(typeMetaNo & count)
If IsEmpty(Range(lengthMetaNo & count)) = False Then
sqlStr = sqlStr & "(" & Range(lengthMetaNo & count) & ") "
End If
Rem NOT NULL COMMENT
If primaryKey = Range(filedMetaNo & count) Then
sqlStr = sqlStr & " NOT NULL COMMENT "
Else
Rem DEFAULT NULL COMMENT '字段名称注释(字段备注)'
sqlStr = sqlStr & " DEFAULT NULL COMMENT "
End If
sqlStr = sqlStr & "'" & Range(commentMetaNo & count)
If IsEmpty(Range(comment2MetaNo & count)) = False Then
sqlStr = sqlStr & "(" & Range(comment2MetaNo & count) & ")"
End If
sqlStr = sqlStr & "'"
sqlStr = sqlStr & "," & Chr(13)
Next
sqlStr = sqlStr & "PRIMARY KEY (" & primaryKey & ")" & Chr(13)
sqlStr = sqlStr & ")ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='" & tableComment & "'" & Chr(13)
Debug.Print sqlStr
End Sub

显示结果:

excel VBA编程入门,自定义excel数据库模板生成sql语句_定义域_23

在navicat中运行

excel VBA编程入门,自定义excel数据库模板生成sql语句_excel编程_24

显示已经把mysql数据库的表结构导入数据库了。

见证奇迹的时刻(如何使用脚本)

脚本只需要改一个地方:

excel VBA编程入门,自定义excel数据库模板生成sql语句_定义域_25


对应这里的行号:

excel VBA编程入门,自定义excel数据库模板生成sql语句_excel编程_26


即可。

excel VBA编程入门,自定义excel数据库模板生成sql语句_excel编程_27

以上即是用脚本生成的4张表效果图。

个人微信公众号:

搜索: 怒放de每一天

不定时推送相关文章,期待和大家一起成长!!

excel VBA编程入门,自定义excel数据库模板生成sql语句_字段_28


举报

相关推荐

0 条评论