开发者

Coding for Excel sheet in Visual Basic 6

开发者_JS百科

i want to get value of Column A of excel sheet1 into some variable of visual basic and then after changing this value then send back to next sheet2


This is a complete and working project example that will copy the value from Sheet1, Cell A1 to 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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜