+ Reply to Thread
Results 1 to 11 of 11

windows("variable filename").activate

  1. #1
    Registered User
    Join Date
    05-26-2009
    Location
    Mobile, AL
    MS-Off Ver
    Excel 2007
    Posts
    5

    windows("variable filename").activate

    Good morning guys, hope you can help me tackle a problem I'm having wth my macro.....

    I have created a macro to split out information from one excel workbook into 2 additional workbooks, and format them the way we need. Everything works fine on the filename I originally recorded the macro from, but is giving me a 'subscript out of range' error when trying to run the macro on a file with a different name.

    currently, my test file has the 2 following fields that I somehow need to have as variables:

    Windows("insmacrotest20090418.xls").Activate

    and

    Windows("Book1").Activate

    I need the filenames and workbooks in the parenthesis to be variable so that the macro can be run on other reports with varying filenames.

    Please help me out with this, as you can tell by the file name, I've been fighting with this macro off & on for over a month now.

    thanks!!!!
    Last edited by stevo1329; 05-26-2009 at 01:55 PM.

  2. #2
    Forum Contributor
    Join Date
    05-05-2009
    Location
    Wisconsin
    MS-Off Ver
    Excel 2010 and 365
    Posts
    113

    Re: windows("variable filename").activate

    As long as you have the variable declared as a string and it contains the proper name, it should work.


    Please Login or Register  to view this content.


    should work

  3. #3
    Registered User
    Join Date
    05-26-2009
    Location
    Mobile, AL
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: windows("variable filename").activate

    sorry, I'm still new with VBA.... I've tried the way you had listed & then a couple different configurations and continued to receive the debug message.

    here is my script, please tell me where I'm going wrong:

    Please Login or Register  to view this content.

    the last line is where my debugger goes everytime....
    Last edited by stevo1329; 05-27-2009 at 01:00 PM.

  4. #4
    Forum Expert
    Join Date
    03-23-2004
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    7,064

    Re: windows("variable filename").activate

    I dont see that insmacrotest20090418 has been set in that code (unless it's in a subroutine you're calling)
    Regards
    Special-K

    Ensure you describe your problem clearly, I have little time available to solve these problems and do not appreciate numerous changes to them.

  5. #5
    Forum Contributor
    Join Date
    05-05-2009
    Location
    Wisconsin
    MS-Off Ver
    Excel 2010 and 365
    Posts
    113

    Re: windows("variable filename").activate

    The dim statement declares a variable. You want to use a name for the variable. Something like MyBookName. This will only declare the variable. After this statement, it will contain an empty string (""). You will then need to set the variable to the name of the worksheet you want to activate.

    Please Login or Register  to view this content.

    Full code might look something like this


    Please Login or Register  to view this content.

    There may be an easier / better way for you to go about getting the workbook name.

    If the workbook you want to activate is the one you just created, then you could create a variable for the workbook itself.


    Please Login or Register  to view this content.

    I imagine that's not the case though because the newly added workbook should already be active.

    You will probably need additional code to build the name of the workbook because it changes all the time?

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: windows("variable filename").activate

    stevo,

    Please edit your post to add code tags.
    Entia non sunt multiplicanda sine necessitate

  7. #7
    Registered User
    Join Date
    05-26-2009
    Location
    Mobile, AL
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: windows("variable filename").activate

    still having the same issue.... the filename insmacrotest20090418 was the file I originally recorded this macro with. however, when I try to run the macro on a file with a different name, it's still giving me the debug message even with the variable input as you listed above.

    any other ideas?

  8. #8
    Registered User
    Join Date
    11-07-2007
    Location
    Estonia
    MS-Off Ver
    OFF 2003 (11.8237.8221) SP3
    Posts
    16

    Re: windows("variable filename").activate

    stevo1329
    My English very bad, so ..small talk.
    Dry something like this:

    Sub ava()
    Dim MyBookName As String

    ' declares the variable, right now it contains an empty string
    Dim Workbook As Workbook

    MyBookName = "insmacrotest20090418" ' this sets the value of the variable to the sheet name

    ActiveWindow.Caption = "1"

    Columns("C:C").Select
    Selection.Cut

    'you code
    Workbooks.Add 'This wbk first named Book1, Later("2")
    ActiveSheet.Paste
    Cells.Select
    Cells.EntireColumn.AutoFit

    ActiveWindow.Caption = "2"
    'Do something

    Windows("1").Activate
    'Do something
    Windows("2").Activate
    'Do something
    Windows("1").Activate
    'Do something
    Windows("2").Activate
    'Do something


    ' Rename as you need , Save, Close.
    End Sub
    Last edited by omnibuster; 05-27-2009 at 03:33 PM.

  9. #9
    Registered User
    Join Date
    05-26-2009
    Location
    Mobile, AL
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: windows("variable filename").activate

    thanks I'll try this out & let you know if it works

  10. #10
    Forum Contributor
    Join Date
    05-05-2009
    Location
    Wisconsin
    MS-Off Ver
    Excel 2010 and 365
    Posts
    113

    Re: windows("variable filename").activate

    Did you replace the
    insmacrotest20090418
    in

    Please Login or Register  to view this content.
    with the window you want to activate?



    Are you sure it shouldn't be "insmacrotest20090418.xls"?

  11. #11
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: windows("variable filename").activate

    More common practice would be to activate the Workbook rather than the Window:
    Please Login or Register  to view this content.
    or as Cyclops suggested,
    Please Login or Register  to view this content.

+ 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