switching to already open file in vba

cocopops2001

Board Regular
Joined
Apr 18, 2011
Messages
112
hey there, i have a workbook which is a sort of MENU/Database of various calculation sheets and for the most part it works fine.

what the book does is allows the user to select which calculation sheet they want and then click a button which asks the user what they want done with the sheet.

they have the option of creating a new book (works fine with no problems), opening an existing(closed) book(works fine too) or using a book that is already open(doesnt work so well).

these options are called fron a userform which has a dropdown box for the open files and a few buttons for the other functions. how do i switch to the open work book?

below is the code for the OK button which they push after selecting the open workbook

Code:
Private Sub CommandButton1_Click()
    
    Dim Myfile As String
        
    Myfile = ComboBox1.Value
    
    '''opens new book
    Myfile.Sheets(1).Activate
    
    '''''checks if cover sheet exists and if not creates one
    covercheck
    
    ''''copies desired sheet
    ThisWorkbook.ActiveSheet.Range("a11:n70").Copy
    
    '''''copies desired sheet
    sheetcopy
     
    UserForm3.Hide
    
End Sub

it worked fine when the OK button just showed a msgbox but now i want it to do somethin it becomes unstuck. i have also tried
Code:
workbooks.open myfile
which gives me the message about opening an already open file and that i might lose my changes.
have tried
Code:
workbooks.activate myfile
and
Code:
workbooks.select myfile
and
Code:
myfile.sheet(1).activate
the sheet in the new book does not matter, as long it is selected and becomes the active book the rest of the macro works.

please help
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
The syntax is different. For instance:

Rich (BB code):
workbooks(myfile).activate
 
Upvote 0
ok got it fixed using
Code:
windows(myfile).activate
new to VBA but my hint to all newbies is use the MACRO recorder and then modify to suit.

never thought i would praise microsoft for much but the macro recorder has been one of their better ideas :D
 
Upvote 0
never thought i would praise microsoft for much but the macro recorder has been one of their better ideas :D

The macro recorder should be used to show you the method or property you want to have, not to write or generate code... You WRITE VBA-code, you do not RECORD it.
 
Upvote 0
Why do you use Windows() instead of Workbooks()? Indeed, the macro recorder.
 
Upvote 0
no reason for windows instead of workbooks, i was getting stuck and that is what the recorder gave me when i selected manually. changed it to workbooks now though.

i am quite new to VBA so still not sure what command does what etc
 
Upvote 0
no reason for windows instead of workbooks, i was getting stuck and that is what the recorder gave me when i selected manually. changed it to workbooks now though.

i am quite new to VBA so still not sure what command does what etc

No worries, everyone here started hesitating ;) Just ask more questions if you need to / want to.
 
Upvote 0
It's generally never needed to activate any workbook/worksheet/range etc to work with it.

If you have code that will only work properly when a particular workbook is active then it's probably worth taking a look at it.

It can probably be rejigged so it doesn't rely on which workbook is active to do what it's meant to.

It shouldn't be too hard to do that and might save a lot of trouble.:)
 
Upvote 0
i could probably get the code to work without the destination workbook being open but i prefer it to be active so that the user can see that the copying process has happened. stops clicking and clicking and ending up with more sheets than they need/want
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,843
Members
452,948
Latest member
UsmanAli786

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