首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

100分求Excel中编写VB程序处理sheet中数据的方法,该如何处理

2012-01-20 
100分求Excel中编写VB程序处理sheet中数据的方法具体要求如下:sheet1中:A1、A2、A3中分别有数据5、6、7现在需

100分求Excel中编写VB程序处理sheet中数据的方法
具体要求如下:
sheet1中:A1、A2、A3中分别有数据5、6、7
现在需要编写VB程序,对上边数据进行复杂的函数处理,然后,结果在sheet2中的B1、C1、D1单元格展示。
sheet2中:
B1--sheet1的A1的阶乘(N!)。
C1--sheet1中A2的A1次方(6的5次方,当然不能写死,数据必须从A1,A2取)
D1--自然数从1开始,一直加,加到sheet1中A2和A3的乘积数为止的总和(1+2+3+……+42,也不能写死)

[解决办法]
'Excel VBA方法:
Function jiechen(n As Long) As Long 'n> =0
If n = 0 Or n = 1 Then
jiechen = 1
Exit Function
Else
jiechen = n * jiechen(n - 1)
End If
End Function

Sub run()
Dim A1 As Long, A2 As Long, A3 As Long
Dim B1 As Long, C1 As Long, D1 As Long
A1 = Worksheets( "sheet1 ").Range( "A1 ").Value
A2 = Worksheets( "sheet1 ").Range( "A2 ").Value
A3 = Worksheets( "sheet1 ").Range( "A3 ").Value
B1 = jiechen(A1)
C1 = A2 ^ A1
D1 = A2 * A3 * (A2 * A3 + 1) / 2
Worksheets( "sheet2 ").Range( "B1 ").Value = B1
Worksheets( "sheet2 ").Range( "C1 ").Value = C1
Worksheets( "sheet2 ").Range( "D1 ").Value = D1
End Sub


[解决办法]
'VB方法
'首先在菜单:工程-引用中选中 Microsoft Office 11.0 Object Library,视你机子上装的Office版本而定
'创建一个窗体Form1,在Form1上添加一按钮Command1,在Form1内添加如下代码
Function jiechen(n As Long) As Long 'n> =0
If n = 0 Or n = 1 Then
jiechen = 1
Exit Function
Else
jiechen = n * jiechen(n - 1)
End If
End Function

Private Sub Command1_Click()
Dim xexl As Object
Dim A1 As Long, A2 As Long, A3 As Long
Dim B1 As Long, C1 As Long, D1 As Long
Set xexl = CreateObject( "Excel.Application ")
xexl.Workbooks.Open ( "C:\exp01.xls ")
A1 = xexl.Worksheets( "sheet1 ").Range( "A1 ").Value
A2 = xexl.Worksheets( "sheet1 ").Range( "A2 ").Value
A3 = xexl.Worksheets( "sheet1 ").Range( "A3 ").Value
B1 = jiechen(A1)
C1 = A2 ^ A1
D1 = A2 * A3 * (A2 * A3 + 1) / 2
xexl.Worksheets( "sheet2 ").Range( "B1 ").Value = B1
xexl.Worksheets( "sheet2 ").Range( "C1 ").Value = C1
xexl.Worksheets( "sheet2 ").Range( "D1 ").Value = D1
xexl.Workbooks( "exp01.xls ").Save
xexl.Workbooks( "exp01.xls ").Close (True)
End Sub

热点排行