Home > Software engineering >  Could you tell me how to solve the following problems using VB program to read and write Excel?
Could you tell me how to solve the following problems using VB program to read and write Excel?

Time:10-06

1. The Excel file on the specified in the program of change seems to exit the program after failure,
2. Each time will open the Excel file to Excel operation,
The above two are don't want to appear as a result, how can I solve!
Part of the program code, what should be amended?
Private Sub Datadeal ()
Call the test ()
Dim N As Integer
Dim xlapp As Excel. Application
Dim xlwork As Excel. The Workbook
Dim xlsheet As Excel. The Worksheet
Xlapp=CreateObject (" Excel. Application ")
Xlapp. Visible=True
Xlwork=xlapp. Workbooks. Open (" F: \ VB \ "Blood XLSX")
Xlsheet=xlwork. Worksheets (1)
Xlsheet. Activate ()
For N=1 To 50
Xlsheet. Cells (1, N)=TD (N)
The Next N
Xlsheet. Cells (1, 1)="ABC"
Xlapp=Nothing
Xlwork=Nothing
Xlsheet=Nothing
End Sub

CodePudding user response:

1 lack save before release statement
Xlapp. Saved=True
2 screen display statement
Xlapp. Visible=True

CodePudding user response:

This read only the problem of how to fix! Seems I open will appear after the file has been is in a state of be edited, become read-only

CodePudding user response:

reference 1st floor zhufobbs response:
1 release before the lack of save statement
Xlapp. Saved=True
2 screen display statement
Xlapp. Visible=True

Seems to have a problem! The xlapp. Saved=true. VS2012 without that

CodePudding user response:

Xlwork. Saved=true

CodePudding user response:

reference 4 floor zhufobbs response:
xlwork. Saved=true
thank you ah, there is a problem I'm sorry, I want writing data from the blank position began to write the end of the last time, how to achieve this?

CodePudding user response:

refer to the second floor qq_15825021 response:
that the read only the problem of how to fix! Seemingly will appear after I open the file was edited has been in the state, in read-only

You said "read only", should be a process is called many times,
"Behind the open document in a read-only state!!!!
The code, you open the document after modified,
There is no save, close the document,
ZhuTie posted code, in addition, you should have grammar mistakes?
Value assigned to the object, it should be: the Set object variables=object reference
This format is the right way!

Revised document, should close the document and save the changes like this:
Xlsheet. Cells (1, 1)="ABC"
The Set xlsheet=Nothing
Xlwork. Close True
The Set xlwork=Nothing
The Set xlsheet=Nothing
1/f, said "the save operation", I think is wrong, so don't save the document,
Another said, is the release of these object reference sequence,
I think, because this a few object has a "hierarchy", should be release reference is better like this:
Release Sheet to quote first, and then close the worksheet (of course you need to hold the document window to the user "will not close).
To release the WorkBook, and finally release the App,
I think it is advantageous to the COM object to properly handle the release and clear memory,

This is just my personal opinion, if you think "it doesn't matter which released casually,"
I also have nothing to say, can only say that turnip greens, his taste,

CodePudding user response:

You are using is VS2012?
If no longer use the Set in the VS,
You set off, in my back upstairs,

CodePudding user response:

reference 5 floor qq_15825021 reply:
Quote: refer to 4th floor zhufobbs response:

Xlwork. Saved=true
thank you ah, there is a problem I'm sorry, I want writing data from the blank position began to write the end of the last time, how to achieve this?

See usedrange

CodePudding user response:

reference 5 floor qq_15825021 reply:
Quote: refer to 4th floor zhufobbs response:

Xlwork. Saved=true
thank you ah, there is a problem I'm sorry, I want writing data from the blank position began to write the end of the last time, how to achieve this?


Xlsheet. Cells (xlsheet UsedRange. Rows. Count + 1, 1). The value="https://bbs.csdn.net/topics/* * *"
  • Related