What is wrong with my VBA?

aaronglover

Board Regular
Joined
Sep 15, 2005
Messages
130
I am getting a run-time error 438 with no option to debug (just end.) I know the issue is with my linkedcell property but I dont know what is wrong with it.

Thanks

Code:
Sub AddButtonAndCode()
     ' Declare variables
    Dim i As Long, Hght As Long
    Dim Name As String, NName As String
     ' Set the button properties
    i = 0
    Hght = 305.25
     ' Set the name for the button
    NName = "cmdAction" & i
     ' Test if there is a button already and if so, increment its name
    For Each OLEObject In ActiveSheet.OLEObjects
        If Left(OLEObject.Name, 9) = "cmdAction" Then
            Name = Right(OLEObject.Name, Len(OLEObject.Name) - 9)
            If Name >= i Then
                i = Name + 1
            End If
            NName = "cmdAction" & i
            Hght = Hght + 27
        End If
    Next
     ' Add button
    Dim myCmdObj As OLEObject, N%
    With Range("c4")
    Set myCmdObj = ActiveSheet.OLEObjects.Add(ClassType:="Forms.OptionButton.1", _
    Link:=False, DisplayAsIcon:=False, Left:=.Left + 0.5, Top:=.Top - 0.5, _
    Width:=.Width, Height:=.Height)
    End With
     ' Define buttons name
    myCmdObj.Name = NName
     ' Define buttons caption
    myCmdObj.Object.Caption = ""
  
    myCmdObj.Object.LinkedCell = "A1"
    
    With ThisWorkbook.VBProject.VBComponents(ActiveSheet.CodeName).CodeModule
        N = .CountOfLines
        .InsertLines N + 1, "Private Sub " & NName & "_Click()"
        .InsertLines N + 2, vbNewLine
        .InsertLines N + 3, vbTab & "MsgBox(" & """" & "Button Clicked!" & """" & " & vbCrLf &" & _
        """" & "Put your code here!" & """" & " & vbCrLf & " & """" & "This is " & """" & _
        "& " & """" & NName & """" & ")"
        .InsertLines N + 4, vbNewLine
        .InsertLines N + 5, "End Sub"
   End With
End Sub
 
Last edited by a moderator:

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Maybe I'm missing something here, but it looks like you are adding a button to the worksheet? If that's the case, then there is no linked cell property available for a command button.

HTH
Cal
 
Upvote 0

Forum statistics

Threads
1,214,929
Messages
6,122,314
Members
449,081
Latest member
tanurai

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top