Harry12345
Harry12345

Reputation: 1160

Store location of cell address to variable in VBA

I'm using VBA in Excel and I'm using a function to find the first empty row then adding some values, after that I need to pass the address of the cell to another function but using the code below I get a runtime error. firstEmptyRow is a function that returns a range,e.g. $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

What is the correct way to save the address of the cell so I can pass it to another function? Below is the code for 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

Upvotes: 2

Views: 100833

Answers (3)

Wayne Clark
Wayne Clark

Reputation: 11

I am a total "noob" at vba and learning by trying code snipits and routines that appear to to me to answer to a problem I am stuck on. When I tried the code posted by Vityata I encountered "Run-time error '424': Object Required at the following stmts:

    Set myCell = Sheet.Range(coltoSearch & i)
    Set FirstEmptyRow = Sheet.Range(coltoSearch & i + 1)

Changing the stmts as follows resolved the errors for me:

    Set myCell = wks.Range(coltoSearch & i)
    Set FirstEmptyRow = wks.Range(coltoSearch & i + 1)

I also added a Debug.Print stmt between the last End If and the End Function stmts so I could quickly see the result when testing as follows:

        End If
        Debug.Print "The 1st empty cell address is "; coltoSearch & i
    End Function

If I have violated some posting rule, please accept my apology. Hopefully sharing this will avoid future confusion by others who use a similar learning process.

Upvotes: 1

Vityata
Vityata

Reputation: 43585

You do not need to overcomplicate the code with something like Set addCell = Worksheets("Schedule").Range(.Address) because it really makes the readability a bit tough. In general, try something as simple as this: Set FirstEmptyRow = myCell. Then the whole code could be rewritten to this:

Public Function FirstEmptyRow() As Range

    Dim myCell As Range
    Dim coltoSearch As String, lastRow As String
    Dim wks As Worksheet

    Set wks = Worksheets(1)
    coltoSearch = "A"

    Dim i As Long
    For i = 3 To wks.Range(coltoSearch & Rows.Count).End(xlUp).Row
        Set myCell = sheet.Range(coltoSearch & i)
        If IsEmpty(myCell) Then
            Set FirstEmptyRow = myCell
            Exit For
        End If
    Next i

    If i = 2 ^ 20 Then
        MsgBox "No empty rows at all!"
    Else
        Set FirstEmptyRow = sheet.Range(coltoSearch & i + 1)
    End If

End Function

The last part of the code makes sure, that if there is no empty cell before the last row and row number 3, then next cell would be given as an answer. Furthermore, it checks whether this next row is not the last row in Excel and if it is so, it gives a MsgBox() with some information.

Upvotes: 0

Gareth
Gareth

Reputation: 5243

The .Address property returns a string so you'll need to to set the addCell variable like so:

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

Upvotes: 10

Related Questions