vba - 将单元格地址的位置存储到 VBA 中的变量中

标签 vba excel runtime-error

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

保存单元格地址以便我可以将其传递给另一个函数的正确方法是什么?下面是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 属性返回一个字符串,因此您需要设置 addCell 变量,如下所示:

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

关于vba - 将单元格地址的位置存储到 VBA 中的变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24735803/

相关文章:

html - 无法将正确的网站数据导入excel

excel - VBA Selenium with Chrome - 将文件上传到网页

VBA RtlMoveMemory 仅适用于 ByVal

java - 线程 "main"java.lang.IllegalAccessError : failed to access class 中出现异常

sql - 错误 "<file-name> already exists with <field/value...> (132)"

vba - 创建压缩(或压缩)文件夹

excel - VBA 自函数返回#VALUE!单元格出错,而在函数窗口中正确返回实际值

vba - 如何使 Excel 单元格等于空(可能通过 VBA 代码)

r - R 中的 .jnew 和 j.check 中的错误中的 write.xlsx 错误

python - 列表索引类型错误