VBA open file and copy tabs

george hart

Board Regular
Joined
Dec 4, 2008
Messages
241
I need to open certain files according to the date within the file name and copy certain tabs: e.g: DyChrt2060711.xls

If I omit dte and the inputbox and just want to get DyChrt2.xls it works fine, but I need the option for one to enter the date to get the right file.

The code below appear to do nothing????

Dim wbOpen As Workbook
Dim wbNew As Workbook
'Change Path
Const strPath As String = "C:\Documents and Settings\HartG\My Documents\Dave Slater\"
Dim strExtension As String
Dte = InputBox("Enter the date")
ChDrive strPath
ChDir strPath
'Change extension
strExtension = Dir("DyChrt2 & dte & .xls")
Dim Tabs As Variant
Tabs = Array("LAIRA STOP", "LANDORE", "SPM", "OOC STOP")
Do While strExtension <> ""
Set wbOpen = Workbooks.Open(strPath & strExtension, UpdateLinks:=0)
With wbOpen
.Sheets(Tabs).Copy After:=ThisWorkbook.Sheets(1)
.Close SaveChanges:=False
End With

strExtension = Dir
Loop

End Sub

Any help would be most appreciated
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Not sure if this is the only problem but there is a typo on this line

strExtension = Dir("DyChrt2 & dte & .xls")

it should be
strExtension = Dir("DyChrt2" & dte & ".xls")

Although, I don't know why it would work if you omit the date unless you have a file named DyChrt2 & dte & .xls
 
Upvote 0
In addition to Rob's comment, the Do Loop isn't necessary if you are only processing one file at a time.

The code in red font below is typically used to process all files meeting a filespec in a folder.

Rich (BB code):
Do While strExtension <> ""
Set wbOpen = Workbooks.Open(strPath & strExtension, UpdateLinks:=0)
With wbOpen
     .Sheets(Tabs).Copy After:=ThisWorkbook.Sheets(1)
     .Close SaveChanges:=False
End With 
strExtension = Dir
Loop
 
Upvote 0
Jerry is correct about the do Loop but you will need an if statement to avoid a runtime error if the file path and name to open doesn't exist. The do loop provides a kind of if statement but it would probably be better to have feedback if the file wasn't found.

Code:
If strExtension <> "" Then
     Set wbOpen = Workbooks.Open(strPath & strExtension, UpdateLinks:=0)
     With wbOpen
          .Sheets(Tabs).Copy After:=ThisWorkbook.Sheets(1)
          .Close SaveChanges:=False
     End With 
Else
     MsgBox strPath & StrExtension & vbCr & "Doesn't Exist"
End If
 
Upvote 0
Resolved...thanks very much for your help. Most apeciated.

For those that are interested, the code below works a treat...:


Sub GetTabs()
Dim wbOpen As Workbook
Dim wbNew As Workbook
'Change Path
Const strPath As String = "C:\Documents and Settings\HartG\My Documents\Dave Slater\"
Dim strExtension As String
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
'dte = InputBox("Enter the date")
dte = Format$(Range("C1").Value, "ddmmyy")

'Remove existing tabs
Call DelTabs
ChDrive strPath
ChDir strPath
'Change extension
strExtension = Dir("DyChrt2" & dte & ".xls")
Dim Tabs As Variant
Tabs = Array("LAIRA STOP", "LANDORE", "SPM", "OOC STOP")
Do While strExtension <> ""
Set wbOpen = Workbooks.Open(strPath & strExtension, UpdateLinks:=0)
With wbOpen
.Sheets(Tabs).Copy After:=ThisWorkbook.Sheets(1)
.Close SaveChanges:=False
End With

strExtension = Dir
Loop

Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
Sheets("Data").Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,559
Messages
6,179,513
Members
452,921
Latest member
BBQKING

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