Sub called from another workbook (Insert new line)

csokosdama

New Member
Joined
Jan 12, 2015
Messages
4
Hi everyone,

I'm trying to call in the parent Workbook (test) a sub in another Workbook (test2). This sub is supposed to insert a new line the test2. That is where the code is originally located. If I run the code seperately it works fine. If I try to call it from test, it doesn't. Do you have any solutions?

Thank you very much!

test.xlsm:

Sub testing()
Application.Run "test2.xlsm!Sheet1.newline()"
End Sub

test2.xlsm:
Sub newline()
Rows("5:5").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Thank you!

Well, I don't get any failure messages, so I suppose the sub is being called however, no row is inserted.
I also inserted some other codes newline() that fill a cell with text and it does that.
 
Upvote 0
What other code did you add? If your VBA code is in a Worksheet module that worksheet will be used if references are unqualified.
 
Upvote 0
The code looks the following:

test.xlsm:

Sub testing()
Windows("test2.xlsm").Activate
Application.Run "test2.xlsm!Sheet1.newline()"
End Sub

test2.xlsm:

Sub newline()
Rows("5:5").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Cells(5, 7) = "Hello World!"
End Sub

I writes the "Hello World!" in the specified cell, however still no new row is added.
Btw. I'm using Excel 2010, if that makes any difference.

The VBA code in test.xlsm is in a separate module. In test2.xlsm the code is in the Worksheet.
Could that be the source of the problem?
 
Upvote 0
As I said before, if your <ACRONYM title="visual basic for applications">VBA</ACRONYM> code is in a Worksheet module that worksheet will be used if references are unqualified.
 
Upvote 0
I went by the code in post 1.

As I said before, if your <ACRONYM title="visual basic for applications">VBA</ACRONYM> code is in a Worksheet module that worksheet will be used if references are unqualified.

As Andrew says, if the called procedure is in a worksheet's module, non-qualified references refer to stuff belonging to that sheet; including Selection...

I am drawing a blank on why inclusion of the parenthesis in 'Application.Run "Book2.xlsm!Sheet1.newline()"' isn't drawing an error, but regardless, they should not be included in the string argument.

For simplicity, I used Book1 (calling) and Book2 (called). This seems to work:

Presuming both are open, in a Standard Module in Book1.xlsm:

<font face=Courier New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br>  <br><SPAN style="color:#00007F">Sub</SPAN> testing()<br>  Application.Run "Book2.xlsm!Sheet1.newline"<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>

In Sheet1's module in Book2.xlsm:

<font face=Courier New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br>  <br><SPAN style="color:#00007F">Sub</SPAN> newline()<br>  Me.Rows(5).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>

Hope that helps,

Mark
 
Upvote 0

Forum statistics

Threads
1,215,791
Messages
6,126,930
Members
449,349
Latest member
Omer Lutfu Neziroglu

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