使用公式中的可变参数

好吧,我想我在这里错过了一些东西。 我试图在variables中使用一个简单的加法公式。 首先,我在第3行调用“Jan Expense Hours”search列标题。MsgBox ColL返回字母“I”,MsgBox ColL2返回字母“J”,两者都是正确的。 lRow回来,第55行也是正确的。 Worksheets("Calcs").Range("F4:F" & lRow).Formula = "=SUM('Resource Details'! & [ColL] & 4: & [ColL2] & 4)"当您尝试将这些variables添加到Worksheets("Calcs").Range("F4:F" & lRow).Formula = "=SUM('Resource Details'! & [ColL] & 4: & [ColL2] & 4)"我在这行代码中得到了应用程序定义或对象定义的错误。 有没有人有一个想法我做错了什么? 顺便说一句,我正在寻找列标题,因为列确实在各种副本上移动。

完整程序:

 Sub JanTotHrsFind() Dim lRow As Long Dim lCol As Long Dim strSearch As String Dim aCell As Range Dim ColL As String Dim ColL2 As String Dim ColNo As Long Sheets("Resource Details").Activate 'find the column strSearch = "*Jan Expense Hours*" Set aCell = Sheets("Resource Details").Rows(3).Find(What:=strSearch, LookIn:=xlValues, _ LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=True, SearchFormat:=False) 'convert column number to letter ColNo = aCell.Column ColL = Split(Cells(, ColNo).Address, "$")(1) ColL2 = Split(Cells(, (ColNo + 1)).Address, "$")(1) 'adds one more column to right MsgBox ColL MsgBox ColL2 lRow = Cells.Find(What:="SUBTOTAL*", _ After:=Range(ColL & "4"), _ LookAt:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, _ MatchCase:=False).Row - 1 'minus 1 row to move above MsgBox "Last Row: " & lRow 'formula for Jan Expense Hours + Jan Capital Hours 'Worksheets("Calcs").Range("F4:F" & lRow).Formula = "=SUM('Resource Details'!I4:J4)" 'Worksheets("Calcs").Range("F4:F" & lRow).Formula = "=SUM('Resource Details'![" & ColL & "]4:[" & ColL2 & "]4)" Worksheets("Calcs").Range("F4:F" & lRow).Formula = "=SUM('Resource Details'! & [ColL] & 4: & [ColL2] & 4)" End Sub 

你不应该写括号内的variables。

所以:

 Worksheets("Calcs").Range("F4:F" & lRow).Formula = "=SUM('Resource Details'!" & [ColL] & "4:" & [ColL2] & "4)" 

你可以请尝试你的代码,因为我纠正上面,看看它是如何去的。