Private Sub CmdJoinExcel_Click() '
Dim xlsApp As Object 
Dim xlsBook As Object 
Dim xlsSheet As Object
 Dim xlsApp As New Excel.Application
 On Error Resume Next ' 改变错误陷阱 
Set xlsApp = GetObject(App.Path, "et.Application")
 If Err Then 
Err.Clear '清除 Err 对象字段 
Set xlsApp = CreateObject("et.Application")
 If Err Then 
Exit Sub ' 退出程序,以避免进入错误处理程序
 End If 
End If
 xlsApp.Visible = True 
Set xlsBook = xlsApp.Workbooks.Add LX = (ComLX.Text)
 Select Case LX 
Case Is = 1 
Set xlsSheet = xlsBook.Worksheets(1) '设定工作表1
 Cells(1, 1) = "基本参数": Cells(2, 1) = "名称": Cells(2, 2) = "开式深沟球优化参数": Cells(3, 1) = "系列" 
Case Is = 2 
Set xlsSheet = xlsBook.Worksheets(2) '设定工作表2
 Cells(1, 1) = "基本参数": Cells(2, 1) = "名称": Cells(2, 2) = "密封深沟球优化参数": Cells(3, 1) = "系列" 
Case Is = 3 
Set xlsSheet = xlsBook.Worksheets(3) '设定工作表3 
Cells(1, 1) = "基本参数": Cells(2, 1) = "名称": Cells(2, 2) = "带防尘盖深沟球优化参数": Cells(3, 1) = "系列" 
End Select
 xlsApp.Save '保存工作簿
 xlsBook.Close '关闭工作薄文件 
xlsApp.Quit '结束excel对象 
Set xlsApp = Nothing '释放xlapp对象得内存空间 
Set xlsBook = Nothing 
Set xlsSheet = Nothing 
End Sub 大神,现在的问题是第一次可以给excel赋值,第二次就变成空的了,不能赋值,求解答,十分感谢啊!

解决方案 »

  1.   

    Private Sub Command1_Click()
    Dim xlsApp As Object
    Dim xlsBook As Object
    Dim xlsSheet As Object
    Dim xlscells As Object
     On Error Resume Next ' 改变错误陷阱
    Set xlsApp = GetObject(App.Path, "excel.Application")
     If Err Then
    Err.Clear '清除 Err 对象字段
    Set xlsApp = CreateObject("excel.Application")
     If Err Then
    Exit Sub ' 退出程序,以避免进入错误处理程序
     End If
    End If
     xlsApp.Visible = True
    Set xlsBook = xlsApp.Workbooks.Add
     LX = Val(Text1.Text)
    Set xlsSheet = xlsBook.Worksheets(1) '设定工作表1
    cells(1, 1) = LX
     xlsApp.Save '保存工作簿
     xlsBook.Close '关闭工作薄文件
    xlsApp.Quit '结束excel对象
    Set xlsApp = Nothing '释放xlapp对象得内存空间
    Set xlsBook = Nothing
    Set xlsSheet = Nothing
    End Sub 大神,现在的代码你看看吧,麻烦了啊!
      

  2.   

    '前缀不能省。你应该把对Excel的引用去掉,原先的语句就报错了。'
    xlsSheet.cells(1, 1) = LX'对象关闭/释放必须按从小到大的次序来!'
    Set xlsSheet = Nothing
    xlsApp.Save '保存工作簿
    xlsBook.Close '关闭工作薄文件
    Set xlsBook = Nothing
    xlsApp.Quit '结束excel对象
    Set xlsApp = Nothing '释放xlapp对象得内存空间
      

  3.   

    好吧,再来一个例子:
    Option Explicit
    Sub Test()
        On Error Resume Next
        Dim xlsApp As Excel.Application, xlsBook As Excel.Workbook
        Dim bCreated As Boolean
        ''首先获取已经打开的Excel对象
        Set xlsApp = GetObject(, "Excel.Application")
        If xlsApp Is Nothing Then
            ''如果Excel没有打开,创建一个新的Excel对象
            Set xlsApp = CreateObject("Excel.Application")
            xlsApp.Visible = True
            bCreated = True ''标记这个excel对象是我创建的
        End If
        ''设置工作簿对象
        Set xlsBook = xlsApp.Workbooks.Add
        ''检查工作表的数量,保证至少有3张表
        With xlsBook.Worksheets
            If .Count < 3 Then
                Do While .Count < 3
                    .Add ''循环添加新工作表
                Loop
            End If
        End With
        ''开始填充数据
        With xlsBook.Worksheets(1) ''第1张表
            .Cells(1, 1) = "基本参数"
            .Cells(2, 1) = "名称"
            .Cells(2, 2) = "开式深沟球优化参数"
            .Cells(3, 1) = "系列"
        End With
        With xlsBook.Worksheets(2) ''第2张表
            .Cells(1, 1) = "基本参数"
            .Cells(2, 1) = "名称"
            .Cells(2, 2) = "密封深沟球优化参数"
            .Cells(3, 1) = "系列"
        End With
        With xlsBook.Worksheets(3) ''第3张表
            .Cells(1, 1) = "基本参数"
            .Cells(2, 1) = "名称"
            .Cells(2, 2) = "带防尘盖深沟球优化参数"
            .Cells(3, 1) = "系列"
        End With
        xlsBook.Save ''保存工作簿
        xlsBook.Close ''关闭工作簿
        Set xlsBook = Nothing ''卸载工作簿对象
        If bCreated Then xlsApp.Quit ''如果是自己创建的excel对象则关闭
        Set xlsApp = Nothing ''卸载Excel对象
    End Sub..