Sub fillAndPrint()
On Error Resume Next
Dim myExcel, WorkBook, ActiveSheet
Set myExcel = CreateObject("Excel.Application")
If Err Then Err.Clear: MsgBox "无法打开Excel程序,程序中止执行": Exit Sub
Set WorkBook = myExcel.Workbooks.Open("c:\test.xls")
If Err Then Err.Clear: MsgBox "无法打开指定Excel文档,程序中止执行": Set myExcel = Nothing: Exit Sub
Set ActiveSheet = myExcel.sheets(1)
For i = 1 To ActiveSheet.UsedRange.Rows.Count
if ActiveSheet.Cells(i, 1).Value <> "" then
ThisDocument.Range(ThisDocument.Bookmarks(1).Start + 1, ThisDocument.Bookmarks(2).Start - 1).Text = ActiveSheet.Cells(i, 1).Value
If Err Then Err.Clear: MsgBox "没找到替换位置前后的两个书签,程序中止": WorkBook.Close: Set WorkBook = Nothing: Set myExcel = Nothing: Exit Sub
ThisDocument.PrintOut
End if
Next
WorkBook.Close
Set WorkBook = Nothing
Set myExcel = Nothing
End Sub
在【工具】菜单中点击【宏】【Visual Basic编辑器】命令;
在打开的VB编辑器的左侧资源窗口中选中【Book1】或【personal】(如果你想你的电脑里的每个Excel文档都能用就用后一个)工程文档;
点击【插入】菜单中【模块】命令,插入一个模块;
双击该模块,在模块中输入下面三行代码:
Function myRound(a As Double, b As Long)
myRound = Round(a, b)
End Function
回到Excel文档中输入“=myround(12.5,0)” 作者: 桃小 时间: 2009-3-9 10:00
10.
能在excel里做个彩票缩水软件吗?
给m个数,选出n个数,一共有多少种排列,且全部列举出来.
Excel里能做的事很多,应该包括缩水软件。
至于给m个数,选出n个数,一共有多少种排列,可用下面这个函数实现:
在【工具】菜单中点击【宏】【Visual Basic编辑器】命令;
在打开的VB编辑器的左侧资源窗口中选中【Book1】或【personal】(如果你想你的电脑里的每个Excel文档都能用就用后一个)工程文档;
点击【插入】菜单中【模块】命令,插入一个模块;
双击该模块,在模块中输入下面的代码:
Function myCombin(strs As String, M As Integer, GoBack As Long, aaa As Boolean) As String
Dim sResult() As String
Dim sResultB() As String
Dim sArray() As String
Dim N As Integer
Dim nNum As Long
Dim nNumb As Long
Dim i As Long, j As Long
Dim tempV As Long
sArray = Split(strs, ",")
N = UBound(sArray) + 1
If M > N Then
myCombin = "#Value_M"
Exit Function
End If
nNum = N ^ M
nNumb = nNum
ReDim sResult(nNum - 1)
ReDim sResultB(nNum - 1)
Do While nNumb > 1
nNumb = nNumb / N
For i = 0 To nNum - 1
tempV = Int(i / nNumb) Mod N
sResult(i) = sResult(i) & sArray(tempV)
If InStr(sResultB(i), sArray(tempV)) <= 0 Then sResultB(i) = sResultB(i) & sArray(tempV)
Next
Loop
If aaa Then
If (GoBack <= nNum) And GoBack > 0 Then
myCombin = sResult(GoBack - 1)
Else
myCombin = "#Result"
End If
Else
tempV = 1
For i = 0 To nNum - 1
If Len(sResultB(i)) < M Then
If i >= nNum - tempV - 1 Then Exit For
For j = i To nNum - tempV - 1
sResultB(j) = sResultB(j + 1)
Next
i = i - 1
tempV = tempV + 1
End If
Next
tempV = nNum - tempV
ReDim Preserve sResultB(tempV)
If (GoBack <= tempV) And GoBack > 0 Then
myCombin = sResultB(GoBack - 1)
Else
myCombin = "#Result"
End If
End If
End Function
回到Excel文档中输入“=mycombin("a,b,c",3,1,1)”,就可知结果。
“mycombin()”函数的说明:
一、参数说明:
第一个参数:M 个供筛选的数,前后用英文字符的双引号括起来,每个数之间用英文的逗号隔开
第二个参数:每次从第一个参数中选出 N 个字符来排列组合
第三个参数:你要返回排列结果中的第几个组合
第四个参数:排列结果中是否允许重复数字,0表示不允许