将单元格地址的位置存储到VBA中的variables中

我在Excel中使用VBA,我正在使用一个函数来查找第一个空行,然后添加一些值,之后,我需要将单元格的地址传递给另一个函数,但使用下面的代码,我得到一个运行时错误。 firstEmptyRow是一个返回范围的函数,例如$A$280

 Dim addCell as Range 'Find first empty row in the table With firstEmptyRow 'Enter Values .Value = taskID .Offset(0, 1).Value = jobID .Offset(0, 2).Value = jobName .Offset(0, 6).Value = taskTypeID .Offset(0, 8).Value = taskName .Offset(0, 9).Value = desc .Offset(0, 11).Value = estMins .Offset(0, 13).Value = "No" set addCell = .Address //Gives a runtime error End With 

什么是正确的方式来保存单元格的地址,所以我可以传递给另一个function? 以下是firstEmptyRow的代码

 Public Function firstEmptyRow() As Range 'Returns the first empty row in the Schedule sheet Dim i, time As Long Dim r As Range Dim coltoSearch, lastRow As String Dim sheet As Worksheet Set sheet = Worksheets("Schedule") time = GetTickCount coltoSearch = "A" For i = 3 To sheet.Range(coltoSearch & Rows.Count).End(xlUp).Row Set r = sheet.Range(coltoSearch & i) If Len(r.Value) = 0 Then Set firstEmptyRow = sheet.Range(r.Address) 'r.Select Exit For 'End the loop once the first empty row is found End If Next i 'Debug.Print "firstEmptyRow time: " & GetTickCount - time, , "ms" End Function 

.Address属性返回一个string,所以你需要像这样设置addCellvariables:

 Set addCell = Worksheets("Schedule").Range(.Address)