简单问题不简单(在线等候)...

chendjin 2003-03-27 08:30:19
我有如下代码,问题处在模板A文件(内只有一个表)被拷贝为B后,打开B文件,发现多出
一个表sheet1,致使数据不能正常写入,请求高手指点!(下面所有变量均定义)
Set xlApp = New Excel.Application
Set xlApp = GetObject(, "Excel.Application")
If Err.Number = 0 Then
xlApp.Quit
Set xlApp = Nothing
End If
'清除旧文件
sFilter = "*.XLS"
DelFile = Dir(MyAppPath & "\Prt\" & sFilter, vbNormal)
Do While DelFile <> ""
Kill MyAppPath & "\Prt\" & DelFile
DelFile = Dir
Loop
'拷贝模板到目录 MyAppPath & "\Prt下
sBakExclFile = MyAppPath & "\Ini\CqR.XLS"
sCurExclFile = MyAppPath & "\Prt\R" & sYmd & ".XLS"
If Dir(sBakExclFile) <> "" Then
FileCopy sBakExclFile, sCurExclFile
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True 'Excel应用程序窗口
Set xlBook = xlApp.Workbooks.Open(sCurExclFile) '打开工作薄
Set xlSheet = xlBook.Worksheets("Cqr")
xlSheet.Cells(5, 2) = YmdStr
nCurSta = 1 '跟踪区县顺序
For II = 8 To 30 Step 2
xlSheet.Cells(II, 2) = CqSta_Cha(nCurSta) '区县市
xlSheet.Cells(II, 3) = CqStaR08_08(nCurSta)
nCurSta = nCurSta + 1
Next
For II = 8 To 30 Step 2
xlSheet.Cells(II, 4) = CqSta_Cha(nCurSta) '区县市
xlSheet.Cells(II, 5) = CqStaR08_08(nCurSta)
nCurSta = nCurSta + 1
Next
For II = 8 To 28 Step 2
xlSheet.Cells(II, 6) = CqSta_Cha(nCurSta) '区县市
xlSheet.Cells(II, 7) = CqStaR08_08(nCurSta)
nCurSta = nCurSta + 1
Next
xlBook.Save '保存资料
xlBook.PrintPreview
''xlBook.Close True
''xlApp.Quit
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
Else
TxtInf.Text = "模板文件丢失!请与管理者联系。"
End If
...全文
7 点赞 收藏 3
写回复
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
LeeZi 2003-03-28
对不起:
xlApp.Workbooks.Open(sBakExcelFile)
应该是
xlApp.Workbooks.Add(sBakExclFile)
回复
LeeZi 2003-03-28
Set xlApp = New Excel.Application
sBakExclFile ’模板,最好用xlt
sCurExclFile ’File
If Dir(sBakExclFile) <> "" Then
Set xlBook = xlApp.Workbooks.Open(sBakExcelFile) '打开工作薄
Set xlSheet = xlBook.Worksheets("Cqr")
xlSheet.Cells(5, 2) = YmdStr
nCurSta = 1 '跟踪区县顺序
For II = 8 To 30 Step 2
xlSheet.Cells(II, 2) = CqSta_Cha(nCurSta) '区县市
xlSheet.Cells(II, 3) = CqStaR08_08(nCurSta)
nCurSta = nCurSta + 1
Next
For II = 8 To 30 Step 2
xlSheet.Cells(II, 4) = CqSta_Cha(nCurSta) '区县市
xlSheet.Cells(II, 5) = CqStaR08_08(nCurSta)
nCurSta = nCurSta + 1
Next
For II = 8 To 28 Step 2
xlSheet.Cells(II, 6) = CqSta_Cha(nCurSta) '区县市
xlSheet.Cells(II, 7) = CqStaR08_08(nCurSta)
nCurSta = nCurSta + 1
Next
xlBook.Saveas sCurExclFile
xlBook.Close
xlApp.Quit
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
Else
TxtInf.Text = "模板文件丢失!请与管理者联系。"
End If
回复
Jneu 2003-03-27
打开A文件然后,另存为B文件.
回复
发动态
发帖子
Office开发/ VBA
创建于2007-08-27

4969

社区成员

其他开发语言 Office开发/ VBA
申请成为版主
社区公告
暂无公告