Home > Software engineering >  Transplanting the vba macros in vb6
Transplanting the vba macros in vb6

Time:09-16

Fellow teachers, there are three or four in the excel vba macros, can run normally, want to migrate to vb6
In vb6, the first button to start the excel, is normal, the second button ready in excel is already started to read and write operations, always go wrong, the code below
The first button
Private Sub Command1_Click ()
Dim the ex As Object
Dim exwbook As Object
Set the ex=CreateObject (" Excel. Application ")
The Set exwbook=ex. Workbooks (). The Add
Ex. The Visible=True
Ex. Worksheets (" Sheet1 "). The Range (" a1 ")="a"
Ex. Worksheets (" Sheet2 "). The Range (" a1 ")="b"
Ex. Worksheets (" Sheet3 "). The Range (" a1 ")="c"
Ex. Sheets (" Sheet2 "). The Name="extraction results" 'Rename Sheet
End Sub
The second button
'to remove the first worksheet in the first column data
Private Sub Command2_Click ()
Dim the ex As Object
Dim exwbook As Object

Set the ex=CreateObject (" Excel. Application ") 'Create the file

Ex. The Workbooks. Open FileName:="D: \ vvv6 \ c.x LSX" 'Open file
Ex. The Visible=True 'show
Ex. Sheets (" Sheet1 "). The Range (" a65536 "). The Clear 'bank problems
End Sub

CodePudding user response:

What is error message?

CodePudding user response:

Teacher, hello, on a problem has been resolved, can help to see why msgbox output not sheet3 b1, but excel sheet current b1
Set Xlsheet=Xlbook. Worksheets (" Sheet3 ") 'specific sheet this sentence is where there are problems
Thank you very much


Private Sub Command1_Click ()
Dim Xlapp As Excel. Application
Dim Xlbook As Excel. The Workbook
Dim Xlsheet As Excel. The Worksheet
On the Error Resume Next
Set Xlapp=GetObject (" excel. Application "), 'open an existing object, this assumes only one excel file
If Xlapp Is Nothing Then 'open If you don't succeed, namely the excel file did not open
The Set Xlapp=CreateObject (" excel. Application ") 'build excel object
The Set Xlbook=Xlapp. Workbooks. Open (App) Path & amp; "\ test. XLSX")
Xlapp. Visible=True 'show excel
End the If
Set Xlsheet=Xlbook. Worksheets (" Sheet3 ") 'specific sheet
'Xlsheet. Activate
'Set Xlsheet=Xlbook. ActiveSheet
If Val (Xlapp. Application. Version) & gt; 8 Then=
The Set Xlsheet=Xlapp. ActiveSheet
The Else
The Set Xlsheet=Xlapp
End the If
The Debug. Print Xlsheet. Cells (1, 2)
MsgBox Xlsheet. Range (" B1 "). The Value
End Sub

CodePudding user response:

The following because of you, to Xlsheet assignment, appointed the active sheet
If Val (Xlapp. Application. Version) & gt; 8 Then=
The Set Xlsheet=Xlapp. ActiveSheet
The Else
The Set Xlsheet=Xlapp
End If
  •  Tags:  
  • VBA
  • Related