请教如何做一个像电子表格那样的一个控件
VB下怎么样做一个你电子表格一样的控件,用来输入数据和显示计算结果?VB下有一个表格控件不太好用,请精通这方面的人指教!!! <p style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0;">欢迎访问我的博客<a href="http://dongxingsofthome.blog.hexun.com/8667925_d.html">http://dongxingsofthome.blog.hexun.com/8667925_d.html</a></p><p style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0;">[原创]在VB中用ScriptControl控件让MSHFlexGrid表格像Excel一样计算<br/>用过Office的朋友都知道Excel的表格是一种具有计算功能的动态表格,这种直观的表格计算功能对于用户来说是相当的人性化了,因此,作为开发者在作一个表格的应用程序时当然十分希望自己的程序能具有这种功能,目前有FORMULA ONE、Spread及OfficeWeb组件等控件可以实现这种功能,有需要使用的朋友可以到网上搜索以下相关资料,很丰富的,因此本人不再对他们做介绍,以下文字将着重介绍如何用MSHFlexGrid和ScriptControl控件来实现这种类Excel的计算表格,由于篇幅有限对于MSHFlexGrid的美化编程不做介绍,大家可参考网上相关资料。<br/>一、控件简介<br/>1、MSHFlexGrid(MSHFlxGrd.ocx)这种控件允许将文本或者图象放置于每个单元格之中,控件的Row与Col属性允许用户在代码中指定当前行和列,当然也可通过操作鼠标和键盘来改变这两个属性,而text属性指明当前单元格的文本。如果单元格的文本太长而不能完全显示出来的话,可以通过将WordWrap属性设置为true来达到显示的目的。<br/>2、ScriptControl(msscript.ocx)控件来解释执行vbs脚本代码。<br/>二、实现基本思路<br/>用MSHFlexGrid和文本框组合使用作为表格数据输入输出的用户界面,将数据用自定义的表格对象来进行存储,这个对象必须包含单元格的地址、单元格内容、行列数、单元格的公式、单元格的格式化字符串等属性,对单元格公式进行处理形成VBS脚本代码,然后用ScriptControl计算出VBS代码的结果,将计算出的结果写入自定义的表格对象和MSHFlexGrid对应的表格内,至此一次计算完成。<br/>三、程序代码<br/>1、窗体:在窗体上放置一个Frame控件命名为“Frame<chmetcnv tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="1" unitname="”" wst="on">1<span lang="EN-US" style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';"><span lang="EN-US">”</span></chmetcnv>,内部放置一个Text</span>控件命名为“Text<chmetcnv tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="1" unitname="”" wst="on">1<span lang="EN-US" style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';"><span lang="EN-US">”</span></chmetcnv>,放置一个MSHFlexGrid</span>控件命名为“MSHFlexGrid<chmetcnv tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="1" unitname="”" wst="on">1<span lang="EN-US" style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';"><span lang="EN-US">”</span></chmetcnv>,<span lang="EN-US">在表格控件内放置一个</span>Text</span>控件命名为“Text<chmetcnv tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="2" unitname="”" wst="on">2<span lang="EN-US" style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';"><span lang="EN-US">”</span></chmetcnv>,<span lang="EN-US">在表格下方放置一个</span>Text</span>控件命名为“Text<chmetcnv tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="3" unitname="”" wst="on">3<span lang="EN-US" style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';"><span lang="EN-US">”</span></chmetcnv>,<span lang="EN-US">在</span>Text3</span>下方放置一个按钮控件命名为“Command<chmetcnv tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="1" unitname="”" wst="on">1<span lang="EN-US" style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';"><span lang="EN-US">”</span></chmetcnv>。</span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0;"><a href="http://dongxingsofthome.photo.hexun.com/17565263_d.html" target="_blank"><img height="489" alt="查看更多精彩图片" src="http://photo5.hexun.com/p/2007/0405/87826/b_594E1E6192F9CE1B2B1568CB7F59410E.jpg" width="621" border="0" style="WIDTH: 621px; HEIGHT: 489px;"/></a><br/><br/>窗体代码:<br/><span style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto;">Dim EditChanged As Boolean, CurrRow As Integer, CurrCol As Integer, EditState As Boolean<br/><br/>Private Sub Command1_Click()<br/><span style="mso-spacerun: yes;"> '</span></span>设置单元格A1的格式为"K0+000.00"<br/><span style="mso-spacerun: yes;"> Sheet("A1").FormatString = "K0+000.00": Call Calculate<br/>End Sub<br/><br/>Private Sub Form_Load()<br/> Dim I As Integer, J As Integer<br/> Me.Caption = "</span>模拟Excel计算表格"<br/><span style="mso-spacerun: yes;"> CreateTableHead 100 '</span>生成表头<br/><span style="mso-spacerun: yes;"> With Text2<br/> .Appearance = 0<br/> .Visible = False: EditChanged = False<br/> .Font.Size = 11<br/> End With<br/> With MSHFlexGrid1<br/> Frame1.Caption = "</span>单元格" & .TextMatrix(0, 1) & .TextMatrix(1, 0) & "的公式"<br/><span style="mso-spacerun: yes;"> '</span>初始化表格对象<br/><span style="mso-spacerun: yes;"> For J = 1 To .Rows - 1<br/> For I = 1 To .Cols - 1<br/> '</span>单元格地址用A1形式表示,公式,单元格格式,单元格文本,行,列,索引关键字<br/><span style="mso-spacerun: yes;"> Sheet.Add .TextMatrix(0, I) & J, "", "", "", J, I, .TextMatrix(0, I) & J<br/> Next<br/> Next<br/> End With<br/> <br/>End Sub<br/>Private Sub CreateTableHead(R As Integer)<br/> With MSHFlexGrid1<br/> .Cols = 20<br/> .Rows = 20<br/> .Font.Size = 12<br/> .AllowUserResizing = flexResizeBoth<br/> s$ = " |"<br/> For J = 65 To 90<br/> s$ = s$ & Chr(J) & "|"<br/> Next<br/> s$ = Left(s$, Len(s$) - 1)<br/> s$ = s$ & ";|"<br/> For J = 1 To R<br/> s$ = s$ & J & "|"<br/> Next<br/> .FormatString = Left(s$, Len(s$) - 1)<br/> For J = 1 To 26<br/> .ColWidth(J) = 1000<br/> Next<br/> End With<br/>End Sub<br/><br/>Private Sub Label1_Click()<br/>'</span>打开某个网址<br/>'ShellExecute Me.hwnd, "open", "http://dongxingsofthome.blog.hexun.com/", vbNullString, vbNullString, vbNormalFocus<br/>Shell "C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE http://dongxingsofthome.blog.hexun.com/8341928_d.html", vbMaximizedFocus<br/>'给某个信箱发电子邮件<br/>'ShellExecute hWnd, "open", "mailto:sst95@21cn.com", vbNullString, vbNullString, 0<br/><br/>End Sub<br/><br/>Private Sub MSHFlexGrid1_DblClick()<br/><span style="mso-spacerun: yes;"> If MSHFlexGrid1.Text <> "" Then<br/> EditState = True<br/> Else<br/> EditState = False<br/> End If<br/> With MSHFlexGrid1<br/> Text2.Text = Sheet(.TextMatrix(0, .Col) & .Row).Formula<br/> End With<br/> Text2.Visible = True<br/> With MSHFlexGrid1<br/> Text2.Top = .CellTop + 2010<br/> Text2.Left = .CellLeft + 90<br/> Text2.Height = .CellHeight - 20<br/> Text2.Width = .CellWidth + 30<br/> Text2.SetFocus<br/> End With<br/>End Sub<br/><br/>Private Sub MSHFlexGrid1_KeyDown(KeyCode As Integer, Shift As Integer)<br/><br/> If KeyCode = 229 Then<br/> Text2.Text = ""<br/> ElseIf KeyCode <> 37 And KeyCode <> 38 And KeyCode <> 39 And KeyCode <> 40 Then<br/> Text2.Text = ""<br/> End If<br/> If KeyCode = 46 Then '</span>处理Delete键<br/><span style="mso-spacerun: yes;"> Text1.Text = ""<br/> With MSHFlexGrid1<br/> For J = .Row To .RowSel<br/> For I = .Col To .ColSel<br/> .TextMatrix(J, I) = ""<br/> Sheet.Item(.TextMatrix(0, I) & J).Formula = ""<br/> Next<br/> Next<br/> End With<br/> End If<br/>End Sub<br/><br/>Private Sub MSHFlexGrid1_KeyPress(KeyAscii As Integer)<br/><br/> If KeyAscii < 255 And KeyAscii <> 27 And KeyAscii <> 8 Then<br/> If Chr(KeyAscii) = "=" Then<br/> Text2.Text = ""<br/> End If<br/> Text2.Text = Text2.Text & Chr(KeyAscii)<br/> End If<br/> If KeyAscii = 8 Then 'back<br/> Text2.Text = ""<br/> End If<br/> If KeyAscii <> 27 And KeyAscii <> 13 Then<br/> Text2.SelStart = Len(Text2.Text)<br/> Text2.Visible = True<br/> With MSHFlexGrid1<br/> Text2.Top = .CellTop + 2010<br/> Text2.Left = .CellLeft + 90<br/> Text2.Height = .CellHeight - 20<br/> Text2.Width = .CellWidth + 30<br/> Text2.SetFocus<br/> End With<br/> End If<br/> If KeyAscii = 13 Then<br/> MSHFlexGrid1.Row = MSHFlexGrid1.Row + 1: EditChanged = False<br/> With MSHFlexGrid1<br/> Frame1.Caption = "</span>单元格" & .TextMatrix(0, .Col) & .TextMatrix(.Row, 0) & "的公式"<br/><span style="mso-spacerun: yes;"> Text1.Text = Sheet.Item(MSHFlexGrid1.TextMatrix(0, MSHFlexGrid1.Col) & MSHFlexGrid1.Row).Formula<br/> End With<br/> End If<br/>End Sub<br/><br/>Private Sub MSHFlexGrid1_RowColChange()<br/> If EditChanged = True Then<br/> With MSHFlexGrid1<br/> .TextMatrix(CurrRow, CurrCol) = Text2.Text<br/> Sheet.Item(.TextMatrix(0, CurrCol) & CurrRow).Formula = Text2.Text<br/> End With<br/> Call Calculate<br/> End If<br/> Text2.Visible = False: EditChanged = False<br/> Text1.Text = Sheet.Item(MSHFlexGrid1.TextMatrix(0, MSHFlexGrid1.Col) & MSHFlexGrid1.Row).Formula<br/>End Sub<br/><br/>Private Sub MSHFlexGrid1_SelChange()<br/> With MSHFlexGrid1<br/> Frame1.Caption = "</span>单元格" & .TextMatrix(0, .Col) & .TextMatrix(.Row, 0) & "的公式"<br/><span style="mso-spacerun: yes;"> '</span>在公式栏内显示单元格的公式<br/><span style="mso-spacerun: yes;"> Text1.Text = Sheet.Item(MSHFlexGrid1.TextMatrix(0, MSHFlexGrid1.Col) & MSHFlexGrid1.Row).Formula<br/> End With<br/>End Sub<br/><br/>Private Sub Text2_Change()<br/> EditChanged = True<br/> CurrRow = MSHFlexGrid1.Row<br/> CurrCol = MSHFlexGrid1.Col<br/>End Sub<br/><br/>Private Sub Text2_KeyDown(KeyCode As Integer, Shift As Integer)<br/> Select Case KeyCode '</span>处理光标键<br/><span style="mso-spacerun: yes;"> Case 37, 38, 39, 40<br/> If EditState = False Then<br/> Call SetCellContent(Text2.Text)<br/> Text2.Visible = False<br/> If KeyCode = 40 Then<br/> MSHFlexGrid1.Row = MSHFlexGrid1.Row + 1<br/> ElseIf KeyCode = 37 Then<br/> MSHFlexGrid1.Col = MSHFlexGrid1.Col - 1<br/> ElseIf KeyCode = 39 Then<br/> MSHFlexGrid1.Col = MSHFlexGrid1.Col + 1<br/> ElseIf KeyCode = 38 Then<br/> MSHFlexGrid1.Row = MSHFlexGrid1.Row - 1<br/> End If<br/> End If<br/> EditState = False<br/> MSHFlexGrid1.SetFocus<br/> End Select<br/>End Sub<br/><br/>Private Sub Text2_KeyPress(KeyAscii As Integer)<br/> Select Case KeyAscii<br/> Case 13 '</span>处理回车键Enter<br/><span style="mso-spacerun: yes;"> Call SetCellContent(Text2.Text)<br/> Text2.Visible = False<br/> MSHFlexGrid1.Row = MSHFlexGrid1.Row + 1<br/> MSHFlexGrid1.SetFocus<br/> EditState = False<br/> EditChanged = False<br/> MSHFlexGrid1.SetFocus<br/> Case 27 '</span>处理ESC键<br/><span style="mso-spacerun: yes;"> Text2.Visible = False<br/> MSHFlexGrid1.SetFocus<br/> EditChanged = False<br/> End Select<br/>End Sub<br/></span>2、模块:在模块窗口内将下面代码粘贴进去<br/><span style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto;">Public Sheet As New Cells<br/>Function FormulaSort(FOBJ As MyFormulas) As String '</span>对公式代码进行排序<br/>Dim SwFlag As Boolean '发生公式交换的标志<br/>Dim TmpFormulaObj As New MyFormula '存放临时的公式对象,作为交换时使用<br/>Dim InsKey As String<br/>SwFlag = True<br/>With FOBJ<br/><span style="mso-spacerun: yes;"> Do<br/> 'SwFlag = False<br/> If .Count = 0 Then<br/> Exit Do<br/> End If<br/> For J = 1 To .Count<br/> findstr = .Item(J).FormulaName<br/> For I = 1 To .Count<br/> If InStr(1, .Item(I).FormulaString, findstr, vbTextCompare) <> 0 Then<br/> If I < J Then<br/> InsKey = .Item(I).FormulaName<br/> Set TmpFormulaObj = .Item(J)<br/> .Remove J<br/> .Add TmpFormulaObj.FormulaName, TmpFormulaObj.FormulaString, InsKey, , TmpFormulaObj.FormulaName<br/> 'For m = 1 To .Count<br/> ' Debug.Print .Item(m).FormulaName, .Item(m).FormulaString<br/> 'Next<br/> SwFlag = True<br/> Exit For<br/> End If<br/> Else<br/> SwFlag = False<br/> End If<br/> Next<br/> If SwFlag = True Then<br/> Exit For<br/> End If<br/> Next<br/> 'SwFlag = False<br/> Loop Until SwFlag = False<br/> FormulaSort = ""<br/> For J = 1 To .Count<br/> FormulaSort = FormulaSort & .Item(J).FormulaName & "=" & .Item(J).FormulaString & vbCrLf<br/> Next<br/>End With<br/>End Function<br/>'</span>表格计算引擎<br/>Sub Calculate()<br/><span style="mso-spacerun: yes;"> Dim FOBJS As New MyFormulas, VarName As String, VBCode As String<br/> With FOBJS<br/> For J = 1 To Sheet.Count '</span>将单元格公式存入FOBJS对象<br/><span style="mso-spacerun: yes;"> Txt = Sheet(J).Formula<br/> If Txt & Sheet(J).Text <> "" Then<br/> If IsDate(Txt) Then<br/> .Add Sheet(J).Address, "#" & CDate(Sheet(J).Formula) & "#", , , Sheet(J).Address<br/> VarName = VarName & Sheet(J).Address & ","<br/> ElseIf IsNumeric(Txt) Then<br/> .Add Sheet(J).Address, Sheet(J).Formula, , , Sheet(J).Address<br/> VarName = VarName & Sheet(J).Address & ","<br/> Else<br/> If Left(Sheet(J).Formula, 1) = "=" Then<br/> .Add Sheet(J).Address, Right(Sheet(J).Formula, Len(Sheet(J).Formula) - 1), , , Sheet(J).Address<br/> VarName = VarName & Sheet(J).Address & ","<br/> Else<br/> .Add Sheet(J).Address, Chr(34) & Sheet(J).Formula & Chr(34), , , Sheet(J).Address<br/> VarName = VarName & Sheet(J).Address & ","<br/> End If<br/> End If<br/> End If<br/> Next<br/> End With<br/> '</span>对上面存入的公式进行排序生成最终的源码<br/><span style="mso-spacerun: yes;"> VBCode = FormulaSort(FOBJS)<br/> If VBCode = "" Then<br/> Exit Sub<br/> End If<br/> VBCode = Left(VBCode, Len(VBCode) - 1) & vbCrLf: VarName = Left(VarName, Len(VarName) - 1)<br/> VBCode = "Function SheetResult()" & vbCrLf _<br/> & "On Error Resume Next" & vbCrLf _<br/> & VBCode & _<br/> "SheetResult=Array(" & VarName & ")" & vbCrLf & _<br/> "End Function"<br/> '</span>将生成的代码,用Script解释器计算并返回计算结果<br/><span style="mso-spacerun: yes;"> Form1.Script1.AddCode VBCode<br/> Form1.Text3.Text = VBCode '</span>显示排序后的单元格计算代码<br/><span style="mso-spacerun: yes;"> Dim Tmp As Variant, VarNstr As Variant<br/> VarNstr = <place wst="on"><city wst="on">Split</city></place>(VarName, ",")<br/> Tmp = Form1.Script1.Run("SheetResult")<br/> '</span>将计算结果返回到单元格中<br/><span style="mso-spacerun: yes;"> For J = 1 To FOBJS.Count<br/> With Form1.MSHFlexGrid1<br/> If IsDate(Tmp(J - 1)) Or IsNumeric(Tmp(J - 1)) Then '</span>如果是数字和日期则用格式化字符串进行格式化<br/><span style="mso-spacerun: yes;"> ts = Format(Tmp(J - 1), Sheet(VarNstr(J - 1)).FormatString)<br/> .TextMatrix(Sheet(VarNstr(J - 1)).Row, Sheet(VarNstr(J - 1)).Col) = ts<br/> Sheet(VarNstr(J - 1)).Text = ts<br/> Else<br/> .TextMatrix(Sheet(VarNstr(J - 1)).Row, Sheet(VarNstr(J - 1)).Col) = Tmp(J - 1)<br/> Sheet(VarNstr(J - 1)).Text = Tmp(J - 1)<br/> End If<br/> End With<br/> Next<br/>End Sub<br/>'</span>设定当前单元格和单元格对象的内容,并进行更新计算<br/>Sub SetCellContent(Txt As String)<br/><span style="mso-spacerun: yes;"> With Form1.MSHFlexGrid1<br/> .Text = Txt<br/> Sheet.Item(.TextMatrix(0, .Col) & .Row).Formula = Txt<br/> End With<br/> Call Calculate<br/>End Sub<br/></span>3、类模块代码:<br/>(1)新建Cell类模块将下面代码粘贴到类模块中<br/><span style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto;">'</span>保持属性值的局部变量<br/>Private mvarAddress As String '局部复制<br/>Private mvarFormula As String '局部复制<br/>Private mvarFormatString As String '局部复制<br/>Private mvarText As String '局部复制<br/>'保持属性值的局部变量<br/>Private mvarRow As Integer '局部复制<br/>Private mvarCol As Integer '局部复制<br/>Public Property Let <place wst="on"><state wst="on">Col</state></place>(ByVal vData As Integer)<br/>'向属性指派值时使用,位于赋值语句的左边。<br/>'Syntax: X.Col = 5<br/><span style="mso-spacerun: yes;"> mvarCol = vData<br/>End Property<br/><br/><br/>Public Property Get <place wst="on"><state wst="on">Col</state></place>() As Integer<br/>'</span>检索属性值时使用,位于赋值语句的右边。<br/>'Syntax: Debug.Print X.Col<br/><span style="mso-spacerun: yes;"> <state wst="on"><place wst="on">Col</place></state> = mvarCol<br/>End Property<br/><br/><br/><br/>Public Property Let Row(ByVal vData As Integer)<br/>'</span>向属性指派值时使用,位于赋值语句的左边。<br/>'Syntax: X.Row = 5<br/><span style="mso-spacerun: yes;"> mvarRow = vData<br/>End Property<br/><br/><br/>Public Property Get Row() As Integer<br/>'</span>检索属性值时使用,位于赋值语句的右边。<br/>'Syntax: Debug.Print X.Row<br/><span style="mso-spacerun: yes;"> Row = mvarRow<br/>End Property<br/><br/><br/>Public Property Let Text(ByVal vData As String)<br/>'</span>向属性指派值时使用,位于赋值语句的左边。<br/>'Syntax: X.Text = 5<br/><span style="mso-spacerun: yes;"> mvarText = vData<br/>End Property<br/><br/><br/>Public Property Get Text() As String<br/>'</span>检索属性值时使用,位于赋值语句的右边。<br/>'Syntax: Debug.Print X.Text<br/><span style="mso-spacerun: yes;"> Text = mvarText<br/>End Property<br/><br/><br/><br/>Public Property Let FormatString(ByVal vData As String)<br/>'</span>向属性指派值时使用,位于赋值语句的左边。<br/>'Syntax: X.FormatString = 5<br/><span style="mso-spacerun: yes;"> mvarFormatString = vData<br/>End Property<br/><br/><br/>Public Property Get FormatString() As String<br/>'</span>检索属性值时使用,位于赋值语句的右边。<br/>'Syntax: Debug.Print X.FormatString<br/><span style="mso-spacerun: yes;"> FormatString = mvarFormatString<br/>End Property<br/><br/><br/><br/>Public Property Let Formula(ByVal vData As String)<br/>'</span>向属性指派值时使用,位于赋值语句的左边。<br/>'Syntax: X.Formula = 5<br/><span style="mso-spacerun: yes;"> mvarFormula = vData<br/>End Property<br/><br/><br/>Public Property Get Formula() As String<br/>'</span>检索属性值时使用,位于赋值语句的右边。<br/>'Syntax: Debug.Print X.Formula<br/><span style="mso-spacerun: yes;"> Formula = mvarFormula<br/>End Property<br/><br/><br/><br/>Public Property Let Address(ByVal vData As String)<br/>'</span>向属性指派值时使用,位于赋值语句的左边。<br/>'Syntax: X.Address = 5<br/><span style="mso-spacerun: yes;"> mvarAddress = vData<br/>End Property<br/><br/><br/>Public Property Get Address() As String<br/>'</span>检索属性值时使用,位于赋值语句的右边。<br/>'Syntax: Debug.Print X.Address<br/><span style="mso-spacerun: yes;"> Address = mvarAddress<br/>End Property<br/></span>(2)新建Cells类模块将下面代码粘贴到类模块中<br/><span style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto;">'</span>局部变量,保存集合<br/>Private mCol As Collection<br/>'保持属性值的局部变量<br/>Private mvarRows As Integer '局部复制<br/>Private mvarCols As Integer '局部复制<br/>Public Property Let Cols(ByVal vData As Integer)<br/>'向属性指派值时使用,位于赋值语句的左边。<br/>'Syntax: X.Cols = 5<br/><span style="mso-spacerun: yes;"> mvarCols = vData<br/>End Property<br/><br/><br/>Public Property Get Cols() As Integer<br/>'</span>检索属性值时使用,位于赋值语句的右边。<br/>'Syntax: Debug.Print X.Cols<br/><span style="mso-spacerun: yes;"> Cols = mvarCols<br/>End Property<br/><br/><br/><br/>Public Property Let Rows(ByVal vData As Integer)<br/>'</span>向属性指派值时使用,位于赋值语句的左边。<br/>'Syntax: X.Rows = 5<br/><span style="mso-spacerun: yes;"> mvarRows = vData<br/>End Property<br/><br/><br/>Public Property Get Rows() As Integer<br/>'</span>检索属性值时使用,位于赋值语句的右边。<br/>'Syntax: Debug.Print X.Rows<br/><span style="mso-spacerun: yes;"> Rows = mvarRows<br/>End Property<br/><br/><br/><br/><br/>Public Function Add(Address As String, Formula As String, FormatString As String, Text As String, Row As Integer, Col As Integer, Optional sKey As String) As Cell<br/> '</span>创建新对象<br/><span style="mso-spacerun: yes;"> Dim objNewMember As Cell<br/> Set objNewMember = New Cell<br/><br/><br/> '</span>设置传入方法的属性<br/><span style="mso-spacerun: yes;"> objNewMember.Address = Address<br/> objNewMember.Formula = Formula<br/> objNewMember.FormatString = FormatString<br/> objNewMember.Text = Text<br/> objNewMember.Row = Row<br/> objNewMember.Col = Col<br/> If Len(sKey) = 0 Then<br/> mCol.Add objNewMember<br/> Else<br/> mCol.Add objNewMember, sKey<br/> End If<br/><br/><br/> '</span>返回已创建的对象<br/><span style="mso-spacerun: yes;"> Set Add = objNewMember<br/> Set objNewMember = Nothing<br/><br/><br/>End Function<br/><br/>Public Property Get Item(vntIndexKey As Variant) As Cell<br/> '</span>引用集合中的一个元素时使用。<br/><span style="mso-spacerun: yes;"> 'vntIndexKey </span>包含集合的索引或关键字,<br/><span style="mso-spacerun: yes;"> '</span>这是为什么要声明为 Variant 的原因<br/><span style="mso-spacerun: yes;"> '</span>语法:Set foo = x.Item(xyz) or Set foo = x.Item(5)<br/><span style="mso-spacerun: yes;"> Set Item = mCol(vntIndexKey)<br/>End Property<br/><br/><br/><br/>Public Property Get Count() As Long<br/> '</span>检索集合中的元素数时使用。语法:Debug.Print x.Count<br/><span style="mso-spacerun: yes;"> Count = mCol.Count<br/>End Property<br/><br/><br/>Public Sub Remove(vntIndexKey As Variant)<br/> '</span>删除集合中的元素时使用。<br/><span style="mso-spacerun: yes;"> 'vntIndexKey </span>包含索引或关键字,这是为什么要声明为 Variant 的原因<br/><span style="mso-spacerun: yes;"> '</span>语法:x.Remove(xyz)<br/><br/><br/><span style="mso-spacerun: yes;"> mCol.Remove vntIndexKey<br/>End Sub<br/><br/><br/>Public Property Get NewEnum() As IUnknown<br/> '</span>本属性允许用 For...Each 语法枚举该集合。<br/><span style="mso-spacerun: yes;"> Set NewEnum = mCol.<br/>End Property<br/><br/><br/>Private Sub Class_Initialize()<br/> '</span>创建类后创建集合<br/><span style="mso-spacerun: yes;"> Set mCol = New Collection<br/>End Sub<br/><br/><br/>Private Sub Class_Terminate()<br/> '</span>类终止后破坏集合<br/><span style="mso-spacerun: yes;"> Set mCol = Nothing<br/>End Sub<br/><br/></span>(3)新建MyFormula类模块将下面代码粘贴到类模块中<br/><span style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto;">'<span style="mso-spacerun: yes;"> ____ ___ ___ ___<br/>' / \/ \/ \/ \<br/>' ______\ ____\____\____\____\<br/>'#/ __\\ \ \ \ \#######################################################<br/>'| / \\____/\___/\___/\___/#######################################################<br/>'| | |############################################################################<br/>'#\__\____/############################################################################<br/>'######################################################################################<br/>'######################################################################################<br/>'######################################################################################<br/>'###### ######<br/>'###### </span></span>版<span style="mso-spacerun: yes;"> </span>本:V1.01<span style="mso-spacerun: yes;"> ######<br/>'###### </span>升级信息:<chsdate wst="on" isrocdate="False" islunardate="False" day="21" month="1" year="2005">2005年1月21日</chsdate>完成修改<span style="mso-spacerun: yes;"> ######<br/>'###### </span>程序编写:董兴<span style="mso-spacerun: yes;"> ######<br/>'###### </span>版权所有:董兴<span style="mso-spacerun: yes;"> ######<br/>'###### </span>电子邮箱:DongXing@126.COM<span style="mso-spacerun: yes;"> ######<br/>'###### ######<br/>'######################################################################################<br/>'######################################################################################<br/>'######################################################################################<br/>'###### ######<br/>'###### </span>声明:本程序让您免费使用,并可自由传播,但对<span style="mso-spacerun: yes;"> ######<br/>'###### </span>使用本程序产生的任何后果概不负责。<span style="mso-spacerun: yes;"> ######<br/>'###### ######<br/>'######################################################################################<br/>'######################################################################################<br/>'######################################################################################<br/><br/>'</span>保持属性值的局部变量<br/>Private mvarFormulaName As String '局部复制<br/>Private mvarFormulaString As String<span style="mso-spacerun: yes;"> '</span>局部复制<br/>Public Property Let FormulaString(ByVal vData As String)<br/>'向属性指派值时使用,位于赋值语句的左边。<br/>'Syntax: X.Y = 5<br/><span style="mso-spacerun: yes;"> mvarFormulaString = vData<br/>End Property<br/><br/><br/>Public Property Get FormulaString() As String<br/>'</span>检索属性值时使用,位于赋值语句的右边。<br/>'Syntax: Debug.Print X.Y<br/><span style="mso-spacerun: yes;"> FormulaString = mvarFormulaString<br/>End Property<br/><br/><br/>Public Property Let FormulaName(ByVal vData As String)<br/>'</span>向属性指派值时使用,位于赋值语句的左边。<br/>'Syntax: X.PName = 5<br/><span style="mso-spacerun: yes;"> mvarFormulaName = vData<br/>End Property<br/><br/><br/>Public Property Get FormulaName() As String<br/>'</span>检索属性值时使用,位于赋值语句的右边。<br/>'Syntax: Debug.Print X.PName<br/><span style="mso-spacerun: yes;"> FormulaName = mvarFormulaName<br/>End Property<br/><br/></span>(4)新建MyFormulas类模块将下面代码粘贴到类模块中<br/><span style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto;">'<span style="mso-spacerun: yes;"> ____ ___ ___ ___<br/>' / \/ \/ \/ \<br/>' ______\ ____\____\____\____\<br/>'#/ __\\ \ \ \ \#######################################################<br/>'| / \\____/\___/\___/\___/#######################################################<br/>'| | |############################################################################<br/>'#\__\____/############################################################################<br/>'######################################################################################<br/>'######################################################################################<br/>'######################################################################################<br/>'###### ######<br/>'###### </span></span>版<span style="mso-spacerun: yes;"> </span>本:V1.01<span style="mso-spacerun: yes;"> ######<br/>'###### </span>升级信息:<chsdate wst="on" isrocdate="False" islunardate="False" day="21" month="1" year="2005">2005年1月21日</chsdate>完成修改<span style="mso-spacerun: yes;"> ######<br/>'###### </span>程序编写:董兴<span style="mso-spacerun: yes;"> ######<br/>'###### </span>版权所有:董兴<span style="mso-spacerun: yes;"> ######<br/>'###### </span>电子邮箱:DongXing@126.COM<span style="mso-spacerun: yes;"> ######<br/>'###### ######<br/>'######################################################################################<br/>'######################################################################################<br/>'######################################################################################<br/>'###### ######<br/>'###### </span>声明:本程序让您免费使用,并可自由传播,但对<span style="mso-spacerun: yes;"> ######<br/>'###### </span>使用本程序产生的任何后果概不负责。<span style="mso-spacerun: yes;"> ######<br/>'###### ######<br/>'######################################################################################<br/>'######################################################################################<br/>'######################################################################################<br/><br/>'</span>局部变量,保存集合<br/>Private mCol As Collection<br/><br/>Public Function Add(FormulaName As String, FormulaString As String, Optional sBefore As String, Optional sAfter As String, Optional sKey As String) As MyFormula<br/><span style="mso-spacerun: yes;"> '</span>创建新对象<br/><span style="mso-spacerun: yes;"> Dim objNewMember As MyFormula<br/> Set objNewMember = New MyFormula<br/><br/><br/> '</span>设置传入方法的属性<br/><span style="mso-spacerun: yes;"> objNewMember.FormulaName = FormulaName<br/> objNewMember.FormulaString = FormulaString<br/> If Len(sKey) = 0 Then<br/> If Len(sBefore) = 0 And Len(sAfter) <> 0 Then<br/> mCol.Add objNewMember, , , sAfter<br/> ElseIf Len(sBefore) <> 0 And Len(sAfter) = 0 Then<br/> mCol.Add objNewMember, , sBefore<br/> Else<br/> mCol.Add objNewMember<br/> End If<br/> Else<br/> If Len(sBefore) = 0 And Len(sAfter) <> 0 Then<br/> mCol.Add objNewMember, sKey, , sAfter<br/> ElseIf Len(sBefore) <> 0 And Len(sAfter) = 0 Then<br/> mCol.Add objNewMember, sKey, sBefore<br/> Else<br/> mCol.Add objNewMember, sKey<br/> End If<br/> End If<br/><br/><br/> '</span>返回已创建的对象<br/><span style="mso-spacerun: yes;"> Set Add = objNewMember<br/> Set objNewMember = Nothing<br/>End Function<br/><br/>Public Property Get Item(vntIndexKey As Variant) As MyFormula<br/> '</span>引用集合中的一个元素时使用。<br/><span style="mso-spacerun: yes;"> 'vntIndexKey </span>包含集合的索引或关键字,<br/><span style="mso-spacerun: yes;"> '</span>这是为什么要声明为 Variant 的原因<br/><span style="mso-spacerun: yes;"> '</span>语法:Set foo = x.Item(xyz) or Set foo = x.Item(5)<br/><span style="mso-spacerun: yes;"> Set Item = mCol(vntIndexKey)<br/>End Property<br/><br/><br/><br/>Public Property Get Count() As Long<br/> '</span>检索集合中的元素数时使用。语法:Debug.Print x.Count<br/><span style="mso-spacerun: yes;"> Count = mCol.Count<br/>End Property<br/><br/><br/>Public Sub Remove(vntIndexKey As Variant)<br/> '</span>删除集合中的元素时使用。<br/><span style="mso-spacerun: yes;"> 'vntIndexKey </span>包含索引或关键字,这是为什么要声明为 Variant 的原因<br/><span style="mso-spacerun: yes;"> '</span>语法:x.Remove(xyz)<br/><br/><br/><span style="mso-spacerun: yes;"> mCol.Remove vntIndexKey<br/>End Sub<br/><br/><br/>Public Property Get NewEnum() As IUnknown<br/> '</span>本属性允许用 For...Each 语法枚举该集合。<br/><span style="mso-spacerun: yes;"> Set NewEnum = mCol.<br/>End Property<br/><br/><br/>Private Sub Class_Initialize()<br/> '</span>创建类后创建集合<br/><span style="mso-spacerun: yes;"> Set mCol = New Collection<br/>End Sub<br/><br/><br/>Private Sub Class_Terminate()<br/> '</span>类终止后破坏集合<br/><span style="mso-spacerun: yes;"> Set mCol = Nothing<br/>End Sub<br/><br/></span>保存工程并运行测试,在A1单元格中输入“=a2*b<chmetcnv tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="2" unitname="”" wst="on">2<span lang="EN-US" style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';"><span lang="EN-US">”</span></chmetcnv>,A2</span>中输入10,B2中输入150,A4中输入“=a1+<chmetcnv tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="3" unitname="”" wst="on">3<span lang="EN-US" style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';"><span lang="EN-US">”</span></chmetcnv>程序运行结果如图。</span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0;"><span lang="EN-US" style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';"><a href="http://dongxingsofthome.photo.hexun.com/17565263_d.html" target="_blank"></a><a href="http://dongxingsofthome.photo.hexun.com/17565264_d.html" target="_blank"><img alt="查看更多精彩图片" src="http://photo5.hexun.com/p/2007/0405/87826/b_BBBB9EFD4F38348F8B2804407DB7ED91.jpg" border="0"/></a><br/><br/></span> 在text3文本框中显示了上面对应的vbs脚本,其实这个对于开发过程中的调试过程比较有用,但在调试成功之后就可以将其屏蔽删除了。<br/> 当然上面的程序还有一定的缺陷,如对于公式循环性的判断,对错误的处理等没有相应的处理代码,本文只是对表格计算功能实现方法进行了简单的研究和介绍,个人要做一个功能比较齐全的电子表格控件是比较困难的,推荐大家还是使用商业的控件比较快捷。<br style="mso-special-character: line-break;"/><br style="mso-special-character: line-break;"/><a href="http://dongxingsofthome.photo.hexun.com/16862609_d.html" target="_blank"><img alt="查看更多精彩图片" src="http://photo5.hexun.com/p/2007/0321/84313/b_F54E8D9A59F01B29A80CDDBFF8084ABC.jpg" border="0"/></a><a title="点击下载源码" href="http://www.51files.com/?D84R2FQJ0OPOFN5ADGH0" target="_blank">点击下载源码</a><br/><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0;"><span lang="EN-US">欢迎访问我的博客<a href="http://dongxingsofthome.blog.hexun.com/8667925_d.html">http://dongxingsofthome.blog.hexun.com/8667925_d.html</a></p></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0;">欢迎访问我的博客<a href="http://dongxingsofthome.blog.hexun.com/8667925_d.html">http://dongxingsofthome.blog.hexun.com/8667925_d.html</a></p>
非常谢谢
谢 版主还没给加分呦呵呵!
页:
[1]