Coding for Excel sheet in Visual Basic 6

匿名 (未验证) 提交于 2019-12-03 01:29:01

问题:

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

回答1:

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 "-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 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!