什么是Excel R1C1参考的types?

我创build了一些用于Excel电子表格的VBA函数。 现在,函数的参数具有Stringtypes,所以我必须将我的列引用作为string传递。

不过,我更喜欢R1C1表示法(自Multiplan以来一直使用这种表示法),但我无法弄清楚如何将列引用传递给具有该表示法的VBA函数。 换句话说,我想定义一个函数,所以我可以用下面的方法来调用它

= FOO(C [-2])

所以我正在寻找正确的types为以下function

函数foo(ColRef as WhatTypeGoesHere)…

这将解释哪一个使用:)

 Dim ws As Worksheet Sub Sample() Set ws = ThisWorkbook.Sheets("Sheet1") Debug.Print foo1("B") '<~~ Col Name Debug.Print foo2(Range("B1")) '<~~ Range Debug.Print foo3(2) '<~~ Col Number Debug.Print foo4("R1C2") '<~~ RC Notation End Sub Function foo1(ColRef As String) As Long foo1 = ws.Cells(ws.Rows.Count, ColRef).End(xlUp).Row End Function Function foo2(ColRef As Range) As Long foo2 = ws.Cells(ws.Rows.Count, ColRef.Column).End(xlUp).Row End Function Function foo3(ColRef As Integer) As Long foo3 = ws.Cells(ws.Rows.Count, ColRef).End(xlUp).Row End Function Function foo4(ColRef As String) As Long Dim MYAr MYAr = Split(ColRef, "C", , vbTextCompare) foo4 = ws.Cells(ws.Rows.Count, Val(MYAr(UBound(MYAr)))).End(xlUp).Row End Function 

截图

在这里输入图像说明

编辑:

需要注意的是,“C [-2]”不是Column B而是当前单元格的偏移量。 这是另一个可以处理不同RC符号的例子。 请注意,我没有做任何error handling,但我相信你可以处理。 此代码的目的是显示如何使用RC符号查找最后一行

 Dim ws As Worksheet Sub Sample() Set ws = ThisWorkbook.Sheets("Sheet1") Debug.Print foo4("C[-2]") Debug.Print foo4("RC[-2]") Debug.Print foo4("R[-1]C[-2]") End Sub Function foo4(ColRef As String) As Long Dim MYAr, sTmp As String Dim colNo As Integer Dim rng As Range '~~> I am assuming that you will pass a `C`. If not then '~~> Add an error handling here MYAr = Split(ColRef, "C", , vbTextCompare) If InStr(1, MYAr(UBound(MYAr)), "[") Then tmpAr = Split(MYAr(UBound(MYAr)), "[")(1) tmpAr = Split(tmpAr, "]")(0) colNo = Val(Trim(tmpAr)) Else colNo = MYAr(UBound(MYAr)) End If Set rng = ActiveCell.Offset(, colNo) foo4 = ws.Cells(ws.Rows.Count, rng.Column).End(xlUp).Row End Function 

在你的情况下,使用Range

您可以指定任何数据types,Excel将根据需要隐式转换。 这真的取决于function是什么。 例如,如果函数使用一组单元格或单元格,则使用Range ,如果该function去掉字符,则使用String