Vous êtes sur la page 1sur 16

2013

EXCEL VBA
VBA Script for Microsoft Excel

TomIngalls Actions Can Explain 10/2/2013

List Datas In A Folder to a spreedsheet


*enbale Macros *enable 'Microsoft Scripting Runtime'

Dim iRow

Sub ListFiles() iRow = 11 Call ListMyFiles(Range("C7"), Range("C8") ) paste the file path in this range End Sub

Sub ListMyFiles(mySourcePath, IncludeSubfolders) Set MyObject = New Scripting.FileSystemObject Set mySource = MyObject.GetFolder(mySourcePath) On Error Resume Next For Each myFile In mySource.Files iCol = 2 Cells(iRow, iCol).Value = myFile.Path iCol = iCol + 1 Cells(iRow, iCol).Value = myFile.Name iCol = iCol + 1

Cells(iRow, iCol).Value = myFile.Size iCol = iCol + 1 Cells(iRow, iCol).Value = myFile.DateLastModified iRow = iRow + 1 Next If IncludeSubfolders Then For Each mySubFolder In mySource.SubFolders Call ListMyFiles(mySubFolder.Path, True) Next End If End Sub

List Folder Name, Path,#files inside


*enbale Macros *enable 'Microsoft Scripting Runtime'

Sub ListFolders() Application.ScreenUpdating = False Workbooks.Add ' create a new workbook for the folder list ' add headers With Range("A1")

.Formula = "Folder contents:" .Font.Bold = True .Font.Size = 12 End With Range("A3").Formula = "Folder Path:" Range("B3").Formula = "Folder Name:" Range("C3").Formula = "Size:" Range("D3").Formula = "Subfolders:" Range("E3").Formula = "Files:" Range("A3:G3").Font.Bold = True ListFolders "C:\file path\", True Raplace with the file path Application.ScreenUpdating = True End Sub

Sub ListFolders(SourceFolderName As String, IncludeSubfolders As Boolean) ' lists information about the folders in SourceFolder ' example: ListFolders "C:\FolderName", True Dim FSO As Scripting.FileSystemObject Dim SourceFolder As Scripting.Folder, SubFolder As Scripting.Folder Dim r As Long Set FSO = New Scripting.FileSystemObject Set SourceFolder = FSO.GetFolder(SourceFolderName) ' display folder properties r = Range("A65536").End(xlUp).Row + 1

Cells(r, 1).Formula = SourceFolder.Path Cells(r, 2).Formula = SourceFolder.Name Cells(r, 3).Formula = SourceFolder.Size Cells(r, 4).Formula = SourceFolder.SubFolders.Count Cells(r, 5).Formula = SourceFolder.Files.Count If IncludeSubfolders Then For Each SubFolder In SourceFolder.SubFolders ListFolders SubFolder.Path, True Next SubFolder Set SubFolder = Nothing End If Columns("A:G").AutoFit Set SourceFolder = Nothing Set FSO = Nothing ActiveWorkbook.Saved = True End Sub

Mp3 File List


*enbale Macros *enable 'Microsoft Scripting Runtime'

Option Explicit ' By John Walkenbach ' Maybe be distributed freely, but not sold

'API declarations Declare Function SHGetPathFromIDList Lib "shell32.dll" _ Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long

Declare Function SHBrowseForFolder Lib "shell32.dll" _ Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long

Public Type BROWSEINFO hOwner As Long pidlRoot As Long pszDisplayName As String lpszTitle As String ulFlags As Long lpfn As Long lParam As Long

iImage As Long End Type

Sub GetAllFiles() Dim Msg As String Dim Directory As String Msg = "Select the directory that contains the Log files. All subdirectories will be included." Directory = GetDirectory(Msg) If Directory = "" Then Exit Sub If Right(Directory, 1) <> "\" Then Directory = Directory & "\" Worksheets("Sheet1").Activate Cells.Clear Call RecursiveDir(Directory) End Sub

Function GetDirectory(Optional Msg) As String Dim bInfo As BROWSEINFO Dim path As String Dim r As Long, x As Long, pos As Integer ' Root folder = Desktop bInfo.pidlRoot = 0& ' Title in the dialog If IsMissing(Msg) Then bInfo.lpszTitle = "Select a folder."

Else bInfo.lpszTitle = Msg End If ' Type of directory to return bInfo.ulFlags = &H1 ' Display the dialog x = SHBrowseForFolder(bInfo) ' Parse the result path = Space$(512) r = SHGetPathFromIDList(ByVal x, ByVal path) If r Then pos = InStr(path, Chr$(0)) GetDirectory = Left(path, pos - 1) Else GetDirectory = "" End If End Function

Public Sub RecursiveDir(ByVal currdir As String) Dim Dirs() As String Dim NumDirs As Long Dim filename As String Dim PathAndName As String

Dim i As Long Dim Row As Long

' Make sure path ends in backslash If Right(currdir, 1) <> "\" Then currdir = currdir & "\"

Application.ScreenUpdating = False

' Put column headings on active sheet Cells(1, 1) = "Path" Cells(1, 2) = "Filename" Cells(1, 3) = "Artist" Cells(1, 4) = "Album" Cells(1, 5) = "Title" Cells(1, 6) = "Track#" Cells(1, 7) = "Genre" Cells(1, 8) = "Duration" Cells(1, 9) = "Size" Range("A1:I1").Font.Bold = True

' Get files filename = Dir(currdir & "*.*", vbDirectory) Do While Len(filename) <> 0 If Left$(filename, 1) <> "." Then 'Current dir

PathAndName = currdir & filename If (GetAttr(PathAndName) And vbDirectory) = vbDirectory Then 'store found directories ReDim Preserve Dirs(0 To NumDirs) As String Dirs(NumDirs) = PathAndName NumDirs = NumDirs + 1 Else If UCase(Right(filename, 3)) = "MP3" Then Row = WorksheetFunction.CountA(Range("A:A")) + 1 Cells(Row, 1) = currdir 'path Cells(Row, 2) = filename 'filename Cells(Row, 3) = FileInfo(currdir, filename, 20) 'artist Cells(Row, 4) = FileInfo(currdir, filename, 14) 'album Cells(Row, 5) = FileInfo(currdir, filename, 21) 'title Cells(Row, 6) = FileInfo(currdir, filename, 26) 'track Cells(Row, 7) = FileInfo(currdir, filename, 16) 'genre Cells(Row, 8) = FileInfo(currdir, filename, 27) 'duration Cells(Row, 9) = Application.Round(FileLen(currdir & filename) / 1024, 0) 'size Application.StatusBar = Row End If End If End If filename = Dir() Loop

' Process the found directories, recursively For i = 0 To NumDirs - 1 RecursiveDir Dirs(i) Next i Application.StatusBar = False End Sub

Function FileInfo(path, filename, item) As Variant Dim objShell As IShellDispatch4 Dim objFolder As Folder3 Dim objFolderItem As FolderItem2

Set objShell = CreateObject("Shell.Application") Set objFolder = objShell.Namespace(path) Set objFolderItem = objFolder.ParseName(filename) FileInfo = objFolder.GetDetailsOf(objFolderItem, item)

Set objShell = Nothing Set objFolder = Nothing Set objFolderItem = Nothing End Function

Clock
*enbale Macros *enable 'Microsoft Scripting Runtime'

Option Explicit Dim NextTick

Sub StartClock() UpdateClock End Sub

Sub StopClock() ' Cancels the OnTime event (stops the clock) On Error Resume Next Application.OnTime NextTick, "UpdateClock", , False End Sub

Sub cbClockType_Click() ' Hides or unhids the clock With ThisWorkbook.Sheets("Clock") If .DrawingObjects("cbClockType").Value = xlOn Then .ChartObjects("ClockChart").Visible = True Else

.ChartObjects("ClockChart").Visible = False End If End With End Sub

Sub UpdateClock() ' Updates the clock that's visible Dim Clock As Chart Set Clock = ThisWorkbook.Sheets("Clock").ChartObjects("ClockChart").Chart

If Clock.Parent.Visible Then ' ANALOG CLOCK Const PI As Double = 3.14159265358979 Dim CurrentSeries As Series Dim s As Series Dim x(1 To 2) As Variant Dim v(1 To 2) As Variant

'

Hour hand Set CurrentSeries = Clock.SeriesCollection("HourHand") x(1) = 0 x(2) = 0.5 * Sin((Hour(Time) + (Minute(Time) / 60)) * (2 * PI / 12)) v(1) = 0 v(2) = 0.5 * Cos((Hour(Time) + (Minute(Time) / 60)) * (2 * PI / 12))

CurrentSeries.XValues = x CurrentSeries.Values = v

'

Minute hand Set CurrentSeries = Clock.SeriesCollection("MinuteHand") x(1) = 0 x(2) = 0.8 * Sin((Minute(Time) + (Second(Time) / 60)) * (2 * PI / 60)) v(1) = 0 v(2) = 0.8 * Cos((Minute(Time) + (Second(Time) / 60)) * (2 * PI / 60)) CurrentSeries.XValues = x CurrentSeries.Values = v

'

Second hand Set CurrentSeries = Clock.SeriesCollection("SecondHand") x(1) = 0 x(2) = 0.85 * Sin(Second(Time) * (2 * PI / 60)) v(1) = 0 v(2) = 0.85 * Cos(Second(Time) * (2 * PI / 60)) CurrentSeries.XValues = x CurrentSeries.Values = v Else

'

DIGITAL CLOCK ThisWorkbook.Sheets("Clock").Range("DigitalClock").Value = CDbl(Time) End If

' Set up the next event one second from now NextTick = Now + TimeValue("00:00:01") Application.OnTime NextTick, "UpdateClock" End Sub

YOU CAN DOWNLOAD THE DEMO FROM HERE


http://www.scribd.com/doc/172475715/Clock-Chart

Vous aimerez peut-être aussi