我希望将excel文件的Sheet1的A列值改为Variable字符串,并在更改其值后,我想将其粘贴到同一个excel文件的另一个Sheet2的A列中。
答案 0 :(得分:1)
以下是您需要做的事情:
首先确保通过访问以下内容在VB6引用中引用EXCEL: 项目>参考> Microsoft Excel XX对象库
其中XX是PC上安装的Office版本。
Dim objEX As New Excel.Application
objEX.Visible = True
' Optional if you want to see what is going on in EXCEL
' while your code is being executed.
objEX.Workbooks.Open "C:\My Files\Filename.xls"
'Make sure you put the right path of the excel workbook you want to open
With objEX
'If you know the name of the sheet you want to read from
' then use this code
.Sheets("FIRST_Sheet_Name_Here").Activate
'Otherwise, you may only know that the sheet is
' physically the first sheet in that workbook, then use this code:
.Sheets(1).Activate
Dim myValue As Double
myValue = .Range("A1").Value
' Change A1 to whatever Cell you want to read from the sheet
'Now we will do something with the value that we read and
' then will save it back to EXCEL in sheet 2 and cell B2 for example
myValue = Val(myValue) + 1000
.Sheets("SECOND_Sheet_Name_Here").Activate
'if you know that name of the second sheet
.Sheets(2).Activate
'if you don't know the name, but know the location
.Range("B2").Value = myValue
' This will write the variable to the location B2 in sheet 2
.ActiveWorkbook.Save
'Saves the changes you have done
.ActiveWorkbook.Close
'Close the workbooks but keeps Excel application open
.Quit
'Quits excel instance and releases the process from task manager
End With
Set objEX = Nothing
'Garbage Collection and making sure memory is released to other processes.