Visual Basic for Applications

简介

语法

  • 不会写法的可以使用录制宏,然后进行代码查看

  • FormulaR1C1是公式输入方法

    • 有中括号是相对于选定单元格的相对偏移量,”-“为向左或向上偏移,正数为右或下偏移。 无中括号为相对于选定单元格的绝对偏移量,没有负数。”R”和”C”对应行和列”
    • 如:C1单元格为”=A1+B1”。Range(“C1”).FormulaR1C1 = “=RC[-2]+RC[-1]”
    • 如:C1单元格为”=A2+E3” Range(“C1”).FormulaR1C1 = “=R[1]C[-2]+R[2]C[2]”
  • Selection.AutoFill Destination:=fillRange, Type:=xlFillDefault 自动填充
    • 此处Selection选中的Range即sourceRange(源),根据sourceRange进行fillRange的填充。此时fillRange必须包含sourceRange。sourceRange中可以有公式等
  • ActiveWindow.SmallScroll Down:=6等是对窗口进行移动,不影响计算
  • 代码必须运行在过程中,如定义一个过程main,此时执行运行-运行子过程
1
2
3
4
5
6
7
8
9
Public Sub main()
MsgBox "hello world..."
With CreateObject("Internetexplorer.application")
.Visible = True
.Navigate "https://www.baidu.com/s?wd=扯乎"
'关闭网页
.Quit
End With
End Sub

VBA配置

  • Excel 2016

    • 开启VBA菜单:文件 - 选项 - 自定义功能区 - 勾选右边开发工具
    • 创建代码窗口:Visual Basic - 视图 - 代码窗口
    • 显示工程管理器:视图 - 工程资源管理器
    • 保存工作簿作为excel 启用宏的工作簿 (* xlsm),下次打开文件则会包含宏代码
    • 让Excel在打开时就自动执行宏代码

      • 在左边project exploer中双击This workbook
      • 出现代码窗口,最上两个下拉菜单,左选workbook,右选open,VBE自动出现code:

        1
        2
        3
        Private Sub Workbook_Open()
        'MsgBox "hello world..."
        End Sub
      • 保存文件并重新打开即可运行宏

  • Access 2016
    • 显示工程管理器:视图 - 工程资源管理器
      • 创建模块:右键工程目录 - 插入 - 模块
      • 代码必须运行在过程中,过程必须写在模块中
  • VBA配置
    • 设置字体:工具 - 选项 - 编辑器格式 - 标准字体 - Consolas (西方)
    • 设置语法检测:工具 - 选项 - 编辑器 - 自动语法检测去勾选。防止编辑时弹框提示语法错误,运行时语法错误会提示

示例

Delat Ct法计算候选基因稳定性(Excel操作)

此算法是临时帮朋友写的,可能与实际算法不符,仅供参考

  • 简介:这是一种常见的算法,叫做Delat Ct法计算候选基因稳定性。最后得到的是每个基因的mean SD值,首先计算两个基因的ΔCt值,再计算其ΔCt值的方差,最后得到该基因与其余每个基因ΔCt值的方差的平均值。
  • excel表格数据如下:
beta-ActinTubulin-alphaEF1AGAPDHTubulin-beta18S rRNA
卵子23.8223.8223.9325.4825.7617.67
受精后22.4723.9422.5123.3123.9113.74
2cell23.2924.7822.8324.2325.0515.35
4cell22.9524.8422.5123.3924.5414.6
8cell21.7523.7721.822.5823.6613.22
16cell21.8224.2122.4923.4523.812.73
32cell21.9224.6222.3323.7421.1213.95
64cell22.9224.8322.1924.3924.915.76
多cell21.6224.0421.2622.6624.0412.63
高囊胚123.0625.0823.0824.4525.0915.17
高囊胚222.9826.2722.2225.0425.7215.55
低囊胚2326.2422.0326.3126.4917.04
原肠胚前期21.2325.462026.4625.8714.12
原肠胚中期20.2324.619.5226.5725.8114.03
原肠胚后期19.5622.7718.526.324.9112.83
神经19.4623.8717.6626.8224.5815.86
肌节18.0322.716.4823.3523.7912.86
器官形成期118.4723.7117.2624.8924.2115.3
器官形成期218.6523.617.8323.9423.7415.92
器官形成期318.1423.2416.1223.6623.8216
破膜17.8222.8917.455.4423.9213.92
仔鱼16.9722.6516.1520.6922.8611.55
  • 效果如下

    效果图

  • VBA处理源码(效率可能较低)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124

    Sub delacCt()
    '
    ' Delac Ct算法计算基因mean SD 宏
    '
    Dim rows As Integer
    Dim columns As Integer
    Dim rangeItem1 As range
    Dim rangeItem2 As range
    Dim targetCellRange1 As range
    Dim targetCellRange2 As range
    Dim isStart As Boolean

    'dataCell为一个Range对象
    Set myRange = Application.InputBox(prompt:="按住Shift选择数据所在区域(包含行标题不包含列标题)", Type:=8)
    'Set myRange = range("B1:D23")

    '获取此区域的总行数和总列数
    rows = myRange.rows.count
    columns = myRange.columns.count

    '循环其中两个基因
    With myRange
    For i = 1 To columns Step 1
    '每一行的方差
    Dim total As Double
    total = 0

    For j = i + 1 To columns Step 1
    '此处.代表myRange(With中)
    Set rangeItem1 = .columns(i)
    Set rangeItem2 = .columns(j)

    Dim cellRow As Integer
    Dim cellColumn As Integer
    cellRow = i * (rows + 3) + 1
    cellColumn = (j - 2) * 4 + 2

    '目标单元格
    Set targetCellRange1 = Worksheets(1).Cells(cellRow, cellColumn)
    Set targetCellRange2 = Worksheets(1).Cells(cellRow, cellColumn + 1)

    '复制并粘贴其中某两个基因
    Call geneCopy(rangeItem1, targetCellRange1)
    Call geneCopy(rangeItem2, targetCellRange2)

    '计算方差
    total = total + geneCalculate(range(targetCellRange1, targetCellRange1.Offset(rows - 1, 0)))
    Next

    ' 计算方差平均值
    If i < columns Then
    'MsgBox total / (columns - i)
    Dim row As Integer
    '方差所在行
    row = targetCellRange1.Offset(rows - 1, 0).row + 1

    Worksheets(1).range("A" & row).Value = "SD"
    Worksheets(1).range("A" & row + 1).Value = "mean SD"
    Worksheets(1).range("B" & row + 1).Value = total / (columns - i)
    End If
    Next
    End With

    'ActiveWorkbook.Save
    End Sub

    Private Sub geneCopy(rangeItem As range, targetCellRange As range)
    '
    ' 复制并粘贴其中某两个基因
    '
    rangeItem.Select
    Selection.Copy

    targetCellRange.Select
    ActiveSheet.Paste
    End Sub

    Private Function geneCalculate(targetRange1 As range) As Double
    '
    ' 对两个基因进行计算
    '
    '求差值
    Call subValue(targetRange1)

    '求方差(targetRange1.Row是获取该单元格得在Sheet中是第几行)
    geneCalculate = varianceValue(range(Worksheets(1).Cells(targetRange1.row + 1, targetRange1.Column + 2), _
    Worksheets(1).Cells(targetRange1.row + targetRange1.rows.count - 1, targetRange1.Column + 2)))

    'MsgBox geneCalculate
    End Function

    Private Sub subValue(rangeItem1 As range)
    '
    ' 求差值
    '
    Dim sourceRange As range
    Dim fillRange As range

    Set sourceRange = Worksheets(1).Cells(rangeItem1.row + 1, rangeItem1.Column + 2)
    sourceRange.Select
    ActiveCell.FormulaR1C1 = "=RC[-2]-RC[-1]"

    Set fillRange = range(Worksheets(1).Cells(rangeItem1.row + 1, rangeItem1.Column + 2), _
    Worksheets(1).Cells(rangeItem1.row + rangeItem1.rows.count - 1, rangeItem1.Column + 2))

    '此处根据选中的sourceRange进行fillRange的填充,fillRange必须包含sourceRange
    Selection.AutoFill Destination:=fillRange, Type:=xlFillDefault
    End Sub

    Private Function varianceValue(range As range) As Double
    '
    ' 求方差
    '
    Dim targetCellRange As range

    Set targetCellRange = Worksheets(1).Cells(range.row + range.rows.count, range.Column)
    targetCellRange.Select

    targetCellRange.FormulaR1C1 = "=SQRT(VAR(R[-22]C[0]:R[-1]C[0]))"

    '返回值
    varianceValue = targetCellRange.Value
    End Function

Http发送数据

1
2
3
4
5
6
7
8
Public Sub Main()
Set myMSXML = CreateObject("Microsoft.XmlHttp")
myMSXML.Open "POST", "http://192.168.17.229:8000/api/v1/biz/login_token", False
myMSXML.setRequestHeader "Content-Type", "application/json"
myMSXML.setRequestHeader "User-Agent", "Firefox 3.6.4"
myMSXML.send "{""username"":""admin"",""password"":""admin""}"
MsgBox myMSXML.responseText
End Sub

ChatGPT开源小程序