+ Reply to Thread
Results 1 to 4 of 4

Store current workbook name in variable and use it

  1. #1
    Registered User
    Join Date
    12-02-2014
    Location
    Netherlands
    MS-Off Ver
    Excel 2013
    Posts
    6

    Store current workbook name in variable and use it

    Hi there to everyone, this here is my first post after my introduction post.

    I will explain what I am trying to do, basically I need a macro (name of the macro: ImportData) in my current opened workbook ("MyCurrentWorkboook.xlsm") that does following in chronological order;

    1. Open a database file (workbook "DATABASE.xlsm")
    2. Copy a range from a sheet in that workbook
    3. Go back to my current opened workbook ("MyCurrentWorkboook.xlsm")
    4. Paste the range in a range I need
    5. Close the databasefile

    Nothing too difficult one might think.

    Please Login or Register  to view this content.
    I got the code working but only when I hardcode the filenames. The databasefile is not a problem for it is a static document, always. But I don't know the name of my current opened workbook, the file itself gets copied over and over again so the name changes depending on the project. So how do I cange the code in order it uses the current workbookname?

    This is where it becomes a drama:

    Please Login or Register  to view this content.
    Any help on this one is much appreciated thanks in advance!

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,256

    Re: Store current workbook name in variable and use it

    ThisWorkbook will always refer to the workbook with the code in it, so you can use:

    Please Login or Register  to view this content.
    Remember what the dormouse said
    Feed your head

  3. #3
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Store current workbook name in variable and use it

    It could also be easier to set a variable to point to the datasheet in your current workbook:

    Please Login or Register  to view this content.
    WBD
    Office 365 on Windows 11, looking for rep!

  4. #4
    Registered User
    Join Date
    12-02-2014
    Location
    Netherlands
    MS-Off Ver
    Excel 2013
    Posts
    6

    Re: Store current workbook name in variable and use it

    Quote Originally Posted by rorya View Post
    ThisWorkbook will always refer to the workbook with the code in it, so you can use:

    Please Login or Register  to view this content.
    Thank you very much Rorya, this works like a charm!
    The with loop is a method I would have not come up with.
    I am sure this will be useful in future coding as well.

    WideBoyDixon: thank you as well.
    I am sure that your code works great too but I would have had a second question, how to shorten this part:

    Please Login or Register  to view this content.
    The with loop just does that where your solution still "doubles" the paste action.
    My preference goes to the other solution.

    Again, thanks both of you for the quik replies!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Function for various variable types, how to store the variable?
    By Scotster in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-04-2018, 12:18 PM
  2. how to store all current filters and sorts
    By lelrich in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-15-2014, 03:36 PM
  3. Store current COUNTIFS value in each row in MS-Excel?
    By silvercover in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 05-06-2014, 07:36 AM
  4. Copy variable range from variable workbook to variable range on current workbook
    By Locster79 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-09-2013, 10:37 AM
  5. Replies: 8
    Last Post: 11-11-2012, 07:22 PM
  6. [SOLVED] Store variable in worksheet and use that variable in VBA code
    By ExcelGal in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-17-2012, 08:20 AM
  7. How to store current row into variable
    By jafery in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-12-2005, 12:03 AM

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