Open Workbook

#BoB#

Active Member
Joined
May 20, 2008
Messages
303
Hi,

Can anyone tell me the code to open a workbook from a specific path!!!

eg: I want to open the workbook "Invoice.xls" from the path: D:\2008\December\Workflow

After opening this workbook, I would like to go to the sheet "Raw Data" and then copy the entire Raw 10. And close the workbook.

Please help me with a code to perform this action.

Thanks in advance.


Regards,
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hello again Bob

Try:

Workbooks.Open("D:\2008\December\Workflow\Invoice.xls")

You can also call the open file dialog with:

Application.Dialogs(xlDialogOpen).Show

And the you can navigate to the desired workbook.

What data range do you want to copy, and where do you want to paste it? And what do you want to paste? That is, values only, formats, formulas or everything?
 
Upvote 0
Hey,

Thanks for the quick reply.

Now, I have files like Invoice_SDunn, Invoice_AWhite, Invoice_SStuart, and so on...(Invoice_USERNAME) around 100 files.

I need to open each of the files and go to the Sheet "Raw Data" and copy the entire "Row 10" and paste it in the file where the macro is (paste as value, one row after another). And then close the Invoice file and move on to the next invoice file.

Hope I am making sense :rolleyes:

Thanks a ton for your help again :)


Regards,
 
Upvote 0
Hi Bob

Try this, but please test this on a copy of your original wrkbook first:

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> ForBob()<br><br><SPAN style="color:#00007F">With</SPAN> Application<br>    .ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>    .EnableEvents = <SPAN style="color:#00007F">False</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br><br><SPAN style="color:#00007F">Dim</SPAN> fso <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>, fld <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>, fil <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>, fldPath <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, wbSrc <SPAN style="color:#00007F">As</SPAN> Workbook, wbCur <SPAN style="color:#00007F">As</SPAN> Workbook<br><br>fldPath = "D:\2008\December\Workflow\"<br><br><SPAN style="color:#00007F">Set</SPAN> wbCur = ActiveWorkbook<br><br><SPAN style="color:#00007F">Set</SPAN> fso = CreateObject("Scripting.FileSystemObject")<br><SPAN style="color:#00007F">Set</SPAN> fld = fso.getfolder(fldPath)<br><br><SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> ErrHandler<br><br><SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> fil <SPAN style="color:#00007F">In</SPAN> fld.Files<br>    <SPAN style="color:#00007F">If</SPAN> InStr(LCase(fil.Name), "invoice") > 0 <SPAN style="color:#00007F">Then</SPAN><br>        <SPAN style="color:#00007F">Set</SPAN> wbSrc = Application.Workbooks.Open(fil.Path)<br>            wbSrc.Sheets("Raw Data").Range("10:10").Copy wbCur.Sheets("Raw Data").Range("A" & Rows.Count).End(xlUp).Offset(1)<br>            wbSrc.Close <SPAN style="color:#00007F">False</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">Next</SPAN> fil<br><br><br>ErrHandler:<br>Application.EnableEvents = <SPAN style="color:#00007F">True</SPAN><br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

NOTE: This pastes the data into a sheet called "Raw Data" in your current workbook. So assumes that both the "invoice_username" files have a sheet caled "Raw Data", but that your consolidation workbook has the same sheet too.
 
Upvote 0
Thnks Jon.

I am sure that now I'm very close to solving this problem.

Let me incorporate your codes in the existing program, and see if I can do the rest myself.

Thanks again. Much appreciated :)


~ChillaX!!!
 
Upvote 0
Hey Jon,

Finally I am able to put my codes together to work with your fantastic codes :biggrin:.

Thanks a TON.


~ChillaX!!!
 
Upvote 0
Hi Bob

Try this, but please test this on a copy of your original wrkbook first:

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> ForBob()<br><br><SPAN style="color:#00007F">With</SPAN> Application<br>****.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>****.EnableEvents = <SPAN style="color:#00007F">False</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br><br><SPAN style="color:#00007F">Dim</SPAN> fso <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>, fld <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>, fil <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN>, fldPath <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, wbSrc <SPAN style="color:#00007F">As</SPAN> Workbook, wbCur <SPAN style="color:#00007F">As</SPAN> Workbook<br><br>fldPath = "D:\2008\December\Workflow\"<br><br><SPAN style="color:#00007F">Set</SPAN> wbCur = ActiveWorkbook<br><br><SPAN style="color:#00007F">Set</SPAN> fso = CreateObject("Scripting.FileSystemObject")<br><SPAN style="color:#00007F">Set</SPAN> fld = fso.getfolder(fldPath)<br><br><SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> ErrHandler<br><br><SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> fil <SPAN style="color:#00007F">In</SPAN> fld.Files<br>****<SPAN style="color:#00007F">If</SPAN> InStr(LCase(fil.Name), "invoice") > 0 <SPAN style="color:#00007F">Then</SPAN><br>********<SPAN style="color:#00007F">Set</SPAN> wbSrc = Application.Workbooks.Open(fil.Path)<br>************wbSrc.Sheets("Raw Data").Range("10:10").Copy wbCur.Sheets("Raw Data").Range("A" & Rows.Count).End(xlUp).Offset(1)<br>************wbSrc.Close <SPAN style="color:#00007F">False</SPAN><br>****<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">Next</SPAN> fil<br><br><br>ErrHandler:<br>Application.EnableEvents = <SPAN style="color:#00007F">True</SPAN><br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

NOTE: This pastes the data into a sheet called "Raw Data" in your current workbook. So assumes that both the "invoice_username" files have a sheet caled "Raw Data", but that your consolidation workbook has the same sheet too.

Hi Jon

How can I reuse your code above if I were to:

1. Search the folder "C:\Documents\" for all PDF files
2. List all of them in Sheet2 of File1.xls, starting from cell A1
3. I only want the Filenames to be listed, without the entire path and file extension.

Thanks
 
Upvote 0
Hello PATSYS

Will you be running it from File1.xls or do you want it to open File1.xls and write the names to Sheet2?

You can place this into a module in File1.xls and run it to return a name of all PDF files in a folder:

Code:
Sub ForPATSYS()

With Application
    .ScreenUpdating = False
    .EnableEvents = False
End With

Dim fso As Object, fld As Object, fil As Object, fldPath As String, i As Long

fldPath = "C:\Documents\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.getfolder(fldPath)
i = 1

On Error GoTo ErrHandler

For Each fil In fld.Files
    If InStr(LCase(fil.Name), ".pdf") > 0 Then
        Sheets("Sheet2").Range("A" & i) = fil.Name
        i = i + 1
    End If
Next fil


ErrHandler:
Application.EnableEvents = True

End Sub
 
Upvote 0
Dear Jon,

I'd like to have a question similar to that of Bob.
My problem arises because FileSearch was removed from Excel VBA 2007. I had a perfect macro, but it has to be restructured.
My question is pointing to how I can look for and open a file.

The aim is to open a file from " V:\ " every day. The files are named NAVmmddyy e.g. NAV101309 (today). I set mmddyy as the variable "date". After opening, only simple copy/paste is needed.

I modified the code you wrote here for Bob, but no success. It stops already at Set fld = fso.getfolder(fldPath).

Could you give a peek at what the problem can be?

Here is the code:

Code:
Dim fso As Object, fld As Object, fil As Object, fldPath As String, wbSrc As Workbook

fldPath = " V:\ "
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.getfolder(fldPath) ' that's where the program stops :(

On Error GoTo ErrHandler

For Each fil In fld.Files
    If fil.Name = "NAV  " & Date & " .xls" Then  ' I want to open a file of today, e.g. NAV101309.xls
        Set wbSrc = Application.Workbooks.Open(fil.Path)
    End If
Next fil
       
        For i = 1 To 600
            nav(i) = Cells(i + 5, 5).Value
        Next i

        Workbooks("TARGET.xls").Sheets("NAV").Activate

        For j = 3 To 94
            Cells(Row, j).Value = nav(i)
        Next j

Workbooks("NAV " & datum & ".xls").Close
ErrHandler:
Application.EnableEvents = True
End Sub

I'm a beginner, so stupid errors may be in the code. I'm looking forward to your answer with a BIG thank you in advance.

Best regards,
Pius from Hungary
 
Upvote 0
Hi
I have some problem to be solved can you help me in writing a macro

Its related to timesheet consolidation
 
Upvote 0

Forum statistics

Threads
1,215,573
Messages
6,125,608
Members
449,238
Latest member
wcbyers

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top