VB.net Office解决方案-访问工作表中命名范围内的值 [英] VB.net Office Solution - Accessing value in named Range in a Worksheet

查看:87
本文介绍了VB.net Office解决方案-访问工作表中命名范围内的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Excel VBA项目转换为VB.net Office解决方案.在VBA中,访问工作表中的命名范围非常容易.
Sheet1.Range("NamedRange")
会为您提供该单元格中的价值.

I'm working on converting an Excel VBA project to a VB.net Office Solution. In VBA accessing a named range within a sheet was pretty easy.
Sheet1.Range("NamedRange")
would give you the value in that cell.

在VB.net中,我尝试了几种不同的方法,所有方法均以失败告终.

In VB.net I've tried several different things, all ending in failure.

我从这里开始. http://msdn.microsoft.com/zh-cn/library/aa168292(v = office.11​​).aspx#excelobj_link9

我正在使用其中一个Visual Studio Excel 2010文档模板,并且设法在工作表上的命名范围内设置了一个值.该模板为工作表上的每个选项卡创建一个类,因此我能够做到这一点.

I'm using one of the Visual Studio Excel 2010 Document template, and I managed to set a value in a named range on a sheet. The template creates a class for each tab on the worksheet, so I was able to do this.

Me.Range("A5").Value

我遇到的麻烦是如何访问工作表上的范围,特别是在访问工作表之前,我无法弄清楚需要创建哪些对象.以下是行不通的:)

What I'm having trouble with is how to access a range on a worksheet, in particular I can't figure out what objects I need to create before I can access a worksheet. The following is what does not work:)

Public Sub GetInputs()
    Dim XlSheet1 As Excel.Worksheets
    Xlval = XlSheet1("Sheet1").Range("NamedRange").Value

这给我留下了null引用...我已经尝试了其他几件事,并且遇到了其他错误.朝正确方向的任何推动将不胜感激.

This leaves me with null reference... I've tried several other things and gotten several other errors. Any push in the correct direction would be much appreciated.

推荐答案

以下代码编译并在单独的代码模块中运行.关键是使用Globals对象访问Excel应用程序对象:

The following code compiles and runs in a separate code module. The key is using the Globals object to access the Excel application objects:

Dim xlWorksheet As Excel.Worksheet
Dim xlNamedRange As Excel.Range
Dim xlVal As Object

xlWorksheet = Globals.ThisWorkbook.Worksheets("Sheet1")
xlNamedRange = xlWorksheet.Range("NamedRange")
xlVal = xlNamedRange.Value
MessageBox.Show(xlVal)

这篇关于VB.net Office解决方案-访问工作表中命名范围内的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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