明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1059|回复: 2

[求助]求帮忙改写一下代码

[复制链接]
发表于 2007-10-23 23:17:00 | 显示全部楼层 |阅读模式

在excel中能正确运行的vba代码,怎样改写成vb中的代码做成dll文件来调用?求高手帮忙改写一下!
vba代码如下:

Sub mc()
Sheets(1).Unprotect Password:="123456"
Sheets("xscj").Unprotect Password:="123456"
cjzhs = Sheets(1).[a1].CurrentRegion.Rows.Count        '成绩表的总行数
cjzls = Sheets(1).[a1].CurrentRegion.Columns.Count - 1 '成绩表的总列数

Sheets("xscj").Select
Cells.ClearContents

Sheets(1).Select
Range(Cells(1, 1), Cells(cjzhs, cjzls)).Select
Selection.Copy
Sheets("xscj").Select
Range("a1").Select
Selection.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Worksheets("xscj").Range(Cells(2, 1), Cells(cjzhs, cjzls)).Sort Key1:=Worksheets("xscj").Range("a2"), Order1:=xlAscending, Key2:=Worksheets("xscj").Range("c2"), Order2:=xlAscending  '以科类班级排序,使文科理科分开同一班的在一起
kml = 7 'InputBox("请输入第一个学科所在的列号(数值):") * 1
Cells(1, cjzls + 1) = "平均"
Cells(1, cjzls + 2) = "总分"
Cells(1, cjzls + 3) = "班名"
Cells(1, cjzls + 4) = "年名"
For i = 1 To cjzls - kml + 1
  Cells(1, cjzls + 4 + i) = Left(Cells(1, kml + i - 1), 1) & "名"
Next i
wlkb = Cells(2, 1)
krs = WorksheetFunction.CountIf(Range(Cells(2, 1), Cells(cjzhs, 1)), wlkb)  '取该科号的人数
bjh = Cells(2, 3)
kbh = 2
brs = WorksheetFunction.CountIf(Range(Cells(2, 3), Cells(kbh + krs - 1, 3)), bjh) '取该班号的人数

bjhh = 2
For i = 2 To cjzhs
 If WorksheetFunction.Sum(Range(Cells(i, kml), Cells(i, cjzls))) Then Cells(i, cjzls + 2) = WorksheetFunction.Sum(Range(Cells(i, kml), Cells(i, cjzls)))
 If Cells(i, cjzls + 2) > 0 Then Cells(i, cjzls + 1) = Round(WorksheetFunction.Average(Range(Cells(i, kml), Cells(i, cjzls))), 2)
Next i
For i = 2 To cjzhs
 If wlkb = Cells(i, 1) Then
    If bjh <> Cells(i, 3) Then
       bjhh = i
       bjh = Cells(i, 3)
       brs = WorksheetFunction.CountIf(Range(Cells(i, 3), Cells(kbh + krs - 1, 3)), bjh) '取该班号的人数
    End If
 Else
    kbh = i
    wlkb = Cells(i, 1)
    krs = WorksheetFunction.CountIf(Range(Cells(i, 1), Cells(cjzhs, 1)), wlkb)  '取该科号的人数
    bjhh = i
    bjh = Cells(i, 3)
    brs = WorksheetFunction.CountIf(Range(Cells(i, 3), Cells(kbh + krs - 1, 3)), bjh) '取该班号的人数
 End If
 If Cells(i, cjzls + 2) > 0 Then
  Cells(i, cjzls + 3) = WorksheetFunction.Rank(Cells(i, cjzls + 2), Range(Cells(bjhh, cjzls + 2), Cells(bjhh + brs - 1, cjzls + 2)))
  Cells(i, cjzls + 4) = WorksheetFunction.Rank(Cells(i, cjzls + 2), Range(Cells(kbh, cjzls + 2), Cells(kbh + krs - 1, cjzls + 2)))
 End If
 For j = 1 To cjzls - kml + 1
   If WorksheetFunction.IsNumber(Cells(i, kml + j - 1)) Then Cells(i, cjzls + 4 + j) = WorksheetFunction.Rank(Cells(i, kml + j - 1), Range(Cells(kbh, kml + j - 1), Cells(kbh + krs - 1, kml + j - 1)))
 Next j
Next i
Sheets("xscj").Protect Password:="123456", AllowFormattingCells:=True, AllowFormattingColumns:=True, AllowFormattingRows:=True, AllowSorting:=True, UserInterfaceOnly:=True
Sheets(1).Protect Password:="123456", AllowFormattingCells:=True, AllowFormattingColumns:=True, AllowFormattingRows:=True, UserInterfaceOnly:=True
End Sub

先谢谢各位高手来帮我一下。

发表于 2007-10-24 18:26:00 | 显示全部楼层
建议你直接用VB来调用Excel软件,这样Excel中就不存在任何代码了。VB做DLL非常难,这不是一句两句能说清(可能一千句也说不清)。
 楼主| 发表于 2007-10-24 22:29:00 | 显示全部楼层
感谢黄玉宏高手指点,让我先学一学VB来调用Excel软件,不会的地方望能请教。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-26 12:33 , Processed in 0.160082 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表