VBS入门-基础语法

Alt+F11 进入VB编辑器

语言

模块声明

Sub name()'name可以是中文
  '内容
End Sub  

变量声明

Dim para1, para2, para3 '直接声明
Dim sPara As sType '带类型声明
Dim para4 As workbook, para5 As String
Dim G As Long

选择结构

With
  '内容
End With

If condition Then 
  '内容
Else
  '内容
End If

If cA And cb And cC Then

End If

相等:=
不相等:<>

'举个例子:遍历每个Sheet把表粘贴成一个大表的语句,使用For Next With End With语句
 
With Workbooks(1).ActiveSheet
	For G = 1 To Sheets.Count
	Wb.Sheets(G).UsedRange.Copy.Cells(.Range("B65536").End(xlUp).Row + 1, 1)
Next
WbN = WbN & Chr(13) & Wb.Name
Wb.Close False

循环结构

Do While condition 
  '内容
Loop

For i = 0 to 100 
  '内容
Next

输出Log

MsgBox sString'消息框

'Exp:
MsgBox "我是个消息"

选择

'表示A1单元格
Range("A1")
Cells(1,1)
Cells(1,"A")

Cells(1,1).Select'选择一个
Sheets("XXX").Select
Sheets("XXX").Columns.Select   
Workbooks("工作簿1.xls")
'值
Range("A1")
Range("B1")=Range("A1").Value

'exp:删除相同行内容'
Sub abc()
    Dim aa, bb, cc, dd, ee, ff
    Dim arr, arr2
    arr = Array("a", "b", "c", "d", "e", "f")
    arr2 = Array(aa, bb, cc, dd, ee, ff)
    Dim different
    For i = 0 To 5
       arr2(i) = Cells(2, arr(i))
    Next
    For i = 3 To 30
        different = False
        For j = 0 To 5
            cur = Cells(i, arr(j))
            befor = arr2(j)
            If cur <> befor Then
                different = True
                Exit For
            End If
        Next
        If different Then
            For j = 0 To 5
                 arr2(j) = Cells(i, arr(j))
            Next
        Else
            Cells(i, "k") = "="
            For j = 0 To 5
                 Cells(i, arr(j)) = ""
            Next
        End If
    Next
End Sub
'计算插值'
Sub generateOffset()
        svalue = 0
        evalue = 0
        num = 0
        startRow = 0
        
    For i = 4 To 448
        Dim curValue
        curValue = Cells(i, "b")
        num = num + 1
        If curValue Then
            If svalue = 0 Then
                startRow = i
                svalue = curValue
            Else
                evalue = curValue
                num = num - 1
                offValue = (evalue - svalue) / num
                For j = 1 To num - 1
                    Cells(startRow + j, "b") = svalue + j * offValue
                Next
                svalue = evalue
                evalue = 0
                num = 1
                 startRow = i
            End If
        End If

    Next
End Sub

参考

https://www.cnblogs.com/wzh313/articles/9737573.html
http://www.seoxiehui.cn/article-20656-1.html

评论