vb 把textl的内容保存到一个excel里面,大家帮忙看看是哪里错了,谢谢了

来源:百度知道 编辑:UC知道 时间:2024/05/17 06:59:00
程序如下:
Private Sub savedata_Click()
Dim Newdata As String
On Error GoTo errhandle:
CommonDialog1.CancelError = True
CommonDialog1.Filter = "工作表文件(*.xls)|*.xls"
CommonDialog1.FileName = "Newdata.xls"
CommonDialog1.ShowSave
Open CommonDialog1.FileName For Output As #1
Dim appexcel2 As Object '定义应用程序(Excel)
Dim wbmybook2 As Object '定义工作簿
Dim wsmysheet2 As Object '定义工作表
Set appexcel2 = CreateObject("excel.application") '创建Excel应用程序对象
Set wbmybook2 = appexcel2.Workbooks.Open("Newdata.xls") '添加工作簿
Set wsmysheet2 = appexcel2.Worksheets("sheet1") '假设工作表名称为sheet1
wsmysheet2.Cells(1, 2) = Text1.Text
wbmybook2.Close
Close #1
errhandle:
Err.Clear

End Sub

写的最多的那个也不对,应该是新建一个excel啊,不是直接读啊

Option Explicit
Dim xlapp As Variant
Dim xlBook As Variant
Dim xlSheet As Variant
Dim fileName As String

Private Sub Command1_Click()
CommonDialog1.CancelError = True
CommonDialog1.Filter = "工作表文件(*.xls)|*.xls"
CommonDialog1.ShowSave
fileName = CommonDialog1.fileName '= "Newdata.xls"
Set xlapp = CreateObject("excel.application")
Set xlBook = xlapp.Workbooks.open(fileName) '打开EXCEL工作簿
Set xlSheet = xlBook.worksheets(1)
xlapp.Visible = True
xlSheet.Cells(1, 2) = Text1.Text
End Sub

Private Sub Form_Unload(Cancel As Integer)
xlapp.Quit '关闭EXCEL
Set xlapp = Nothing '释放EXCEL对象
End Sub

Open CommonDialog1.FileName For Output As #1
EXCEL不能用这种方法打开写数据。直接操作对象。

要用workbooks对象的open属性打开表格文件,具体你可以找些Vba的例子看看.

你可以用EXCEL中编写程序呀,那不更方便