简体   繁体   English

在VBA Excel中使用索引公式和名称的引用

[英]Reference to use index formula and name in VBA Excel

I want to ask, how about the code in writing VBA Excel like this. 我想问一下,编写这样的VBA Excel的代码如何。

Textbox1=Application.WorksheetFunction.Index(dataname, Application.WorksheetFunction.Match(SpinButton1.Value, indexdataname, 0), 3)

In simple formulas on sheets like 在简单的公式上像

=INDEX(dataname,MATCH(number,indexdataname,0),3)

I assume indexdataname and dataname are named ranges. 我认为indexdatanamedataname的命名范围。

It would work with something like below: 它将与如下所示一起工作:

Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1") 'define sheet name

Dim Dataname As Range
Set Dataname = ws.Range("dataname")

Dim IndexDataname As Range
Set IndexDataname = ws.Range("indexdataname")

Dim MatchedRow As Double
On Error Resume Next 'next line throws error if no match is found
MatchedRow = Application.WorksheetFunction.Match(SpinButton1.Value, IndexDataname, 0)
On Error Goto 0 'always re-activate error reporting !!!

'test if something matched
If MatchedRow = 0 Then
    MsgBox "Nothing matched", vbCritical
    Exit Sub
End If

Textbox1 = Application.WorksheetFunction.Index(Dataname, MatchedRow, 9)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM