2011-03-17 98 views
-2

我想要得到的Excel工作表Sheet1的A列的值到Visual Basic,然后一些变量改变这个值后,再发回下一个Sheet2中在Visual Basic 6

+0

您用什么来加载电子表格? Excel自动化? – Rup 2011-03-17 11:38:05

回答

2

这是一个完整的工作编码的Excel工作表项目示例将复制从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 
+0

@ Zaheer4444请给上面的答案一个复选标记,如果它解决了你的问题。谢谢。 – 2011-05-25 15:07:30