在Excel中使用VB引用单元格 [英] Referencing A Cell using VB in Excel

查看:90
本文介绍了在Excel中使用VB引用单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图根据单选按钮的值填充新的工作表.如果启用了单选按钮,那么我希望新工作表中的单元格中的"N"以及该单选按钮的文本标签.如何引用存储值的单元格以及应调用的类是什么?任何示例代码都肯定会在紧迫的时间内提供帮助.

I am trying to populate a new worksheet based on the value of a radio button. If radio button is enabled then I want "N" in a cell in a new worksheet along with the text label of that radio button. How do I reference the cell in which to store value and what are the classes I should call? Any sample code will definitely help in a tight time frame.

推荐答案

如果这是VB而不是VB.NET的问题,则下面的代码显示了如何创建实例Excel应用程序的说明以及如何写入和读取数据.
If this is a question of VB, not VB.NET, the code below shows how to create instance of MS Excel application and how to write and read data.
Option Explicit

Sub ReadWriteExcel()
'declare variables
Dim oExc As Object, oWbk As Object, oWsh As Object

'catch errors
On Error GoTo Err_ReadWriteExcel

'set variables
'instance od excel
Set oExc = CreateObject("Excel.Application")
'add workbook object
Set oWbk = oExc.Workbooks.Add()
'set worksheet object - first in the collection
Set oWsh = oWbk.Worksheets(1)

'write username to cell A1
oWsh.Range("A1") = Environ("username")
'read username from cell A1
MsgBox "Welcome " & oWsh.Range("A1") & "!", vbInformation, "Information..."

'exit procedure
Exit_ReadWriteExcel:
    'ignore errors
    On Error Resume Next
    'show excel if object Excel instance was successfuly created
    If Not oExc Is Nothing Then oExc.Visible = True
    'clean up
    Set oWsh = Nothing
    Set oWbk = Nothing
    Set oExc = Nothing
    Exit Sub

'error handler/error catcher
Err_ReadWriteExcel:
    'show message
    MsgBox Err.Description, vbExclamation, Err.Number
    'go to exit procedure
    Resume Err_ReadWriteExcel
End Sub


这篇关于在Excel中使用VB引用单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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