VB写了新建保存excel文件,但是为啥excel数据没写进去

OptionExplicitDimxlsAppAsExcel.ApplicationPublicxlsBookAsExcel.WorkbookPublicxlsSheet... Option Explicit
Dim xlsApp As Excel.Application
Public xlsBook As Excel.Workbook
Public xlsSheet As New Excel.Worksheet
Dim xlsChar As New Excel.Chart
'判断是否保存,读入
Function savetoexcel()
Dim t1, t2 As Integer
Dim fname As String
CDSave1.Filter = "*.xls|*.xls"

CDSave1.ShowSave
fname = CDSave1.FileName
If fname <> "" Then

Set xlsApp = New Excel.Application
Set xlsApp = CreateObject("Excel.Application") '添加新建表格
Set xlsBook = xlsApp.Workbooks.Add
Set xlsSheet = xlsBook.Worksheets.Add

xlsApp.Sheets("sheet1").Cells(1, 1).Value = "initial Azimuth (°)"
xlsApp.Sheets("sheet1").Cells(1, 2).Value = "initial Inclination (°)"
xlsApp.Sheets("sheet1").Cells(1, 3).Value = "Measured Azimuth (°)"
xlsApp.Sheets("sheet1").Cells(1, 4).Value = "Measured Inclination (°)"
xlsApp.Sheets("sheet1").Cells(1, 5).Value = "Drilling Length (m)"

Dim i As Integer
For i = 1 To count Step 1
xlsApp.Sheets("sheet1").Cells(i + 1, 1).Value = Data1(i)
xlsApp.Sheets("sheet1").Cells(i + 1, 2).Value = Data2(i)
xlsApp.Sheets("sheet1").Cells(i + 1, 3).Value = Data4(i)
xlsApp.Sheets("sheet1").Cells(i + 1, 4).Value = Data5(i)
xlsApp.Sheets("sheet1").Cells(i + 1, 5).Value = Data6(i)
Next i
xlsBook.SaveAs (fname) '保存文件
xlsApp.Quit
Set xlsApp = Nothing
Else
t2 = MsgBox("Please enter a filename", vbInformation + vbYesNoCancel, "OK")
End If
End Function
为啥写的标头字符串不显示呢
展开
 我来答
不想起名字了99
2013-07-23 · TA获得超过1695个赞
知道大有可为答主
回答量:2708
采纳率:76%
帮助的人:730万
展开全部
Option Explicit
Dim xlsApp As Excel.Application
Public xlsBook As Excel.Workbook
Public xlsSheet As New Excel.Worksheet
Dim xlsChar As New Excel.Chart
'判断是否保存,读入
Function savetoexcel()
Dim t1, t2 As Integer
Dim fname As String
CDSave1.Filter = "*.xls|*.xls"

CDSave1.ShowSave
fname = CDSave1.Filename
If fname <> "" Then

Set xlsApp = New Excel.Application
Set xlsApp = CreateObject("Excel.Application") '添加新建表格
Set xlsBook = xlsApp.Workbooks.Add
Set xlsSheet = xlsBook.Worksheets.Add

With xlsSheet
.Cells(1, 1) = "initial Azimuth (°)"
.Cells(1, 2) = "initial Inclination (°)"
.Cells(1, 3) = "Measured Azimuth (°)"
.Cells(1, 4) = "Measured Inclination (°)"
.Cells(1, 5) = "Drilling Length (m)"

Dim i As Integer
For i = 1 To Count Step 1
.Cells(i + 1, 1) = Data1(i)
.Cells(i + 1, 2) = Data2(i)
.Cells(i + 1, 3)= Data4(i)
.Cells(i + 1, 4)= Data5(i)
.Cells(i + 1, 5)= Data6(i)
Next i
End With
xlsBook.SaveAs (fname) '保存文件
xlsApp.Quit
Set xlsApp = Nothing
Else
t2 = MsgBox("Please enter a filename", vbInformation + vbYesNoCancel, "OK")
End If
End Function
追问
楼上,搞错了吧,我是说数据全部没写进去啊,不知道为啥,表格中没有数字或者字符串啊。你这就只是帮我简化了一下,用了个with而已啊···
追答

你的程序中有很多变量就没有初始化和赋值。

也有可能是你发上来的只是程序的片断,所以我没有办法测试,我给你一段核心部分经过调试的代码。你在此基础上修改调试吧。我调试生成的结果我也一并送上。

你把代码部分放到窗口中任意一个按钮的单击事件中,试一下

由于只能发一个附件,结果不发了


已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式