user29811
user29811

Reputation: 13

Compile Error - Variable Required-can't assign to this expression

Here's a code I'm replicating:

Private Sub btnRefresh_Click()
    Dim W As Worksheet: Set W = ActiveSheet
    Dim Last As Integer:  Last = W.Range("A100").End(xlUp).Row
    If Last = 1 Then Exit Sub
    Dim Symbols As String
    Dim i As Integer
    For i = 2 To Last
        Symbols = Symbols & W.Range("A" & i).Value & "+"
    Next i
    Symbols = Left(Symbols, Len(Symbols - 1))
    Debug.print Last 
    Debug.print Symbols
End Sub

I'm pretty sure that the issue is in the third line. I had originally written

Dim Last as Integer: Set Last = W.Rang("A100").End(xlUp).Row

I realized that Set was only for objects and so I removed it. Now I receive the error message:

Compile Error. Variable Required-cannot assign to this expression.

Any idea on what is wrong?

Upvotes: 1

Views: 3447

Answers (1)

Sobigen
Sobigen

Reputation: 2169

It's the line:

Symbols = Left(Symbols, Len(Symbols - 1))

You're trying to subtract a number from a string. I think it should be

Symbols = Left(Symbols, Len(Symbols) - 1)

To subtract from the length of Symbols When you get a compile error it'll highlight the line that the error is on. In my case it highlighted the minus sign.

Upvotes: 1

Related Questions