Solved

vba - user enters path and work book name

Posted on 2010-11-12
6
410 Views
Last Modified: 2012-05-10
I am trying to set it where the user enters the workbook path and work book name..but I keep getting an error, saying it cant find it..amI adding/escaping the quotes correctlY?

...code...

dim var_quotes as string
var_quotes = """"

Workbooks.Open Filename:=var_quotes & workbook_path & workbook_name & var_quotes
0
Comment
Question by:GlobaLevel
[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
6 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 34122858
Why not ask the user to select the file to open with a dialog?

Sub SomeMacro()

    Dim SourcePath As Variant
    Dim SourceWb As Workbook
    Dim SourceWasOpen As Boolean

    SourcePath = Application.GetOpenFilename("Excel files (*.xls*), *.xls*", , _
        "Select file to process", , False)

    If SourcePath = False Then
        MsgBox "No file selected", vbCritical, "Aborting"
        Exit Sub
    End If

    On Error Resume Next
    Set SourceWb = Workbooks(Mid(SourcePath, InStrRev(SourcePath, "\") + 1))
    If Err = 0 Then
        SourceWasOpen = True
    Else
        SourceWasOpen = False
        Err.Clear
        Set SourceWb = Workbooks.Open(SourcePath)
    End If
    On Error GoTo 0

    ' now do whatever it is you have to do

    If Not SourceWasOpen Then SourceWb.Close False

End Sub

Open in new window

0
 
LVL 5

Expert Comment

by:Pabilio
ID: 34122861
I thin that there is Not need to be double "" when you dim as string...
var_quotes = ""
0
 
LVL 20

Expert Comment

by:Ardhendu Sarangi
ID: 34122865
Hi,
Can you try something like this below?

- Ardhendu
Sub GetFile()
Dim FLName As String

FLName = Application.GetOpenFilename
Range("a1").Value = FLName

End Sub

Open in new window

0
Industry Leaders: 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!

 
LVL 81

Expert Comment

by:byundt
ID: 34122939
Further to Pabilio's comment, also make sure that there is a path separator between the path and name
0
 
LVL 10

Author Comment

by:GlobaLevel
ID: 34123409
MatthewsPatrick,

I like that route...but how do I then set that so I can add sheets to that workbook they select and name them...
....

Dim sheet_name( 1 to 7) as Variant
Set wb = sourcewb

sheet_name(1) = "test1"
sheet_name(2) = "ttest2"
sheet_name(3) = "try6        "
sheet_name(4) = "testd  "
sheet_name(5) = "tehgyu    "
sheet_name(6) = "Ewsed     "
sheet_name(7) = "ghjk    "


For i = 1 To 7
Set ws = sourcewb.Sheets.Add
ws.Name = "sheet_name(i)"

i = i + 1
Next
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 34123562
GlobaLevel,

With respect, that goes beyond your original question.

In any event, with my example, you end up with an object variable, SourceWb, that represents the selected file.  That object variable gives you an entree to manipulating anything inside that file.  For example:

With SourceWb
    .Worksheets(1).Name = "foo"
    .Worksheets.Add After:=.Worksheets(.Worksheets.Count)
    .Worksheets(.Worksheets.Count).Name = "hee"
End With

Open in new window


Patrick
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
Determine dependencies 3 21
Formula returning #N/A 9 35
Pull Phone Number out of Cell 3 12
Expand information down with a lot of information 35 38
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

730 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