2006年8月14日星期一

Inclusion Constant数据处理脚本0.01

懒懒洋洋的做了一点工作,发现自己已经完全记不得VB的任何东西了。不过总算写出了一点东西——事实上,大多数是用Excel录制的。这个脚本只能处理一种数据,所以还没有实用价值——花瓶一个。不过是因为好玩才贴出来看看的。

这个脚本包含了两个宏过程,后面的将把两个过程合并,所有的数据录入均采用输入框形式。目前,数据处理需要先执行初始化宏,然后把数据手工输入或者粘贴工作表指定区域,然后执行数据处理宏,才能完成。最后因为目前还不了解如何获得回归方程的值,所以还需要手工计算IC(Inclusion Constant)。

后面的目标有:
1. 解决处理的数据组数,目前的脚本仅仅是针对处理8组吸光值数据而写的,后面将把这个8变成一个变量;
2. 完全重写数据处理部分内容,增加灵活性,目前使用了自动填充功能,后面将使用循环结构代替自动填充;//计划明天完成。
3. 重新调整数据输出格式,目前的数据输出格式的可扩展性太差;//计划明天完成。
4. 增加两种数据处理方法,计算IC有3种方法,目前的这种方法是很折中的方法,性价比耶最高,还有两种方法,其中一种更宽松,另一种更苛刻;
5. 增加错误处理,目前的脚本中没有任何处理异常错误的代码,后面将增加错误处理代码,改善用户体验;
6. 自动计算IC;
7. 让曲线拟合部分支持在任意工作表中执行,目前只支持sheet1;//计划明天完成。
8. (这个目标比较远)用Perl + gnuplot 改写所有数据处理脚本。(也许用Matlab写可能更简单些,完成了上面的7个目标之后再作打算。)

Attribute VB_Name = "模块1"
Sub Init_Input()
'
' Init_Input Macro
' 初始化表格,等待用户输入吸光值(A0-An)、环糊精质量(mCD)和环糊精平均分子量(MCD)
'

'
    Range("A1").Select
    ActiveCell.FormulaR1C1 = "A0->An"
    Range("A2").Select
    ActiveCell.FormulaR1C1 = "mCD"
    Range("A3").Select
    ActiveCell.FormulaR1C1 = "MCD"
    Range("A4").Select
End Sub

Sub Data_Proc_All()

    Dim m_cd As String   '定义CD物质的质量,即称量质量
    Dim mw_cd As String  '定义CD的分子量,即摩尔质量
    Dim n As Double      '定义CD物质的量,即质量/分子量

    m_cd = InputBox("请输入环糊精质量:", "环糊精质量")
    mw_cd = InputBox("请输入环糊精平均分子量:", "环糊精平均分子量")
   
    n = (m_cd / mw_cd)
       
    Range("B2").Select
    ActiveCell.FormulaR1C1 = m_cd
    Range("B3").Select
    ActiveCell.FormulaR1C1 = mw_cd
    Range("A4").Select
    ActiveCell.FormulaR1C1 = "CD物质的量"
    Range("B4").Select
    ActiveCell.FormulaR1C1 = n
'
' Init_format Macro
' 初始化表格格式,创建样式一致的数据区域。
'

'
    Range("B5").Select
    ActiveCell.FormulaR1C1 = "A"
    Range("C5").Select
    ActiveCell.FormulaR1C1 = "cd(mol/l)"
    Range("D5").Select
    ActiveCell.FormulaR1C1 = "1/cd"
    Range("E5").Select
    ActiveCell.FormulaR1C1 = "1/A-A0"
    Range("F5").Select
    ActiveCell.FormulaR1C1 = "A-A0"
    Range("G5").Select
    ActiveCell.FormulaR1C1 = "cd/A-A0"
    Range("A6").Select
    ActiveCell.FormulaR1C1 = "0"
    Selection.AutoFill Destination:=Range("A6:A14"), Type:=xlFillSeries
    Range("B1:J1").Select
        Selection.Copy
    Range("B6").Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=True
    Range("B15").Select

'
' data process Macro
' 数据处理部分。暂时不能推广。
' 比色皿初始注入体积2.5mL,每针100uL,打8针
' 这一段代码将完全被我的自己的代码所替换,以支持变量的使用
'

'
    Range("C7").Select
    ActiveCell.FormulaR1C1 = _
        "=(0.1551/1135) /(10*10^-3)*(100*10^-6)*RC[-2]/((2.5+(100*10^-3)*RC[-2])*10^-3)" 
'暂时还不能在这个公式中插入变量。
    Range("D7").Select
    ActiveCell.FormulaR1C1 = "=1/RC[-1]"
    Range("F7").Select
    ActiveCell.FormulaR1C1 = "=RC[-4]-0.053"
    Range("E7").Select
    ActiveCell.FormulaR1C1 = "=1/RC[1]"
    Range("G7").Select
    ActiveCell.FormulaR1C1 = "=RC[-4]*RC[-2]"
    Range("C7").Select
    Selection.AutoFill Destination:=Range("C7:C14"), Type:=xlFillDefault
    Range("C7:C14").Select
    Range("D7").Select
    Selection.AutoFill Destination:=Range("D7:D14"), Type:=xlFillDefault
    Range("D7:D14").Select
    Range("E7").Select
    Selection.AutoFill Destination:=Range("E7:E14"), Type:=xlFillDefault
    Range("E7:E14").Select
    Range("F7").Select
    Selection.AutoFill Destination:=Range("F7:F14"), Type:=xlFillDefault
    Range("F7:F14").Select
    Range("G7").Select
    Selection.AutoFill Destination:=Range("G7:G14"), Type:=xlFillDefault
    Range("G7:G14").Select
    Range("G15").Select

'
' 曲线拟合 Macro
' 曲线拟合。线性回归。
'

'
    Range("D7:E14").Select
    Charts.Add
    ActiveChart.ChartType = xlXYScatter
    ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("D7:E14")
    ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1"
    ActiveChart.PlotArea.Select
    Selection.ClearFormats
    ActiveChart.Axes(xlValue).MajorGridlines.Select
    Selection.Delete
    ActiveChart.SeriesCollection(1).Select
    ActiveChart.SeriesCollection(1).Trendlines.Add(Type:=xlLinear, Forward:=0, _
        Backward:=0, DisplayEquation:=False, DisplayRSquared:=False).Select

'
' 显示回归方程(截距为0)
' 显示 r^2 的值。
'

'
    ActiveChart.SeriesCollection(1).Trendlines(1).Select
    ActiveChart.SeriesCollection(1).Trendlines(1).Select
    With Selection
        .Type = xlLinear
        .Forward = 0
        .Backward = 0
'        .Intercept = 0.1
        .DisplayEquation = True
        .DisplayRSquared = True
        .NameIsAuto = True
    End With
   
'
' 显示包合常数的表格
' 暂时还没有想到好办法让包合常数也自己计算出来。
'

'
    Range("I4").Select
    ActiveCell.FormulaR1C1 = "包合常数"
    Range("J4").Select

End Sub

没有评论:

发表评论