lcmphy 发表于 2007-10-23 23:17:00

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

<p>在excel中能正确运行的vba代码,怎样改写成vb中的代码做成dll文件来调用?求高手帮忙改写一下!<br/>vba代码如下:</p><p>Sub mc()<br/>Sheets(1).Unprotect Password:="123456"<br/>Sheets("xscj").Unprotect Password:="123456"<br/>cjzhs = Sheets(1)..CurrentRegion.Rows.Count&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '成绩表的总行数<br/>cjzls = Sheets(1)..CurrentRegion.Columns.Count - 1 '成绩表的总列数</p><p>Sheets("xscj").Select<br/>Cells.ClearContents</p><p>Sheets(1).Select<br/>Range(Cells(1, 1), Cells(cjzhs, cjzls)).Select<br/>Selection.Copy<br/>Sheets("xscj").Select<br/>Range("a1").Select<br/>Selection.PasteSpecial Paste:=xlPasteValues<br/>Application.CutCopyMode = False<br/>Worksheets("xscj").Range(Cells(2, 1), Cells(cjzhs, cjzls)).Sort Key1:=Worksheets("xscj").Range("a2"), Order1:=xlAscending, Key2:=Worksheets("xscj").Range("c2"), Order2:=xlAscending&nbsp; '以科类班级排序,使文科理科分开同一班的在一起<br/>kml = 7 'InputBox("请输入第一个学科所在的列号(数值):") * 1<br/>Cells(1, cjzls + 1) = "平均"<br/>Cells(1, cjzls + 2) = "总分"<br/>Cells(1, cjzls + 3) = "班名"<br/>Cells(1, cjzls + 4) = "年名"<br/>For i = 1 To cjzls - kml + 1<br/>&nbsp; Cells(1, cjzls + 4 + i) = Left(Cells(1, kml + i - 1), 1) &amp; "名"<br/>Next i<br/>wlkb = Cells(2, 1)<br/>krs = WorksheetFunction.CountIf(Range(Cells(2, 1), Cells(cjzhs, 1)), wlkb)&nbsp; '取该科号的人数<br/>bjh = Cells(2, 3)<br/>kbh = 2<br/>brs = WorksheetFunction.CountIf(Range(Cells(2, 3), Cells(kbh + krs - 1, 3)), bjh) '取该班号的人数</p><p>bjhh = 2<br/>For i = 2 To cjzhs<br/>&nbsp;If WorksheetFunction.Sum(Range(Cells(i, kml), Cells(i, cjzls))) Then Cells(i, cjzls + 2) = WorksheetFunction.Sum(Range(Cells(i, kml), Cells(i, cjzls)))<br/>&nbsp;If Cells(i, cjzls + 2) &gt; 0 Then Cells(i, cjzls + 1) = Round(WorksheetFunction.Average(Range(Cells(i, kml), Cells(i, cjzls))), 2)<br/>Next i<br/>For i = 2 To cjzhs<br/>&nbsp;If wlkb = Cells(i, 1) Then<br/>&nbsp;&nbsp;&nbsp; If bjh &lt;&gt; Cells(i, 3) Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bjhh = i<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bjh = Cells(i, 3)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; brs = WorksheetFunction.CountIf(Range(Cells(i, 3), Cells(kbh + krs - 1, 3)), bjh) '取该班号的人数<br/>&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;Else<br/>&nbsp;&nbsp;&nbsp; kbh = i<br/>&nbsp;&nbsp;&nbsp; wlkb = Cells(i, 1)<br/>&nbsp;&nbsp;&nbsp; krs = WorksheetFunction.CountIf(Range(Cells(i, 1), Cells(cjzhs, 1)), wlkb)&nbsp; '取该科号的人数<br/>&nbsp;&nbsp;&nbsp; bjhh = i<br/>&nbsp;&nbsp;&nbsp; bjh = Cells(i, 3)<br/>&nbsp;&nbsp;&nbsp; brs = WorksheetFunction.CountIf(Range(Cells(i, 3), Cells(kbh + krs - 1, 3)), bjh) '取该班号的人数<br/>&nbsp;End If<br/>&nbsp;If Cells(i, cjzls + 2) &gt; 0 Then<br/>&nbsp; Cells(i, cjzls + 3) = WorksheetFunction.Rank(Cells(i, cjzls + 2), Range(Cells(bjhh, cjzls + 2), Cells(bjhh + brs - 1, cjzls + 2)))<br/>&nbsp; Cells(i, cjzls + 4) = WorksheetFunction.Rank(Cells(i, cjzls + 2), Range(Cells(kbh, cjzls + 2), Cells(kbh + krs - 1, cjzls + 2)))<br/>&nbsp;End If<br/>&nbsp;For j = 1 To cjzls - kml + 1<br/>&nbsp;&nbsp; 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)))<br/>&nbsp;Next j<br/>Next i<br/>Sheets("xscj").Protect Password:="123456", AllowFormattingCells:=True, AllowFormattingColumns:=True, AllowFormattingRows:=True, AllowSorting:=True, UserInterfaceOnly:=True<br/>Sheets(1).Protect Password:="123456", AllowFormattingCells:=True, AllowFormattingColumns:=True, AllowFormattingRows:=True, UserInterfaceOnly:=True<br/>End Sub</p><p>先谢谢各位高手来帮我一下。<br/></p>

黄玉宏 发表于 2007-10-24 18:26:00

建议你直接用VB来调用Excel软件,这样Excel中就不存在任何代码了。VB做DLL非常难,这不是一句两句能说清(可能一千句也说不清)。

lcmphy 发表于 2007-10-24 22:29:00

感谢<font face="Verdana" color="#61b713"><strong>黄玉宏</strong><font color="#29384e">高手指点,让我先学一学VB来调用Excel软件,不会的地方望能请教。</font></font>
页: [1]
查看完整版本: [求助]求帮忙改写一下代码