Please...Need help running macro in another workbook.

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
my current code which does not work:

Code:
Sub Test1()
Dim wb As Workbook, x As String
For Each wb In Workbooks
If wb.Name <> ThisWorkbook.Name Then x = wb.Name
Next wb
MsgBox "The other open workbook is named " & x & "Book2" & vbCrLf & _
"Click OK to activate it."
Workbooks(x).Activate
Cells.Activate
Range("A1").Value = "Hello!"
Range("A3").Select
Range("A3").Formula = "=A1"
Workbooks(x).AcceptAllChanges
Workbooks(x).Save
Workbooks(x).Close
End Sub

Please advice how it works!
Pedies
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
I was expecting the macro macro might haved saved hello! in cell A1, but when i open Book2 it is not.

Am I doing somthing wrong????
 
Upvote 0
I have book2 closed which is why it was creating prob. How can i make the macro open book2 by itself?
When I open book2 & run the macro it is perfectly okay but does not work when it is closed
 
Upvote 0
It works for me, both workbooks must be open.
Also the code assumes that the second workbook is "Book2", this is dangerous as it loops through all open workbooks.
And if you are not vigilant,(careful), you could overwrite data.

EDIT: This reply is for post #5
 
Last edited:
Upvote 0
This will open Book2.xls. It assumes that Book2.xls is saved in the same location as the current workbook. If not, you'll have to edit the path and file name in Red.

Code:
Sub Test2()

    Dim wb As Workbook, MyFile As String
    
    MyFile = [COLOR="Red"]ThisWorkbook.Path & "\Book2.xls"[/COLOR]
    On Error Resume Next
        Set wb = Workbooks.Open(Filename:=MyFile)
    On Error GoTo 0
    
    If wb Is Nothing Then MsgBox "Couldn't locate " & MyFile: Exit Sub
    
    With wb
        '.Activate
        '.Cells.Activate
        With ActiveSheet
            .Range("A1").Value = "Hello!"
            .Range("A3").Formula = "=A1"
        End With
        .Save
        .Close
    End With
    
End Sub
 
Upvote 0
Sure...thanks alot for inputting.
I was like waiting for you guys!!!!;)
 
Upvote 0
Okay that worked perfectly...
Is there a way i can assigned a password for book2 in book1 macro so that even if book2 is locked with password it opens it??;)
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,817
Members
449,049
Latest member
cybersurfer5000

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