【发布时间】:2019-04-06 01:46:58
【问题描述】:
我很难弄清楚如何将动态范围传递给公式。
通常我会有一个场景,我知道我的范围将在哪一列(下面的示例),其中 LR 是我范围内的行数。
Range("A1:A" & LR).FormulaR1C1 = ....some formula here
当我必须使用(在我的情况下)列标题名称动态创建范围时,问题就开始了。我可以通过标题名称找到列,获取列号,将其转换为字母,但我尝试过的任何解决方案都不起作用。
这是我用来获取列号的:
Function getColumn(searchText As String, wsname As String) As Integer
Set aCell = Sheets(wsname).Rows(1).Find(what:=searchText, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If aCell Is Nothing Then
MsgBox ("Column was not found. Check spelling")
Exit Function
Else
getColumn = aCell.Column
End If
End Function
我用这段代码把它转换成字母:
Function Col_Letter(lngCol As Long) As String
Dim vArr
vArr = Split(Cells(1, lngCol).Address(True, False), "$")
Col_Letter = vArr(0)
End Function
现在,为了在范围内填充公式,我尝试了类似的方法,但不幸的是没有运气:
colLetter = Col_Letter(manuallyAdjustedNumber)
Range(colLetter & "2:" & colLetter & LR).FormulaR1C1 = "=EXACT([RC-1],[" & harnessDrawingNumber & " - " & manuallyAdjustedNumber & "])"
我会感谢任何帮助!谢谢!
【问题讨论】:
-
您能否确认您从 Col_Letter() 函数中获得了正确的字母值?
-
Debug.Print该公式并尝试在工作表中使用它。这不是你想的那样。您的括号已关闭。