我希望将excel sheet1的A列值变为visual basic的某个变量,然后在更改此值后再发送回下一个sheet2
答案 0 :(得分:2)
这是一个完整且有效的项目示例,它将Sheet1,Cell A1中的值复制到Sheet2,Cell A1:
' declare these variables & you need to add a reference
' to the microsoft excel 'xx' object library.
' you need two command buttons: cmdCopy and cmdSave
' on the form, an excel file in c:\book1.xls
Dim xl As New Excel.Application
Dim xlsheet As Excel.Worksheet
Dim xlwbook As Excel.Workbook
Private Sub cmdCopy_Click()
Dim temp As String
temp = xlsheet.Cells(1, 1) ' row 1 col 1
' TODO: process the value stored in the variable 'temp'
temp = temp & "-changed" ' in this case "<Sheet1-CellA1-value>-changed"
' Open Sheet2
Set xlsheet = xlwbook.Sheets.Item(2)
' write the value to cell A1 on Sheet2
xlsheet.Cells(1, 1) = temp
End Sub
Private Sub cmdSave_Click()
xlwbook.Save
' You MUST do this or you will not be able to open
' c:\book1.xls again, untill you restart Windows OR
' kill EXCEL.EXE with the Task Manager
xl.ActiveWorkbook.Close False, "c:\book1.xls"
xl.Quit
End Sub
Private Sub Form_Load()
Set xlwbook = xl.Workbooks.Open("c:\book1.xls")
Set xlsheet = xlwbook.Sheets.Item(1)
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set xlwbook = Nothing
Set xl = Nothing
End Sub