2017-07-27 115 views
0

我有一个名为“原始数据”的文件夹,并有几个Excel文件名,扩展名为.xlsx 我有另一个启用了宏的Excel文件作为“Test.xlsm”。如何直接访问文件夹和访问Excel文件

现在,我有一个宏,以这样的方式工作,它进入本地目录,然后打开Excel文件。当我改变我的文件夹时,这对我来说是不可行的。

是否有可能,我可以有一个代码,以这种方式,它只是寻找文件夹“原始数据”。并打开我提到的文件。

我不知道如何做到这一点。任何潜在客户都会有帮助。

现在我有下面的代码工作。 (不过,这看起来从一个驱动器位置d的原始数据“)

Private Sub CommandButton11_Click() 
Dim filename As String 

Workbooks.Open ("D:\Jenny\Raw data\Result.xlsx") 

filename = ActiveWorkbook.Path & "\Result.xlsx" 

End Sub 
+0

你是什么意思的“寻找文件夹”?你不知道*文件夹在哪里? –

+0

如果您希望宏允许您浏览文件夹,请使用'Application.FileDialog(msoFileDialogFolderPicker)' –

+1

您不会创建在任何地方寻找'\ raw data' *的东西,这可能需要很长时间才能完成非常低效。您需要知道它的位置,例如在用户文档或应用程序数据文件夹等知道的路径中或与ActiveWorkbook.Path相关的某个地方 - 例如它的子目录。 –

回答

0

假设用户知道在哪里的文件夹,只是提示输入:

Dim fldr$ 
Dim fdlg As FileDialog 
Set fdlg = Application.FileDialog(msoFileDialogFolderPicker) 
fdlg.Show 

If fdlg.SelectedItems.Count <> 0 Then 
    fldr = fdlg.SelectedItems(1) 
Else: 
    Exit Sub 
End If 

Dim wb as Workbook 
Set wb = Workbooks.Open(fldr & Application.PathSeparator & "Results.xlsx") 

当然,你应该有错误 - 在办案的文件没有在文件夹中存在用户选择,等等

或者使用Application.FileDialog(msoFileDialogFilePicker)来提示用户定位手动文件。应用程序无法以其他方式知道的文件可能存在 - 他们可能在任何地方,或者他们甚至可能不存在于用户可以访问的位置。

Dim resultsBook as Workbook 
Dim testBook as Workbook 
Dim fdlg as FileDialog 
Set fdlg = Application.FileDialg(msoFileDialogFilePicker) 
MsgBOx "Select the Results file" 
fdlg.Show 
If fdlg.SelectedItems.Count <> 0 Then 
    Set resultsBook = Workbooks.Open(fdlg.SelectedItems(1)) 
Else: 
    Exit Sub 
End If 
MsgBox "Select the Test file" 
fdlg.Show 
    If fdlg.SelectedItems.Count <> 0 Then 
    Set testBook = Workbooks.Open(fdlg.SelectedItems(1)) 
Else: 
    Exit Sub 
End If 
0

使用CreateObject("Shell.Application")

Sub tst() 

    Dim oShell As Object 
    Dim sFolderPath As String 

    Set oShell = CreateObject("Shell.Application").BrowseForFolder(0, "Please choose a folder", 0) 
    If oShell Is Nothing Then Exit Sub 'Pressed cancel 

    sFolderPath = oShell.Self.Path & Application.PathSeparator 

    MsgBox sFolderPath 
    'Workbooks.Open sFolderPath & "Result.xlsx" 

End Sub 
0

这听起来像您要重,可能移动的原始数据文件夹,而不是“破发”的宏观能力的另一种方法。如果是这种情况,请将Test.xlsm文件保存在原始数据文件夹中。

然后做一些像这样的循环打开&处理文件夹中的每个XLSX原始数据文件。我的代码是userFiles /你可能是rawDataFiles或其他东西。

userFilesPath = ThisWorkbook.Path 
userFileName = Dir(userFilesPath & "*.xlsx", vbNormal) 

Do While userFileName <> "" 
    On Error Resume Next 
    userFile = userFilesPath & userFileName 

    ' this is the raw data file 
    On Error Resume Next 
    Set uf = Workbooks.Open(Filename:=userFile, UpdateLinks:=False, ReadOnly:=True) 

    ' do some stuff with the raw data 
    On Error Resume Next 
    For Each s In uf.Sheets 
     If Len(s.Range("a1").Value) > 1 Then 
      s.Range("a1:z" & s.Range("a1000000").End(xlUp).Row).Copy 
      ws.Range("a" & ws.Range("a1000000").End(xlUp).Row + 1).PasteSpecial xlPasteValues 
     End If 
     Application.CutCopyMode = False 
    Next 
    uf.Close False 

    userFileName = Dir 
Loop 
+0

能否请您评论我们使用此代码做什么? – Jenny

+0

你的代码适合我的要求。请你帮我避免错误? – Jenny