Solved

vba - user enters path and work book name

Posted on 2010-11-12
6
404 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
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:pari123
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 80

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

706 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now