+ Reply to Thread
Results 1 to 7 of 7

Macro to prevent switching ActiveBook

Hybrid View

  1. #1
    Registered User
    Join Date
    04-02-2010
    Location
    Toronto
    MS-Off Ver
    Excel 2007
    Posts
    62

    Macro to prevent switching ActiveBook

    Hi All,

    I have the following code in a UserForm to copy a range of cells and paste it into a new workbook.

        
    Sheets("YoYMats").Select
        Range("a1:s25").Select
        Selection.Copy
        Workbooks.Add
        ActiveSheet.Paste
    Unfrotunately, when I do this it makes the new workbook the Active Workbook and the above code fails when it is run a second time. I should also note, I do not want the user to see the workbook behind the UserForm. My current solution to the above problem was to add this code after the paste:

    Windows("GHG Calculator NM v3.xlsm").Activate
    This works well until the user saves the file as another name, then the code fails and i need to manually go in and update it. Is there a way for me to set the workbook with the macros as always the ActiveWorkbook?

    Thanks in advance for your help.
    Last edited by savio21; 12-02-2011 at 02:44 PM.

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Macro to prevent switching ActiveBook

    Hi

    You can use ThisWorkbook, rather than ActiveWorkbook

    This is also a little bit neater, faster and avoids Activating altogether
    Sub copyData()
    
    Application.ScreenUpdating = False
       
    Workbooks.Add.Sheets("Sheet1").Range("A1:S25").Value = _
             ThisWorkbook.Sheets("Sheet1").Range("A1:S25").Value
    
    Application.ScreenUpdating = True
    End Sub
    Or maybe:
    Sub copyData()
    
    Application.ScreenUpdating = False
       
             ThisWorkbook.Sheets("Sheet1").Range("A1:S25").Copy _
                    Workbooks.Add.Sheets("Sheet1").Range("A1")
                    
    
    Application.ScreenUpdating = True
    End Sub
    If you want the formatting
    Last edited by Kyle123; 12-02-2011 at 10:50 AM.

  3. #3
    Registered User
    Join Date
    04-02-2010
    Location
    Toronto
    MS-Off Ver
    Excel 2007
    Posts
    62

    Re: Macro to prevent switching ActiveBook

    I appear to be getting a "Subscript out of range" error and highlights this section of code for the first:

    Workbooks.Add.Sheets("Sheet1").Range("A1:S25").Value = _
             ThisWorkbook.Sheets("Sheet1").Range("A1:S25").Value
    and this part for the second:

    [code ]ThisWorkbook.Sheets("Sheet1").Range("A1:S25").Copy _
    Workbooks.Add.Sheets("Sheet1").Range("A1")
    [/code]

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Macro to prevent switching ActiveBook

    Do you have a sheet named "sheet1"?

  5. #5
    Registered User
    Join Date
    04-02-2010
    Location
    Toronto
    MS-Off Ver
    Excel 2007
    Posts
    62

    Re: Macro to prevent switching ActiveBook

    Well its called Sheet1(Year over Year) in the VB Project window. I don't actually have a tab called Sheet 1 though. Does that make a difference?

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Macro to prevent switching ActiveBook

    You need in this case to refer to the sheet by it's name/
    Option Explicit
    Sub copyData()
    
    Application.ScreenUpdating = False
       
    Workbooks.Add
    ThisWorkbook.Sheets("Year to Year").Range("A1:S25").Copy _
        ActiveWorkbook.Sheets("Sheet1").Range("A1:S25")
    
    Application.ScreenUpdating = True
    End Sub
    Or
    Option Explicit
    Sub copyData()
    
    Application.ScreenUpdating = False
       
    Workbooks.Add
    ThisWorkbook.Sheet1.Range("A1:S25").Copy _
        ActiveWorkbook.Sheet1.Range("A1:S25")
    
    Application.ScreenUpdating = True
    End Sub
    Read this for a fuller explanation.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  7. #7
    Registered User
    Join Date
    04-02-2010
    Location
    Toronto
    MS-Off Ver
    Excel 2007
    Posts
    62

    Re: Macro to prevent switching ActiveBook

    Awesome, thanks Roy and Kyle that worked! Now I don't have to worry about changing the file names everytime

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1