|
本帖最后由 xmhope 于 2013-5-21 20:24 编辑
我编的一段代码在自己的机器上可以运行,但在同事的机器上运行出现下标越界的错误提示。我是新手找不出原因,向各位高手求救。代码如下:
Private Sub CommandButton1_Click()
Dim z As Integer '定义变量z为整型,用于存放光标所在行号
Dim strMl As String '定义变量strMl为字符串型,用于存放合同台账的路径
Dim strtzk As String '定义变量strtzk为字符串型,用于存放台账库路径
Dim htmc As String '定义变量htmc为字符串型,用于存放合同名称
Dim c, d As String '定义c和d变量为字符串型
Dim stryf As String '定义stryf变量为字串型,用于存放乙方单位名称
Dim sigprice As Single '定义sigprice变量为单精度型,用于存放合同价格
Dim savfilename, oriname As String '定义两个变量为字串型,用于复制和存储付款台账合同的文件名
z = ActiveCell.Row '读取光标所在行
htmc = Cells(z, 2) '读入合同编号
If htmc = "" Then GoTo 10 '判断合同编号是否为空,如为空转10行给出提示信息
stryf = Cells(z, 4) '读取乙方单位名称
sigprice = Cells(z, 7) '读取合同价款
strMl = Application.ActiveWorkbook.Path '将合同台账所在路径赋予strml变量
strtzk = strMl + "\台账库" '合同台账所在路径下一级台账库路径赋予strtzk变量
c = Dir(strtzk, 16) '判断台账库子目录是否存在
If c = "" Then MkDir strtzk '台账库目录如不存在则建立台账库目录
d = strtzk + "\" + htmc + ".xlsm" '生成合同付款台账文件名,存放在变量d中
If Dir(d, 16) = "" Then '检查台账库目录下是否存在以合同编号命名的付款台账文件
oriname = strMl + "\合同付款台账样表.xlsm": savfilename = strtzk + "\" + htmc + ".xlsm": FileCopy oriname, savfilename '将样表以合同编号为文件名存储
Cells(z, 2).Select
ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:=d, TextToDisplay:=htmc '建立超链接
Workbooks.Open d
Workbooks(htmc).Worksheets("合同付款台账").Cells(3, 2) = htmc '将合同付款台账里的数据传递给新建的文件(此处出错!!)
Workbooks(htmc).Worksheets("合同付款台账").Cells(5, 2) = stryf
Workbooks(htmc).Worksheets("合同付款台账").Cells(6, 2) = Cells(z, 3)
Workbooks(htmc).Worksheets("合同付款台账").Cells(7, 2) = sigprice
Workbooks(htmc).Worksheets("合同付款台账").Cells(7, 4) = Cells(z, 6)
Workbooks(htmc).Worksheets("合同付款台账").Cells(3, 6) = Cells(z, 5)
Workbooks(htmc).Worksheets("合同付款台账").Cells(7, 6) = "='" + strMl + "\[合同台账.xlsm]合同台账'" + "!$I$" + CStr(z)
Workbooks("合同台账").Worksheets("合同台账").Cells(z, 8) = "='" + strtzk + "\[" + htmc + ".xlsm]" + "变更签证台账'" + "!$I$16"
Workbooks("合同台账").Worksheets("合同台账").Cells(z, 11) = "='" + strtzk + "\[" + htmc + ".xlsm]" + "合同付款台账'" + "!$D$34"
Else
MsgBox "该合同付款台账已存在,不可重复建立"
End If
GoTo 20
10 MsgBox "合同未编号,无法创建付款台账"
20 End Sub
|
|