为什么是“A1”在VBA中的GetValue函数中使用? [英] Why is cell "A1" being used in this GetValue function in VBA?
问题描述
我正在使用此函数从封闭的工作簿中检索一个值。在这段代码的第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 theRange
object. For example, if the selection is cellC3
, thenSelection.Range("B1")
returns cellD3
because it’s relative to theRange
object returned by theSelection
property. On the other hand, the codeActiveSheet.Range("B1")
always returns cellB1
.
代码正在使用第二个 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屋!