Good day.
I am looking for a solution for a problem, where in i have hundreds of CSV files located in a folder/nested folders.
I am trying to write a VBA script, which loads the list of files in this directory to Excel's work sheet-1 as soft links to each files. when a link is clicked, i am expecting the file contents to be loaded to worksheet 2, which would be used for further reporting purposes.
I have tried writing a VB script to extract the list of folder contents, however, i couldn't get them converted to soft links and loading the file contents. Any suggestions on how (else) could this be achieved ?
Any help in this regard is highly appreciated. Thanks in advance.
答案 0 :(得分:2)
将有2部分代码。第一个代码将转到一个模块,列出文件夹和地址中文件的名称。第一列是名称,第二列是地址
Sub ListFiles()
Dim wb As Workbook
Dim ws As Worksheet
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Long
Set wb = ActiveWorkbook '<-- Master workbook
Set ws = wb.Sheets("Sheet1") '<-- Sheet you store file names and addresses
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\youfolderaddress")
i = 1
For Each objFile In objFolder.Files
ws.Cells(i + 1, 1) = objFile.Name
ws.Cells(i + 1, 2) = objFile.Path
i = i + 1
Next objFile
End Sub
第二部分将转到存储文件列表的工作表,我们将使用workshet选择更改:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim wb As Workbook
Dim ws1, ws2 As Worksheet
Dim newBook As Workbook
Dim newSheet As Worksheet
Set wb = ActiveWorkbook '<- Master workbook with all list etc.
Set ws1 = wb.Sheets("Sheet1") '<-- Sheet that contains the list
Set ws2 = wb.Sheets("Sheet2") '<-- Sheet that will display csv content
If Target.Column = 2 And Target.Value <> "" Then '2 is the number of the column that contains file addresses
ws2.Cells.ClearContents
Workbooks.Open Filename:=Target.Value
Set newBook = ActiveWorkbook
Set newSheet = newBook.ActiveSheet
newSheet.Cells.Copy
ws2.Activate
ActiveSheet.Cells(1, 1).Select
ActiveSheet.Paste
Application.DisplayAlerts = False
newBook.Close
Application.DisplayAlerts = True
End If
End Sub
答案 1 :(得分:1)
You could use event handlers. I assume that you are able to load the names into sheet 1 and set a range variable equal to the resulting range of names. You can then use the selection change event to trigger your loading code using something like the following (in the sheet1 code module). Clicking on a cell containing a file name will cause that name to be displayed:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim fnames As Range
Set fnames = Range("fnames") 'adjust to match your code
If Union(Target, fnames).Address = fnames.Address And _
Target.Cells.Count = 1 Then
MsgBox Target.Value 'replace this by code to load csv in sheet 2
End If
End Sub
答案 2 :(得分:1)
With this code you can, with a default directory, retreive the list of file and add on sheet with the list (linked) of this file. You can change this code with your to select the different directory
Sub ListAllFile()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim ws As Worksheet
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set ws = Worksheets.Add
'Get the folder object associated with the directory
Set objFolder = objFSO.GetFolder("C:\Users\Desktop\Code\Excel")
ws.Cells(1, 1).Value = "The files found in " & objFolder.Name & "are:"
'Loop through the Files collection
For Each objFile In objFolder.Files
ws.Cells(ws.UsedRange.Rows.Count + 1, 1).Select
Selection.Hyperlinks.Add Anchor:=Selection, Address:=objFile.Name
Next
'Clean up!
Set objFolder = Nothing
Set objFile = Nothing
Set objFSO = Nothing
End Sub