我很难弄清楚如何将动态范围传递到公式中。
通常我会有一个场景,我知道我的范围将在哪一列中(如下例所示),其中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 & "])"我将感谢任何帮助!谢谢!
发布于 2018-11-02 03:36:28
你让这个方式变得太复杂了。我完全不明白为什么要使用R1C1表示法,同时还要来回转换从列字母到列号的所有内容。
你有两个问题:
它应该看起来像这样:
Public Function GetColumn(searchText As String, wsname As String) As Integer
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
With ws
If LCase$(.Name) = LCase$(wsname) Then
Dim header As Range
Set header = .Rows(1).Find(searchText, , xlValues, xlWhole, xlNext, False)
If Not header Is Nothing Then
GetColumn = header.Column
Exit Function
End If
MsgBox ("Column was not found. Check spelling")
Exit Function
End If
End With
Next
MsgBox "Worksheet '" & wsname & "' not found."
End Functionhttps://stackoverflow.com/questions/53108002
复制相似问题