Home > Software engineering >  Vb assignment right the first time, the second assignment 1004 error, ask the great spirit guide!!!!
Vb assignment right the first time, the second assignment 1004 error, ask the great spirit guide!!!!

Time:09-25

 Private Sub CmdJoinExcel_Click () '
Dim xlsApp As Object
Dim xlsBook As Object
Dim xlsSheet As Object
Dim xlsApp As New Excel. Application
On the Error Resume Next 'Error changing trap
The Set xlsApp=GetObject (App) Path, "et. Application")
If Err Then
Err. Clear 'removal Err object field
The Set xlsApp=CreateObject (" et. Application ")
If Err Then
Exit Sub 'Exit the program, in order to avoid entering the error handler
End the If
End the If
XlsApp. Visible=True
The Set xlsBook=xlsApp. Workbooks. Add LX=(ComLX. Text)
The Select Case LX
The Case Is=1
Set xlsSheet=xlsBook. Worksheets (1) 'setting work table 1
Cells (1, 1)="basic parameters" : Cells (2, 1)="name" : Cells (2, 2)="open type deep groove ball optimized parameters" : Cells (3, 1)="series,"
The Case Is=2
Set xlsSheet=xlsBook. Worksheets (2) 'setting work table 2
Cells (1, 1)="basic parameters" : Cells (2, 1)="name" : Cells (2, 2)="sealed deep groove ball optimized parameters" : Cells (3, 1)="series,"
The Case Is=3
The Set xlsSheet=xlsBook. Worksheets (3) 'setting work table 3
Cells (1, 1)="basic parameters" : Cells (2, 1)="name" : Cells (2, 2)="with dust cover deep groove ball optimized parameters" : Cells (3, 1)="series,"
End the Select
XlsApp. Save 'Save the workbook
XlsBook. Close 'Close work thin file
XlsApp. Quit 'end excel object
Set xlsApp=Nothing 'release xlapp object to memory space
The Set xlsBook=Nothing
The Set xlsSheet=Nothing
End Sub


The great god, the question now is to excel assignment for the first time, the second time they became empty, not the assignment, please answer, thank you very much!

CodePudding user response:

The building Lord, Mars? This code on earth can you run?

CodePudding user response:

reference 1st floor Topc008 response:
the building Lord, Mars? This code on earth can you run?

 Private Sub Command1_Click () 
Dim xlsApp As Object
Dim xlsBook As Object
Dim xlsSheet As Object
Dim xlscells As Object
On the Error Resume Next 'Error changing trap
The Set xlsApp=GetObject (App) Path, "excel. Application")
If Err Then
Err. Clear 'removal Err object field
The Set xlsApp=CreateObject (" excel. Application ")
If Err Then
Exit Sub 'Exit the program, in order to avoid entering the error handler
End the If
End the If
XlsApp. Visible=True
The Set xlsBook=xlsApp. Workbooks. Add
LX=Val (Text1. Text)
Set xlsSheet=xlsBook. Worksheets (1) 'setting work table 1
Cells (1, 1)=LX
XlsApp. Save 'Save the workbook
XlsBook. Close 'Close work thin file
XlsApp. Quit 'end excel object
Set xlsApp=Nothing 'release xlapp object to memory space
The Set xlsBook=Nothing
The Set xlsSheet=Nothing
End Sub


A great god, and now the code you see in trouble!

CodePudding user response:

 'prefix can't save, you should remove the reference to Excel, the original statement is an error,' 
XlsSheet. Cells (1, 1)=LX

'object to shut off the order/release must be growing up! '
The Set xlsSheet=Nothing
XlsApp. Save 'Save the workbook
XlsBook. Close 'Close work thin file
The Set xlsBook=Nothing
XlsApp. Quit 'end excel object
Set xlsApp=Nothing 'release xlapp object to memory space

CodePudding user response:

Ok, one more example:
 Option Explicit 
Sub Test ()
On the Error Resume Next
Dim xlsApp As Excel Application, xlsBook As Excel. The Workbook
Dim bCreated As Boolean
'first of all get already open the Excel object
The Set xlsApp=GetObject (, "Excel. Application")
If xlsApp Is Nothing Then
"' if there is no open the Excel, to create a new Excel object
The Set xlsApp=CreateObject (" Excel. Application ")
XlsApp. Visible=True
BCreated=True 'tag the excel object is I create
End the If
'set the workbook object
The Set xlsBook=xlsApp. Workbooks. Add
"Check the number of work table, ensure at least 3 list
With xlsBook. Worksheets
If the Count & lt; 3 Then
The Do While. Count & lt; 3
. Add 'cycle to Add a new worksheet
Loop
End the If
End With
"' began to populate the data
With xlsBook. Worksheets (1) "' table 1
Cells (1, 1)="basic parameters"
Cells (2, 1)="name"
Cells (2, 2)="open type deep groove ball optimized parameters"
Cells (3, 1)="series,"
End With
With xlsBook. Worksheets (2) "' table 2
Cells (1, 1)="basic parameters"
Cells (2, 1)="name"
Cells (2, 2)="sealed deep groove ball optimized parameters"
Cells (3, 1)="series,"
End With
With xlsBook. Worksheets (3) 'table 3
Cells (1, 1)="basic parameters"
Cells (2, 1)="name"
Cells (2, 2)="with dust cover deep groove ball optimized parameters"
Cells (3, 1)="series,"
End With
XlsBook. Save 'Save the workbook
XlsBook. Close 'Close the workbook
Set xlsBook=Nothing 'uninstall workbook object
If bCreated Then xlsApp. Quit 'If is create excel object is close
Set xlsApp=Nothing 'uninstall Excel object
End Sub



.
  • Related