VBA基础知识

服务器

  'VBA的常见数据类型

  '整形 integer 简写(%)

  '单精度 single 简写(!)

  '双精度 double 简写(#)

  '长整型 long 简写(&)

  '字符型 string 简写($)

  '货币型 currency 简写(@)

  'exit for 表示退出for循环,相当于php的break;

  'goto line 100 跳转到100标记的地方

  'on error resume next 当出现错误的时候继续执行,就是自动忽略错误

  'with 当某个对象执行一系列语句时,不用重复指出对象名;

  'iif的作用相当于php里的三元运算

  'vba一句话的结束符是':'冒号,不过比较少用,一般一句话写一行就不需要了;

  '只有在2句话要写在一行时才有

  ''''''''''''''''''''''''''''''''''''''''''

  ''''vba颜色的表示方法,方法1和2是color,而3是colorindex

  '[a1].Interior.Color = &H2ACB2A ''''方法1,用16进制的方法

  '[a1].interior.color = vbred ''''方法2,用vb颜色常数

  '[a1].interior.colorindex = 4 ''''方法3,用数字表示只有1-56

  '''''''''''''''''''''''''''''''''''''''''''''''''''''''

  'Sub dd()

  ' a = Range("a10").Address

  ' b = Range("a10").Parent.Name

  ' Range("a10") = "xxxxx"

  'End Sub

  '这种写法比较麻烦,可以采用下面的方法

  'Sub xx()

  ' With Range("a11")

  ' a = .Address

  ' b = .Parent.Name

  ' .Value = "wwwww"

  ' End With

  'End Sub

  ' MsgBox "xxxxx" ''''msgbox的作用相当于JS里的alert

  'End Sub

  'Function bianl(s As Integer) As Double

  ' Dim a% '表示变量a是整型

  ' a = s * 22

  ' bianl = a

  'End Function

  'Function hs(dyg As Range) As String

  ' 'range是单元格类型

  'End Function

  'Function zhang() As Double 'for循环的方法

  ' For i = 1 To 5 Step 1

  ' zhang = zhang + 3

  ' Next

  'End Function

  Function countStr(s$, x$) As String '统计各个等级有几个,并合成nA、nB、nC、nD的形式,如果系数是0不显示

   Dim a%, str$

   a = Len(s) - Len(Replace(s, x, ""))

   If a <> 0 Then '系数为0的不要

   str = a & x

   End If

   countStr = str

  End Function

  '最后进行合并nAnBnCnD的形式

  Function getGrade(ParamArray a()) '参数可以是任意个

   Dim i%, tmp As Range, s$

   For i = LBound(a) To UBound(a)

   For Each tmp In a(i)

   s = s + IIf(i = 0, yw(tmp.Value), IIf(i = 1 Or i = 2, sx(tmp.Value), qt(tmp.Value)))

   Next

   Next

   getGrade = countStr(s, "A") + countStr(s, "B") + countStr(s, "C") + countStr(s, "D")

  End Function

  Function countNum(s$, x$) As Integer '统计各个等级有几个,并合成等级分

   Dim a%, num%

   a = Len(s) - Len(Replace(s, x, ""))

   If a <> 0 Then '系数为0的不要

   num = IIf(x = "A", 10, IIf(x = "B", 1, IIf(x = "D", -100, 0))) '类似于php的三元运算

   End If

   countNum = a * num

  End Function

  Function getGF(ParamArray a()) '最后进行合并总的等级分

   Dim i%, tmp As Range, s$

   For i = LBound(a) To UBound(a)

   For Each tmp In a(i)

   s = s + IIf(i = 0, yw(tmp.Value), IIf(i = 1 Or i = 2, sx(tmp.Value), qt(tmp.Value)))

   Next

   Next

   getGF = countNum(s, "A") + countNum(s, "B") + countNum(s, "C") + countNum(s, "D")

  End Function

  '''''Sub shows()

  '''''

  '''''Dim arr(1 To 7)

  '''''For Each my In [a1:a7]

  ''''' n = n + 1

  ''''' arr(n) = my

  '''''Next

  '''''MsgBox arr(3)

  '''''

  '''''End Sub

  '

  'Function shows(ParamArray a()) As Integer ''''for each循环

  ' Dim i%, s%

  ' For i = LBound(a) To UBound(a)

  ' For Each t In a(i)

  ' s = s + t

  ' Next

  ' Next

  ' shows = s

  'End Function

  Sub wbs()

  ' MsgBox Workbooks().Parent '''''工作簿对象

  ' MsgBox Workbooks(1).Name '''''目前打开的第一个工作簿的名称

  ' MsgBox Workbooks().Count '''''目前打开的工作簿有几个

  '

  ' MsgBox Worksheets(3).Name ''''工作表的名字

  ' MsgBox Sheets(3).Name ''''工作表的名字

  [a2].Resize(3, 3).Select

  End Sub

  '''''变量的类型声明不声明都是可以的,但是最好声明,只要是可以避免有的版本不兼容''''''

  '''''但是如果在模块的前面加上一句代码Option explicit,声明变量就必须有类型

  'Function zhang(x As Range) As Double

  ' zhang = Val(x) + 1 ''''''单元格的值加1

  'End Function

  'apace(n) 函数返回n个空格

  'string(n,s)函数返回n个s字符串里的第一个字符

  'len()函数是技术字符串的长度

  'Format(expression[,format[,firstdayofweek[,firstweekofyear]]])格式化字符串

  ' strOut = Format("8888888", "(@@@)&&&-&&&&") '返回( )888-8888

  'replace替换字符串

  Sub zhang()

  ' Dim MyString

  ' MyString = String(5, "2") ' 返回 "*****"

  ' MsgBox MyString

  ' Dim s$

  ' s = "xxxwwwww"

  ' MsgBox Len(s)

  ' Dim str$

  ' str = "abcde"

  ' MsgBox Replace(str, "c", "p")

  '''''单元格的表示用range("b5")或者用cells(5,2)是一样的,哪一种写法都没关系''''

  ''''复制单元格''''

  'Range("a1").Copy Destination:=Range("a3") ''''a3单元格复制a1单元格的值

  'Range("a1").Copy Range("a10") ''''这两种写法都一样

  '''''背景颜色'''''

  'Range("a1").Interior.ColorIndex = 43 ''''单元格的背景颜色用1-56表示

  ''''''字体的颜色'''''''

  'Cells(1, 1).Font.ColorIndex = 2

  '' Dim n%, cel As Range

  '' For Each cel In [a1:a50]

  '' n = n + 1

  '' cel.Value = n

  '' Next

  ''Dim i%

  ''

  ''For i = 1 To 100

  '' Cells(i, 3) = i

  '' 'Range("c" & i) = i '''''这2种方法一样

  ''Next

  'Dim i%

  'For i = 1 To 100

  ' If i Mod 5 = 0 Then ''''' vba的求余用mod '''''

  ' Cells(i, 3) = ">>>><<<<"

  ' Else: Cells(i, 3) = i

  ' End If

  'Next

  'Dim i%

  'For i = 1 To 50

  ' Cells(i, 5) = IIf(i Mod 5 = 0, "<<<>>>", i)

  'Next

  'Dim i%, arr(1 To 5) As Integer '''''循环向数组添加元素

  'For i = 1 To 5

  ' arr(i) = i

  'Next

  '

  'For i = 1 To 5

  ' MsgBox arr(i)

  'Next

  'Dim i%, j%

  'For i = 1 To 100

  ' For j = 1 To 5

  ' Cells(i, j) = i

  ' Next

  'Next

  'Dim i%, j%

  'For i = 1 To 100

  ' For j = 1 To 5

  ' Cells(i, 6) = Cells(i, 6) + Cells(i, j) '''''第6列单元格的值等于同一行前5列的和

  ' Next

  'Next

   'Range("g5") = "=SUMIF(a10:f10,10,c10:f10)" '''''直接使用excel自带的函数

  '' Dim i%

  '' For i = 1 To 100

  '' Debug.Print i '''''debug.print 是把正在执行的过程显示在立即窗口

  '' Next

  ''''for each ... in进行的是对象的循环''''''''

  ''do...loop循环

  Dim i%, sr$

  100:

   sr = Application.InputBox("请输入数字", "输入提示")

   If Len(sr) = 0 Or sr = 5 Then GoTo 100

  End Sub

标签: 服务器