软件版本:Office2007
方法如下:
1在Excel中按下Alt+F11,打开VBE界面:
2选择其中一个工作表,输入代码:
3按F5执行代码,就可以看到在Excel中运行了VBA程序:
方法/步骤如下:
1、双击打开桌面的EXCEL;
2、单击右上角的OFFICE图标,找到EXCEL选项,单击打开;
3、勾选在功能区显示“开发工具”选项卡,单击确定按钮完成修改。
4、单击菜单栏的开发工具,然后找到Visual Basic单击打开便可以进行相关的VBA编程。
Private Sub TextBox1_Change()
Dim a As Long
a = Len(Trim(TextBox1Text))
Dim s As String
s = Trim(TextBox1Text)
Dim s1 As String
Dim s2 As String
Dim s3 As String
Dim fzs As String
If a = 0 Then Exit Sub
Dim i As Long
Dim sum As Single
sum = 0
Dim b As Long
For i = 1 To a
s1 = Megetstr(s, i)
If Not (s1 = "") And s1 >= "A" And s1 <= "Z" Then
s2 = Megetstr(s, i + 1)
If Not (s2 = "") And s2 >= "A" And s2 <= "Z" Then
fzs = s1
b = 1
ElseIf Not (s2 = "") And s2 >= "a" And s2 <= "z" Then
fzs = s1 + s2
s3 = Megetstr(s, i + 2)
If Not (s3 = "") And s3 >= "1" And s3 <= "9" Then
b = Int(s3)
Else
b = 1
End If
ElseIf Not (s2 = "") And s2 >= "1" And s2 <= "9" Then
fzs = s1
b = Int(s2)
Else
fzs = s1
b = 1
End If
sum = sum + b Megetfzl(fzs)
End If
Next
MeTextBox2Text = sum
End Sub
Private Sub UserForm_Click()
End Sub
Public Function getstr(s As String, i As Long) As String
If s = "" Or i > Len(s) Then
getstr = ""
Else
getstr = Mid(s, i, 1)
End If
End Function
Public Function getfzl(s As String) As Single
'这里添加各单元素的分子量
Dim t As Single
If s = "C" Then
t = 12
ElseIf s = "H" Then
t = 1
ElseIf s = "O" Then
t = 16
ElseIf s = "Ca" Then
t = 26
Else
End If
getfzl = t
End Function
'解释 当前元素分子量库只有C,H,O,Ca 等,其他的可以自己添加
在TEXTBOX1 中输入 H2O TEXTBOX2中显示18
输入H3C 则显示15
输入H2Ca 则显示28
以上就是关于如何在Excel中创建VBA程序全部的内容,包括:如何在Excel中创建VBA程序、EXCEL中如何用VBA实现、如何用Excel中的VBA设计程序,使的在建立好的窗口中输入化学式后可自动算出其相对分子量等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)