为什么是“A1”在VBA中的GetValue函数中使用? [英] Why is cell "A1" being used in this GetValue function in VBA?

查看:928
本文介绍了为什么是“A1”在VBA中的GetValue函数中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此函数从封闭的工作簿中检索一个值。在这段代码的第8行中,我不明白为什么使用A1。在整个第八行发生了什么?我也对xlR1C1参数感到困惑。

I'm using this function to retrieve a value from a closed workbook. In this 8th line of this code, I don't understand why "A1" is being used. What exactly is happening in that entire 8th line? I'm confused by the xlR1C1 argument as well.

Private Function GetValue(path, file, sheet, ref)

    Dim arg As String
    If Right(path, 1) <> "\" Then path = path & "\"
    If Dir(path & file) = "" Then
        GetValue = "File Not Found"
        Exit Function
    End If
    arg = "'" & path & "[" & file & "]" & sheet & "'!" & _
      Range(ref).Range("A1").Address(, , xlR1C1)
    GetValue = ExecuteExcel4Macro(arg)

End Function


推荐答案

Range()。Range() 此处的文档


当应用于 Range 对象时,该属性相对于范围对象。例如,如果选择是单元格 C3 ,则 Selection.Range(B1)返回单元格 D3 ,因为它与 Selection 属性返回的 Range 对象相对。另一方面,代码 ActiveSheet.Range(B1)总是返回单元格 B1

When applied to a Range object, the property is relative to the Range object. For example, if the selection is cell C3, then Selection.Range("B1") returns cell D3 because it’s relative to the Range object returned by the Selection property. On the other hand, the code ActiveSheet.Range("B1") always returns cell B1.

代码正在使用第二个 Range(A1),以确保如果您有一个 ref 范围大于一个单元格,它只返回该范围的左上角单元格。还会出现您的其他 Sub 调用 ExecuteExcel4Macro()需要一个 R1C1 类型单元格引用,所以地址正在转换为该类型,以将 arg 字符串传递到 Sub 中。

The code is using that second Range("A1") to ensure that if you have a ref range larger than one cell, it only returns the top left cell of that range. Also it would appear your other Sub called ExecuteExcel4Macro() requires an R1C1 type cell reference so the address is being converted into that type for passing the arg string into the Sub.

这篇关于为什么是“A1”在VBA中的GetValue函数中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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