?
Solved

vba - user enters path and work book name

Posted on 2010-11-12
6
Medium Priority
?
412 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 93

Accepted Solution

by:
Patrick Matthews earned 2000 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
Technology Partners: 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 93

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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

777 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