| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 872 人关注过本帖
标题:[求助]请教写exce表问题。谢谢l
只看楼主 加入收藏
cguzh
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2007-8-23
收藏
 问题点数:0 回复次数:2 
[求助]请教写exce表问题。谢谢l

我的程序有个界面,通过选择时间段计算结果,然后把结果保存到EXCEL文件中。我保存的EXCEL文件的文件名是以时间段为文件名的。
我现在是这样写的
Dim zsbexcel As excel.Application

Dim zsbworkbook As Workbook
Set zsbexcel = New excel.Application
zsbexcel.Visible = False
zsbexcel.SheetsInNewWorkbook = 1
Set zsbworkbook = zsbexcel.Workbooks.Add
zsbexcel.Sheets(zsbexcel.Sheets.Count).Name = "1.xls"
With zsbexcel.ActiveSheet
.Cells(14, 1).Value = "aaaaa"
.Cells(15, 1).Value = "bbbbb"
......(具体附值就不细写了)
End With

dpath = Trim(Text17.Text) ‘这是我保存的路径
pnum = Len(dpath)
If Right(dpath, 1) = "\" Then dpath = Left(dpath, pnum - 1)
ActiveWorkbook.Close True, dpath & "\" & ds & "--" & de (ds,de分别是每次计算前附值,这些都和出错没关系)
zsbexcel.Quit
Set zsbexcel = Nothing

保存第一个文件不会提示错误,但如果时间段不变,再保存就会提示:“对象变量或者WITH块变量未设置”,如果我改变时间段计算后,等于是保存不同文件名的EXCEL文件了,但还是会提示“是否保存对“book2”的更改”,为什么这样的呢,我保存的文件名和我第一个并不重复啊?

提示错误的行是:ActiveWorkbook.Close True, dpath & "\" & ds & "--" & de
谢谢

搜索更多相关主题的帖子: zsbexcel Set EXCEL Application Dim 
2007-08-23 14:05
lanshanlhy
Rank: 1
等 级:新手上路
帖 子:130
专家分:0
注 册:2006-4-16
收藏
得分:0 
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlSheet1 As Excel.Worksheet
Dim i As Integer, tmHour As String
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
On Error Resume Next

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Set xlApp = Excel.Application
Set xlBook = xlApp.Workbooks.Add
'xlBook.Activate
Set xlSheet = xlBook.Worksheets(1) '''''''''''''''''''''''''''''''''引用第1张工作表
xlApp.ActiveSheet.Rows.VerticalAlignment = xlVAlignCenter '''''垂直方向居中
xlApp.ActiveSheet.Rows.HorizontalAlignment = xlVAlignCenter '''水平方向居中
xlSheet.Name = "实测值"
Set xlSheet1 = xlBook.Worksheets(2)
xlSheet1.Name = "Chart"

With xlSheet
For i = 2 To 11
.Range(Cells(1, 1), Cells(1, i)).Merge ''''''''''''''''''''合并A-K单元格
Next
' .Cells(1, 1).ForeColor = RGB(100, 150, 255)
.Cells(1, 1).Font.Size = 25

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''设置行高'设置列宽
For i = 1 To 22
.Rows(i).RowHeight = 25
Next
For i = 1 To 11
.Columns(i).ColumnWidth = 15
Next
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''合并单元格
For i = 3 To 22

If i < 8 Then
.Range(Cells(3, 1), Cells(i, 1)).Merge '''''''''''''合并A3-A7单元格
.Range(Cells(3, 8), Cells(i, 8)).Merge '''''''''''''合并H3-H7单元格
ElseIf i < 13 Then
.Range(Cells(8, 1), Cells(i, 1)).Merge
.Range(Cells(8, 8), Cells(i, 8)).Merge
ElseIf i < 18 Then
.Range(Cells(13, 1), Cells(i, 1)).Merge
.Range(Cells(13, 8), Cells(i, 8)).Merge
ElseIf i < 23 Then
.Range(Cells(18, 1), Cells(i, 1)).Merge
.Range(Cells(18, 8), Cells(i, 8)).Merge
End If
Next
''''''''''''''''''''''''''''''''''''''''''''
.Range("A1", "K22").Borders.LineStyle = xlContinuous '''''''''''单元格边框
.Range("A1", "K22").Borders.Color = vbBlue ''''''''''''''''''''''边框颜色
.Range("A1", "K22").Interior.Color = RGB(100, 180, 0) '''''''''''区域 背景色


'''''''''''''''''''''''''''''''''''电压值
.Range("A3").Value = "90"
.Range("A8").Value = "115"
.Range("A13").Value = "230"
.Range("A18").Value = "264"
End With
tmHour = "-" & Hour(Time)
tmHour = tmHour & "-" & Minute(Time)
tmHour = tmHour & "-" & Second(Time)
xlApp.ActiveWorkbook.SaveAs App.Path & "\" & Format(Date, dddd, mmmm, yyyy) & tmHour + ".xls"
xlApp.Workbooks.Close
xlApp.Quit
Set xlApp = Nothing '释放引用

永远以今天为荣!
2007-08-23 15:13
cguzh
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2007-8-23
收藏
得分:0 

谢谢。。。。。。。。

2007-08-24 15:17
快速回复:[求助]请教写exce表问题。谢谢l
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025513 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved