0

I'm getting an "application-defined or object-defined error" being thrown when I try to set a cell in my active sheet to a formula. I think its due to me trying to use the Sheets.Name function in the formula, see code below:

Public Sub getChannels()
    Dim lastRow As Long
    Dim i As Integer, counter As Integer
    Dim rng As Range, rngB As Range, rngC As Range
    Dim sht As Worksheet

    Dim test As String
    Set sht = Sheets("Summary Sheet - 30-07-2015")
    sht.Activate

    lastRow = sht.Cells(sht.Rows.Count, "B").End(xlUp).Row
    For counter = 1 To lastRow Step 3
        If ActiveSheet.Cells(counter, 12) = "LTE 2C" Then
            ActiveSheet.Cells(counter, 16) = _
                "=INDEX('LTE 2C'!C[55],MATCH(""'"" & sht.name &""'""!RC[-14],'LTE 2C'!C[-11],0))"
            ActiveSheet.Cells(counter, 17) = _
                "=INDEX('LTE 2C'!C[53],MATCH(""'"" & sht.name &""'""!RC[-15],'LTE 2C'!C[-12],0))"
            ActiveSheet.Cells(counter, 18) = _
                "=INDEX('LTE 2C'!C[55],MATCH(""'"" & sht.name &""'""!RC[-16],'LTE 2C'!C[-13],0))"

            Range("P" & counter & ":R" & counter).Select
            Selection.Copy
            Range("P" & counter + 1 & ":P" & counter + 2).Select
            ActiveSheet.Paste
        End If

        Next

End Sub

Am I missing something obvious?

2
  • Try replacing MATCH(""'"" & sht.name &""'""! with MATCH('" & sht.Name & "'! Commented Aug 7, 2015 at 13:35
  • Try ActiveSheet.Cells(Counter, 16) = _ "=INDEX('LTE 2C'!C[55],MATCH('" & sht.name & "'!RC[-14],'LTE 2C'!C[-11],0))" Commented Aug 7, 2015 at 13:37

2 Answers 2

1

Change your formula like this:

ActiveSheet.Cells(counter, 16) = _
    "=INDEX('LTE 2C'!C[55],MATCH(" & "'" & sht.name & "'" & "!RC[-14],'LTE 2C'!C[-11],0))"
ActiveSheet.Cells(counter, 17) = _
    "=INDEX('LTE 2C'!C[53],MATCH(" & "'" & sht.name & "'" & "!RC[-15],'LTE 2C'!C[-12],0))"
ActiveSheet.Cells(counter, 18) = _
    "=INDEX('LTE 2C'!C[55],MATCH(" & "'" & sht.name & "'" & "!RC[-16],'LTE 2C'!C[-13],0))"
Sign up to request clarification or add additional context in comments.

Comments

0

Nelly is correct, but another way to do this would be to simply remove the unnecessary extra string for each apostrophe and replace it with this:

ActiveSheet.Cells(counter, 16) = _
"=INDEX('LTE 2C'!C[55],MATCH('" & sht.name & "'!RC[-14],'LTE 2C'!C[-11],0))"

Where the apostrophes are just attached to the strings before and after. It really makes no difference, but it removes the extra (&)s.

1 Comment

Thank you this was very helpful! I was unaware you could use the apostrophe and quotation marks in that way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.