2014-01-24 126 views
0

我想创建一个在文件中打开excel文件夹的宏。唯一的问题是我遇到现在的是,我不能通常由宏象这样:使用更改名称打开文件

Sub CopyDataFromWorksheet() 

    Workbooks.Open ("dir\files\dashboard 24-01-2014.xls") 

End Sub 

因为我想打开该文件包含一个变量的组成部分。它有一个固定的naam,仪表板,但也是一个日期,20-01 - 2014,经常变化。所以我在寻找代码:

  • 打开一个文件夹
  • 查找包含“仪表板”
  • 打开他们的XLS文件。

有人想过我该如何编码?

尊敬的问候,

马克

+1

我觉得这http://stackoverflow.com/questions/10380312/loop-through-files-in-a-folder-using-vba 会为你的情况:) – parakmiakos

+1

做查找迪尔函数在VBA帮助中,它将让您查找包含通配符的文件名。例如:“Dashboard * .xls”将选取以“Dashboard”开头的任何xls文件。 –

回答

1

认为你只需要一个很小的改变了代码:

Sub openAllFiles() 

yourPath = "X:\SSC_HR\SENS\Bedrijfsbureau\Rapportages\sterren\MAANDELIJKSE RAPPORTAGES\UITDRAAI DB_MAANDELIJKS_DASHBOARD\" 
file = Dir(yourPath & "Dashboard*.xls") 
Do While file <> vbNullString 
Workbooks.Open (yourPath & file) 
file = Dir() 
Loop 
End Sub 

workbooks.Open需要的完整路径,而不只是文件名

1

试试这个:

Sub loopdir() 
Dim MyFile$, Fold$ 
'Dim FD As FileDialog 
Dim WBCur As Workbook, WBFile As Workbook 
Set WBCur = ActiveWorkbook 

'''pick a folder with dialog 
'Set FD = Application.FileDialog(msoFileDialogFolderPicker) 
'With FD 
'.Title = "Select a Folder" 
'.AllowMultiSelect = False 
'If .Show <> -1 Then Exit Sub 
'Fold = .SelectedItems(1) & "\" 
'End With 
'Set FD = Nothing 
'''or just 
Fold = "<your folder here with \ in the end>" 

MyFile = Dir(Fold & "dashboard*.xls*") 'last * for both xls and xlsx 
Do While MyFile <> "" 
    Workbooks.Open Filename:=Fold & MyFile 
    Set WBFile = ActiveWorkbook 
    '''your code here 
    'Application.DisplayAlerts = False 
    'WBFile.Close 
    'Application.DisplayAlerts = True 
    MyFile = Dir() 
Loop 
'Application.DisplayAlerts = True 'for sure 
Set WBCur = Nothing 
Set WBFile = Nothing 
End Sub 
1

这应该工作确定为您服务。

Sub openAllFiles() 
yourPath="dir\files\" 
file=Dir(yourPath & "Dashboard*.xls") 
Do while file<>vbNullString 
Workbooks.Open(yourpath & file) 
file=Dir() 
Loop 
End Sub 
+0

感谢您的回复bmgh,但如果用我选定的文件夹运行它,我会得到错误的名称或编号错误。任何想法可能会导致什么?这是现在的代码:Sub openAllFiles() yourPath =“X:\ SSC_HR \ SENS \ Bedrijfsbureau \ Rapportages \ sterren \ MAANDELIJKSE RAPPORTAGES \ UITDRAAI DB_MAANDELIJKS_DASHBOARD \” file = Dir(yourPath&“Dashboard * .xls”) 做,当文件<> vbNullString Workbooks.Open(文件) 文件= DIR() 循环 结束小组 – user181796

+0

我唯一一次获得通过测试,如果'yourPath'目录要么是错误的,或者你已经离开了“\”结束。我会检查你的文件夹路径,然后再试一次。 – bmgh1985

0

很好的解决方案亚历克斯。我把你的答案更进一步,稍微偏向一边:)而不是打开所有类似命名的文件。我需要打开最新的,类似命名的文件。所以,我没有这个...

Dim newest As Date 
Dim current As Date 
Dim right_file As String 
Dim rot_cnt As Integer 
rot_cnt = 1 

Dim my_path As String 
Dim file_name As String 
my_path = "C:\Path\To\File\Dir\" 
file_name = Dir(my_path & "My-Similar-Files*.xlsm") 

Do While file_name <> vbNullString 
    If rot_cnt = 1 Then 
     newest = FileDateTime(my_path & file_name) 
    End If 
    If rot_cnt >= 1 Then 
     current = FileDateTime(my_path & file_name) 
    End If 
    If DateSerial(Year(current), Month(current), Day(current)) >= _ 
    DateSerial(Year(newest), Month(newest), Day(newest)) Then 
     newest = FileDateTime(my_path & file_name) 
     right_file = my_path & file_name 
    End If 
    file_name = Dir() 
    rot_cnt = rot_cnt + 1 
Loop 

Workbooks.Open (right_file), UpdateLinks:=False, ReadOnly:=True 

后进一步测试这个使用最后一次保存在“真实”的创作时间,所以它可能会返回不想要的结果。 BuiltinDocumentProperties(“创建日期”)也是创建日期的虚假导向。如果有人复制工作簿,则会克隆此值。为了达到正确的结果而不必手动启用任何新的引用,我使用了这个。

Dim oFS As Object 
Dim StrFile As String 
Dim rot_cnt As Integer 
rot_cnt = 1 

Dim current As Date 
Dim newest As Date 
Dim right_file As String 

Dim my_path As String 
Dim file_name As String 
my_path = "C:\Path\To\File\Dir\" 
file_name = "My-Similar-Files*.xlsm" 

StrFile = Dir(my_path & file_name) 
Do While Len(StrFile) > 0 
    Set oFS = CreateObject("Scripting.FileSystemObject") 
    If rot_cnt = 1 Then 
     newest = oFS.GetFile(my_path & StrFile).DateCreated 
    End If 
    If rot_cnt >= 1 Then 
     current = oFS.GetFile(my_path & StrFile).DateCreated 
    End If 

'The Right(StrFile, 6) If parameter is because Dir() also gives the exact 
'string of file_name as one of the values which we don't want to process. 
    If DateSerial(Year(current), Month(current), Day(current)) >= _ 
    DateSerial(Year(newest), Month(newest), Day(newest)) _ 
    And Right(StrFile, 6) <> "*.xlsm" Then 
     newest = oFS.GetFile(my_path & StrFile).DateCreated 
     right_file = my_path & StrFile 
    End If 

    StrFile = Dir 
    Set oFS = Nothing 
    rot_cnt = rot_cnt + 1 
Loop 

Workbooks.Open (right_file), UpdateLinks:=False, ReadOnly:=True