Home > Software engineering >  Already using vb to control open the excel file, how to use in the process of another
Already using vb to control open the excel file, how to use in the process of another

Time:09-29


Such as:
Outside the process wrote
Dim Excel As Excel. Application
Dim Workbook As Excel. The Workbook
Dim Sheet1 As Excel. The Worksheet
Dim sheet2 As Excel. The Worksheet
Dim sheet3 As Excel. The Worksheet

In the process of a
The Set of Excel=CreateObject (" Excel. Application ")
Set Workbook.=Excel Workbooks. Open (App) Path & amp; "\ temp. XLS")
The Set Sheet1=Workbook. Sheets (" Sheet1 ")
Opened the temp. XLS and assignment, how to use this object in another process?

Again using the above statement?

CodePudding user response:

The Workbook or Sheet1 passed as an argument!

CodePudding user response:

The things into the form level variables or defined as global public variables,
If it is to use in a module, class is defined as the form variables, use can be either private or dim; If anywhere in your project is likely to need, it is defined as a global public variables, such as:

In the standard module defined as global public variables, and then in your project all the modules can be called directly,
Public Excel As Excel. Application
. Public Workbook As Excel Workbook
Public Sheet1 As Excel. The Worksheet
Public sheet2 As Excel. The Worksheet
Public sheet3 As Excel. The Worksheet

Then as in form1 or can be used directly in form2 sheet1, sheet2, sheet3...

CodePudding user response:

See, if using Getobject excel file has been opened, open multiple workbook, and then use workbooks (I) method to determine the use of the workbook

CodePudding user response:

The same program transfer object directly!
Different programs to use the GetObject (),

CodePudding user response:

reference 4 floor Tiger_Zhao response:
the same program transfer object directly!
Different programs to use the GetObject (),

Not very understand, defined as Public objects or outside the sub level is defined as the form of the object, the same form, in the same engineering of different sub how to use?
The sub used in finish, need to set??=nothing?

CodePudding user response:

1) if the method call methods b
Defined as a local variable, a creation, as a parameter to b, a final release,
2) if a and b are respectively called after the user action, no sure successively relationship
Level is defined as the form variables, created in the Form_Load, Form_Unload release,

CodePudding user response:

Baidu again still don't understand, I think it over

CodePudding user response:

I have the following views:
1, your statement released no exit and objects;
2, to achieve common calls, can be written as general procedure or function; The sample is as follows:
The Public Function OpExcelFile (TempExcelFiles as string, SheetName as string) as Boolean
'A declared variable
'B operation (see your own code)
C release object
'End the Function

Then in the other place calls to this function is ok!
---------------------------------------------------------

CodePudding user response:

Private Sub Command4_Click ()
The Set excel1=New Excel. Application
Excel1. Visible=False
Excel1. Workbooks. Add

Excel1. Sheets (" sheet1 "). The Range (" A2 ".) CopyFromRecordset Adodc1. You
With excel1. Sheets (" sheet1 ")

Cells (1, 1)="number", "
Cells (1, 2)="hotel name
"Cells (1, 3)="kitchen"
Cells (1, 4)="name"
Cells (1, 5)="number"
Cells (1, 6)="unit", "
Cells (1, 7)="unit price"
Cells (1, 8)="price"
. The Columns (" g "g"). The NumberFormat="$0.00"
Columns (h: "h"). The NumberFormat="$0.00"
. The Columns (I: "I"). The Delete
. The Columns (I: "I"). The Delete
. The Columns (I: "I"). The Delete
. The Cells. The Replace What:=", "Replacement:=", "LookAt:=xlPart, SearchOrder:=_
XlByRows MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
11.. Cells. The Font Size=
. The Rows. AutoFit
. The Columns. AutoFit
End With
Excel1. Visible=True
End Sub
Reference code
  • Related