Selecting the right module

Pete81

New Member
Joined
Aug 27, 2015
Messages
26
I have a code which exports a module from a source workbook ("Directory Creation Test.xlsm"), then imports it to a new workbook, assigning he module to a button in the new book. The problem I am having is that when I click the button, the code from the source workbook is used instead. I need the selection.onaction part of the code to select specifically the module in the new workbook. Here's what I have so far:

Code:
Dim FName As String

FName = Range("A1") & ".xlsm"

Workbooks.Add.SaveAs Filename:="C:\Folders\" & FName, FileFormat:=52


Workbooks("Directory Creation Test").VBProject.VBComponents("Module6").Export Filename:=("C:\Folders\Modules\ReturnData.bas")
Workbooks(FName).VBProject.VBComponents.Import ("C:\Folders\Modules\ReturnData.bas")

ActiveSheet.Buttons.Add(100, 100, 47.25, 47.25).Select
    Selection.OnAction = Workbooks(FName) & "!ReturnData2"
    Selection.Characters.Text = "Return Data"

Running this code, I get run-time error 438: Object doesn't support this property or method, and seems to indicate that the Selection.OnAction line is the issue. Where is my code wrong?
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Try...

Code:
[color=darkblue]With[/color] ActiveSheet.Buttons.Add(100, 100, 47.25, 47.25)
    .OnAction = "'" & FName & "'!ReturnData2"
    .Caption = "Return Data"
[color=darkblue]End[/color] [color=darkblue]With[/color]

Hope this helps!
 
Upvote 0
Try...

Code:
[color=darkblue]With[/color] ActiveSheet.Buttons.Add(100, 100, 47.25, 47.25)
    .OnAction = "'" & FName & "'!ReturnData2"
    .Caption = "Return Data"
[color=darkblue]End[/color] [color=darkblue]With[/color]

Hope this helps!

Hi Domenic,

It worked perfectly. Thank you so much. If you have the time, could you explain why it worked, and what the additional apostrophes mean in the code?
 
Upvote 0
In your original code, you're trying to concatenate an object (Workbooks(FName) returns a Workbook object) and a string, which you can't do. And then trying to assign it to the OnAction property, which requires a string. The apostrophes are added in case the filename contains spaces. Although, it looks like in this case they're automatically included when needed.
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,580
Members
449,039
Latest member
Arbind kumar

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