在 Visual Basic 6 中为 Excel 工作表编码 [英] Coding for Excel sheet in Visual Basic 6

查看:22
本文介绍了在 Visual Basic 6 中为 Excel 工作表编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将excel sheet1的A列的值转换为visual basic的某个变量,然后在更改此值后发送回下一个sheet2

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

推荐答案

这是一个完整且有效的项目示例,它将值从 Sheet1, Cell A1 复制到 Sheet2, Cell A1:

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

这篇关于在 Visual Basic 6 中为 Excel 工作表编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆