+ Reply to Thread
Results 1 to 5 of 5

Saving Active Workbook as Variable

Hybrid View

  1. #1
    Forum Contributor amyxkatexx's Avatar
    Join Date
    11-23-2009
    Location
    Chattanooga, TN
    MS-Off Ver
    Excel 2004 for Mac
    Posts
    128

    Saving Active Workbook as Variable

    Okay, so I have quite a few scenarios where I am needing within my macro to activate multiple workbooks.

    These workbooks are often named different names, including one of which I use regularly that is a template, so every time it opens, it has a number at the end of it.

    I had an epiphany that if there was a way that I could get excel to assign a variable name to an active workbook so that later in the code, it is able to go back to that workbook that is no longer active now because it has moved on to other workbooks since then, that would make life a whole lot easier.

    I googled a bit, but all I could find was code that activates workbooks.

    So in a nutshell, I am not needing a code that can activate a workbook. I need a code that can sort of grab whatever the name is of the current active workbook and allow me to use that later.

    Any ideas?

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Saving Active Workbook as Variable

    Like so:
    Dim wbTemp as Workbook
    
    Workbooks.Open "Template.xls"
    
    Set wbTemp = ActiveWorkbook

    With this, you can use wbTemp as the template object through the rest of the macro.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Contributor amyxkatexx's Avatar
    Join Date
    11-23-2009
    Location
    Chattanooga, TN
    MS-Off Ver
    Excel 2004 for Mac
    Posts
    128

    Re: Saving Active Workbook as Variable

    Okay, so right now I use this code to open the workbook.
    Workbooks.Open ("Lorencz:Users:amykate:Desktop:Template.xlt")

    I have some code before it and then more code after it. But at the end, I am wanting to go back to that open template to use it again.

    Where would I put that "Dim wbTemp as Workbook" part?

    Would it be like this? (your code is in bold)
    Sub FTH()
    
        Dim wbTemp as Workbook
    
        Selection.Columns("A:BQ").Copy
    
        Workbooks.Open ("Lorencz:Users:amykate:Desktop:WebconTemplate.xlt")
    
        Sheets("FTH").Select
        Range("A2").Select
        
        Selection.PasteSpecial Paste:=xlValues
        Cells.Select
        Selection.Copy
        Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
            False, Transpose:=False
    
        Set wbTemp = ActiveWorkbook
    
        Sheets("FTH").Select
        Range("A1").Select
        
       Cells.Find(What:="STOP", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
          :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, _
          MatchCase:=False).Activate
    
        Range(ActiveCell, "BR2").Copy
        Workbooks("2010_RunningSheet.xls").Worksheets("FTH").Cells _
            (Rows.Count, "C").End(xlUp).Offset(1, -2).PasteSpecial xlPasteValues
    
    End Sub

    And also, what would the activate workbook code look like using that object?

    Like this?
    Workbooks("wbTemp").Activate
    Last edited by amyxkatexx; 03-29-2010 at 03:35 PM.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Saving Active Workbook as Variable

    Maybe like so...the wbTemp is an object, so you can use it as such, no need to try and nest it inside an Sheet reference.

    Option Explicit
    
    Sub FTH()
    Dim wbTemp As Workbook
    
    Workbooks.Open ("Lorencz:Users:amykate:Desktop:Template.xlt")
    Set wbTemp = ActiveWorkbook
    
        Columns("A:BQ").Copy
        
        Workbooks.Open ("Lorencz:Users:afarrow:Desktop:WebconTemplate.xlt")
        
        Sheets("FTH").Activate
        Range("A2").PasteSpecial Paste:=xlValues
        Cells.Value = Cells.Value
        
       Cells.Find(What:="STOP", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
          :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, _
          MatchCase:=False).Activate
    
        Range(ActiveCell, "BR2").Copy
        
        Workbooks("2010_RunningSheet.xls").Worksheets("FTH").Cells _
            (Rows.Count, "C").End(xlUp).Offset(1, -2).PasteSpecial xlPasteValues
    
    wbTemp.Activate
    
    End Sub

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

    Re: Saving Active Workbook as Variable

    You generally do not need to activate workbooks,sheets or ranges

    If your workbook is actually a template (*.xlt) then when it opens it creates a new workbook (*.xls) based on the template. So each time your code opens the template a new workbook will be created so it will need renaming.

    Just a guess from your posted code
    Option Explicit
    
    Sub FTH()
        Dim wbTemp As Workbook
        Dim wbThis As Workbook
        Dim cl As Range
    
        Set wbThis = ThisWorkbook
        ActiveSheet.Columns("A:BQ").Copy
    
    
        Set wbTemp = Workbooks.Open("Lorencz:Users:afarrow:Desktop:WebconTemplate.xlt")
    
        With wbTemp
            .Sheets("FTH").Range("A2").Selection.PasteSpecial Paste:=xlValues
            .Cells.Select
            .Selection.Copy
            .Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
                                    False, Transpose:=False
    
            Set cl = .UsedRange.Find(What:="STOP", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
                                                                                        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, _
                                     MatchCase:=False)
            If Not cl Is Nothing Then
                .Range(cl, "BR2").Copy
                Workbooks("2010_RunningSheet.xls").Worksheets("FTH").Cells _
                        (Rows.Count, "C").End(xlUp).Offset(1, -2).PasteSpecial xlPasteValues
            End If
        End With
    End Sub
    Last edited by royUK; 03-10-2010 at 02:41 AM.
    Hope that helps.

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

    Free DataBaseForm example

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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