Consulting

Results 1 to 4 of 4

Thread: setting a workbook as a constant

  1. #1

    setting a workbook as a constant

    hello,

    I have a workbook that is going to dig up data from other workbooks.

    is it possible to set these workbooks (data source) as constants so I don't have to change every single line where their name appears in case we need to change their name??

    I tried somehting like this:

    Public Const input = "myworkbook1.xls"

    and then tried to call:

    workbooks("input").activate

    it didnt work.

    my idea is to set all the workbooks I need to use as constant and to call them like this:

    input.activate
    input1.activate etcetc

    any idea please??

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]
    Sub Test()
    Dim WB1 As Workbook, WB2 As Workbook 'etc
    Set WB1 = Workbooks.Open("C:\AAA\Book2.xls")
    Set WB21 = Workbooks.Open("C:\AAA\Book3.xls")
    WB1.Activate
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    That should work, thae activate line that is but better like this

    [vba]

    Public wb1 As Workbook
    Public wb2 As Workbook

    Sub MySub()

    Set wb1 = Workbooks("myworkbook1.xls")
    Set wb2 = Workbooks("myworkbook2.xls")

    '...

    wb1.Activate

    '...

    wb2.Activate

    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Choubix,
    When you use a variable, do not enclose it in quotes, or it is read as a string

    workbooks("input").activate

    should be

    workbooks(input).activate
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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