Solved

Get Excel Sheet Name from GetOpenFilename()

Posted on 2009-03-30
6
787 Views
Last Modified: 2012-05-06
What I want to do is:

Ask the user to select the file they want to pull the data out of.

What I can't do is get the name of the damn file they select so I can pull data from it.

The following is pretty simple it gets the open dialog to asks them what to open. If its open it asks them if they want to re-open it. If you select no at this point it skips to the next part. That part works great.

All I want to do now is get the name of the file that was selected so that I can copy paste the data out of it but I can't seem to get thet working. If you select "don't reopen" the file is not active so I can't use that little trick.

Thanks,

Haydan

Dim OpenFileName As Variant
Dim wBook As Workbook
Dim SampleReg As Workbook
On Error Resume Next ' if it stuffs out go to the next step
 
ActiveWorkbook = SampleReg
OpenFileName = Application.GetOpenFilename() 'Opens the File Dialog
    If OpenFileName = False Then Exit Sub 'If no file selected stop
Workbooks.Open (OpenFileName) 'works
OpenFileName.Activate
Databook = OpenFileName.Name
Range("P6") = OpenFileName
Workbooks(Databook).Activate

Open in new window

0
Comment
Question by:Haydan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 24025169
Hi Haydan,

Try something like the below code. It assigns the workbook to the 'wBook' variable to make referencing that workbook easier.

Regards,

Wayne
     Dim OpenFileName As Variant
     Dim wBook As Workbook
     Dim SampleReg As Workbook
     On Error Resume Next     ' if it stuffs out go to the next step 
     ActiveWorkbook = SampleReg
     OpenFileName = Application.GetOpenFilename()     'Opens the File Dialog
     If OpenFileName = False Then Exit Sub     'If no file selected stop
     Set wBook = Workbooks.Open(OpenFileName)
     wBook.Activate
     wBook.ActiveSheet.Range("P6") = OpenFileName

Open in new window

0
 

Author Comment

by:Haydan
ID: 24025234
Nope still doesn't work, Here's a better explained code, its the last three lines I need help with. I just need to extract that damn file name! :)
     Dim OpenFileName As Variant
     Dim wBook As Workbook
     Dim SampleReg As Workbook
     On Error Resume Next                               'If it stuffs out go to the next step
     ActiveWorkbook = SampleReg                         'Sets the current workbook as the sampleregister
     OpenFileName = Application.GetOpenFilename()       'Opens the File Dialog for the person to select the file to open
     If OpenFileName = False Then Exit Sub              'If the person clicks cancel it stops
     Set wBook = Workbooks.Open(OpenFileName)           'The workbook has been selected and this get the name of that workbook
     wBook.Activate                                     'This should activate the workbook that was just opened (or was already open)
     SampleReg.ActiveSheet.Range("P6") = OpenFileName   'The file name should be put in this cell to show that its working

Open in new window

0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 24025250
To get the name of the newly opened workbook, use this....

     Set wBook = Workbooks.Open(OpenFileName)
     'there's no need to activate the workbook
     SampleReg.ActiveSheet.Range("P6") = wBook.Name

I assume 'SampleReg' as another workbook variable?

Wayne
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Haydan
ID: 24025755
Seems to work except when you don't open the workbook (i.e. its already open and you click don't reopen) but thats OK I'm getting really frustrated with this so I'll just tell people to make sure the file is shut or it won't work. It should be so simple!

I still can't seem to get the bloody thing to work though.

It seems to be getting to the last bit and thats as far as it gets. I need to copy data from the new workbooks (wBook) worksheet "UllB4" (is the sheets Codename - Sheets names get renamed so need to use code name.)
    Dim OpenFileName As Variant
    Dim wBook As Workbook
    Dim SampleReg As Workbook
'    On Error Resume Next                               'If it stuffs out go to the next step
     
    Set SampleReg = ActiveWorkbook                         'Sets the current workbook as the sampleregister
     
    OpenFileName = Application.GetOpenFilename()       'Opens the File Dialog for the person to select the file to open
     
    If OpenFileName = False Then Exit Sub              'If the person clicks cancel it stops
     
    Set wBook = Workbooks.Open(OpenFileName)           'Set the opened workbooks name
 
    Set SampleRegData = SampleReg.Worksheets("Data") 'Set the data page
 
wBook.UllB4.Range("X10:AH145").Copy 'Copy the data
    SampleRegData.Range("A2:AH145").PasteSpecial xlPasteValues

Open in new window

0
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 125 total points
ID: 24025780
Instead of copying the data, try simply setting the values....

    SampleRegData.Range("A2:AH137").Value = wBook.UllB4.Range("X10:AH145").Value

And to get the workbook if it is already open, use this....

    On Error Resume Next
    Set wBook = Workbooks(Mid(OpenFileName, InStrRev(OpenFileName, "\") + 1))
    If wBook Is Nothing Then
        Set wBook = Workbooks.Open(OpenFileName)
    End If

Wayne
0
 

Author Closing Comment

by:Haydan
ID: 31564656
This all worked. Was getting a little sidetracked at the end so have closed this Q and opened a new one with regards to refering to codenames outside of the initial workbook.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question