Vba 打开具有更改名称的文件

Vba 打开具有更改名称的文件,vba,excel,Vba,Excel,我想创建一个宏来打开文件中的excel文件夹。我现在遇到的唯一问题是,我无法通过以下宏正常执行此操作: Sub CopyDataFromWorksheet() Workbooks.Open ("dir\files\dashboard 24-01-2014.xls") End Sub 因为我要打开的文件包含一个变量组件。它有一个固定的naam,仪表盘,但也有一个日期,20-01-2014,经常变化。因此,我正在寻找以下代码: 打开文件夹 查找包含“Dashboard”的xls文件 打

我想创建一个宏来打开文件中的excel文件夹。我现在遇到的唯一问题是,我无法通过以下宏正常执行此操作:

Sub CopyDataFromWorksheet()

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

End Sub
因为我要打开的文件包含一个变量组件。它有一个固定的naam,仪表盘,但也有一个日期,20-01-2014,经常变化。因此,我正在寻找以下代码:

  • 打开文件夹
  • 查找包含“Dashboard”的xls文件
  • 打开它们
有人想过我应该如何编写代码吗

尊敬的各位

Marc

试试这个:

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
Sub-loopdir()
Dim MyFile$,折叠$
'将FD设置为文件对话框
将WBCur设置为工作簿,将WBFile设置为工作簿
设置WBCur=ActiveWorkbook
''选择一个带有对话框的文件夹
'Set FD=Application.FileDialog(msoFileDialogFolderPicker)
“与FD
'.Title=“选择一个文件夹”
'.AllowMultiSelect=False
'如果.Show-1,则退出Sub
'Fold=.SelectedItems(1)和“\”
"以
'设置FD=无
''或者只是
Fold=“”
对于xls和xlsx,MyFile=Dir(折叠和“dashboard*.xls*”)last*
当我的文件“”时执行此操作
工作簿。打开文件名:=Fold&MyFile
设置WBFile=active工作簿
''你的密码在这里
'Application.DisplayAlerts=False
'WBFile.Close
'Application.DisplayAlerts=True
MyFile=Dir()
环
“Application.DisplayAlerts=True”是肯定的
设置WBCur=Nothing
设置WBFile=Nothing
端接头
试试这个:

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
Sub-loopdir()
Dim MyFile$,折叠$
'将FD设置为文件对话框
将WBCur设置为工作簿,将WBFile设置为工作簿
设置WBCur=ActiveWorkbook
''选择一个带有对话框的文件夹
'Set FD=Application.FileDialog(msoFileDialogFolderPicker)
“与FD
'.Title=“选择一个文件夹”
'.AllowMultiSelect=False
'如果.Show-1,则退出Sub
'Fold=.SelectedItems(1)和“\”
"以
'设置FD=无
''或者只是
Fold=“”
对于xls和xlsx,MyFile=Dir(折叠和“dashboard*.xls*”)last*
当我的文件“”时执行此操作
工作簿。打开文件名:=Fold&MyFile
设置WBFile=active工作簿
''你的密码在这里
'Application.DisplayAlerts=False
'WBFile.Close
'Application.DisplayAlerts=True
MyFile=Dir()
环
“Application.DisplayAlerts=True”是肯定的
设置WBCur=Nothing
设置WBFile=Nothing
端接头

这对您来说应该没问题

Sub openAllFiles()
yourPath="dir\files\"
file=Dir(yourPath & "Dashboard*.xls")
Do while file<>vbNullString
Workbooks.Open(yourpath & file)
file=Dir()
Loop
End Sub
子openAllFiles()
yourPath=“dir\files\”
file=Dir(您的路径和“Dashboard*.xls”)
执行文件VBNullString时
工作簿。打开(您的路径和文件)
file=Dir()
环
端接头

这对您来说应该没问题

Sub openAllFiles()
yourPath="dir\files\"
file=Dir(yourPath & "Dashboard*.xls")
Do while file<>vbNullString
Workbooks.Open(yourpath & file)
file=Dir()
Loop
End Sub
子openAllFiles()
yourPath=“dir\files\”
file=Dir(您的路径和“Dashboard*.xls”)
执行文件VBNullString时
工作簿。打开(您的路径和文件)
file=Dir()
环
端接头

您认为您只需要对代码进行一个非常小的更改:

Sub openAllFiles()

yourPath = "<your_file_path_ends_with\>"
file = Dir(yourPath & "Dashboard*.xls")
Do While file <> vbNullString
Workbooks.Open (yourPath & file)
file = Dir()
Loop
End Sub
子openAllFiles()
yourPath=“”
file=Dir(您的路径和“Dashboard*.xls”)
当文件为vbNullString时执行
工作簿。打开(您的路径和文件)
file=Dir()
环
端接头

Workbook.Open需要完整路径,而不仅仅是文件名

我想您只需要对代码做一点小小的更改:

Sub openAllFiles()

yourPath = "<your_file_path_ends_with\>"
file = Dir(yourPath & "Dashboard*.xls")
Do While file <> vbNullString
Workbooks.Open (yourPath & file)
file = Dir()
Loop
End Sub
子openAllFiles()
yourPath=“”
file=Dir(您的路径和“Dashboard*.xls”)
当文件为vbNullString时执行
工作簿。打开(您的路径和文件)
file=Dir()
环
端接头

Workbook.Open需要完整路径,而不仅仅是文件名

很好的解决方案。我把你的答案更进一步,有点偏了:)而不是打开所有类似命名的文件。我需要打开最新的、名称类似的文件。所以我做了这个

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
Dim最新日期
暗电流作为日期
将右键文件设置为字符串
Dim rot_cnt作为整数
rot_cnt=1
将我的路径设置为字符串
Dim文件名为字符串
my_path=“C:\path\To\File\Dir”
file_name=Dir(我的路径和“我的相似文件*.xlsm”)
Do While file_name vbNullString
如果rot_cnt=1,则
最新=文件日期时间(我的路径和文件名)
如果结束
如果rot\u cnt>=1,则
当前=文件日期时间(我的路径和文件名)
如果结束
如果日期序列(年(当前)、月(当前)、日(当前))>=_
DateSerial(年(最新)、月(最新)、日(最新))然后
最新=文件日期时间(我的路径和文件名)
right\u file=我的路径和文件名
如果结束
文件名=Dir()
rot_cnt=rot_cnt+1
环
Workbooks.Open(右_文件),UpdateLinks:=False,ReadOnly:=True
在进一步测试之后,这将使用“真实”创建时间上的最后一次保存时间,因此它可能会返回不需要的结果。内置文档属性(“创建日期”)也是指向创建日期的虚假线索。如果有人复制工作簿,则会克隆此值。为了在不必手动启用任何新引用的情况下获得正确的结果,我使用了这个

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
Dim oFS作为对象
作为字符串的Dim StrFile
Dim rot_cnt作为整数
rot_cnt=1
暗电流作为日期
暗淡如日期
将右键文件设置为字符串
将我的路径设置为字符串
Dim文件名为字符串
my_path=“C:\path\To\File\Dir”
file_name=“我的类似文件*.xlsm”
StrFile=Dir(我的路径和文件名)
当Len(StrFile)>0时执行
Set of s=CreateObject(“Scripting.FileSystemObject”)
如果rot_cnt=1,则
最新=oFS.GetFile(我的路径和StrFile).DateCreated
如果结束
如果rot\u cnt>=1,则
current=oFS.GetFile(我的路径和StrFile).DateCreated
如果结束
'右侧(StrFile,6)If参数是因为Dir()也给出了精确的
'文件名字符串作为我们不希望处理的值之一。
如果日期序列(年(当前)、月(当前)、日(当前))>=_
日期序列(年(最新)、月(最新)、日(最新))_
右(StrFile,6)“*.xlsm”然后
最新=oFS.GetFile(我的路径和StrFile).DateCreated
右\u文件=我的\u路径和StrFile
如果结束
StrFile=Dir
集合=无
rot_cnt=rot_cnt+1
环
Workbooks.Open(右_文件),UpdateLinks:=False,ReadOnly:=True

很好的解决方案。我把你的答案更进一步,有点偏了:)而不是打开所有类似命名的文件。我需要打开最新的、名称类似的文件。所以我做了这个

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
Dim最新日期
暗电流作为日期
昏暗的右翼