Inserted Module not pointing to correct module

depcdivr

Active Member
Joined
Jan 21, 2008
Messages
343
Office Version
  1. 365
Platform
  1. Windows
I found some code that will allow me to import a module into a workbook from a network folder. However when I try to use my VBA code to assign a macro to an autoshape it is trying to look for the macro in the original workbook not the new one where the macro resides.

Here is the code that I am using.

Code:
sub test()
InsertVBComponent ActiveWorkbook, "R:\SPC Log\SPC_Module.bas"
    ActiveSheet.Shapes.AddShape(msoShapeRectangle, 228#, 557.25, 126#, 13.5).Select
    With Selection
        .Width = 110
        .Height = 18
        .Top = Range("D34").Top
        .Left = Range("D34").Left
        .Characters.Text = "Click to Insert Name"
        .ShapeRange.Fill.Visible = msoTrue
        .ShapeRange.Fill.ForeColor.SchemeColor = 43
        .ShapeRange.Line.Visible = msoTrue
        .ShapeRange.Line.ForeColor.SchemeColor = 64
        .ShapeRange.Line.BackColor.RGB = RGB(255, 255, 255)
        .Name = "Prepared_by_box"
        .OnAction = "Prepared_by_box"
     End With
end sub
 
Sub InsertVBComponent(ByVal wb As Workbook, ByVal CompFileName As String)
    If Dir(CompFileName) <> "" Then
        On Error Resume Next
        wb.VBProject.VBComponents.Import CompFileName
        On Error GoTo 0
    End If
    Set wb = Nothing
End Sub

Any suggestions on how I can get it to point to the macro in the newly imported module?
 
Last edited:

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
When you use OnAction, it is safest to always specify the workbook name as well:
Code:
.OnAction = "'" & Activeworkbook.name & "'!Prepared_by_box"
 
Upvote 0
That worked. The problem was that it was pointing to the current workbook where my macro was running not the new workbook I was creating. This fixed the issue. Thanks.
 
Upvote 0

Forum statistics

Threads
1,203,752
Messages
6,057,150
Members
444,908
Latest member
Jayrey

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