Const ForReading = 1, ForWriting = 2, ForAppending = 8 '概要: フォルダー選択ダイアローグを出して、フォルダを選択 '引数: 無し '戻り値: getFilePath 選択されたパス Function getFilePath() As String Dim fd As FileDialog Set fd = Application.FileDialog(msoFileDialogFolderPicker) With fd .Title = "フォルダを開く" If .Show = -1 Then getFilePath= .SelectedItems(1) End If End With End Function '概要: 指定された行番を読む '引数: strRowNo 行番 '戻り値: readRowNo 指定行目の内容 Function readRowNo(strPath As String, strRowNo As Integer) As String Dim FileObj Dim FilePath Dim strNY As String Set FileObj = CreateObject("Scripting.FileSystemObject") Set File = FileObj.openTextFile(strPath, 1) For i = 1 To strRowNo File.SkipLine Next readRowNo = File.readLine File.Close Set File = Nothing Set FileObj = Nothing End Function '概要: フォルダー中のファイル一覧(全てファイル)を取得(サブフォルダー含む) '引数: strInPath パス, arrOutput 戻す配列 '戻り値: makeFileList「true:ファイル有り, false:ファイル無し」 Function makeFileList(strInPath As String, arrOutput() As String) As Boolean Dim fso As Object Dim fd As Object makeFileList = False ReDim arrOutput(0) Set fso = CreateObject("Scripting.FileSystemObject") Set fd = fso.GetFolder(strInPath) searchFiles fd, arrOutput Set fso = Nothing Set fd = Nothing If 0 < UBound(arrOutput, 1) Then ReDim Preserve arrOutput(UBound(arrOutput, 1) - 1) Else Exit Function End If makeFileList = True End Function '概要: フォルダー中のファイル一覧(指定拡張子)を取得(サブフォルダー含む) '引数: strInPath:パス, strFileType:ファイル類型 ,arrOutput 戻す配列 '戻り値: makeFileList「true:ファイル有り, false:ファイル無し」 Function makeFileList(strInPath As String, strFileType As String, arrOutput() As String) As Boolean Dim fso As Object Dim fd As Object makeFileList = False ReDim arrOutput(0) Set fso = CreateObject("Scripting.FileSystemObject") Set fd = fso.GetFolder(strInPath) searchFiles fd, strFileType, arrOutput Set fso = Nothing Set fd = Nothing If 0 < UBound(arrOutput, 1) Then ReDim Preserve arrOutput(UBound(arrOutput, 1) - 1) Else Exit Function End If makeFileList = True End Function '概要: フォルダー中のファイル一覧(全てファイル)を取得 '引数: fd フォルダー対象, arrOutput 戻す配列 '戻り値: searchFiles「true:ファイル有り, false:ファイル無し」 Function searchFiles(fd As Object, arrOutput() As String) As Boolean Dim fl As Object Dim sfd As Object searchFiles = False For Each fl In fd.Files ReDim Preserve arrOutput(UBound(arrOutput, 1) + 1) arrOutput(UBound(arrOutput, 1) - 1) = fl.Path Next fl If fd.SubFolders.Count = 0 Then Exit Function End If For Each sfd In fd.SubFolders searchFiles sfd, strFileType, arrOutput Next Set sfd = Nothing searchFiles = True End Function '概要: フォルダー中のファイル一覧(指定拡張子)を取得 '引数: fd フォルダー対象, strFileType:ファイル類型, arrOutput 戻す配列 '戻り値: searchFiles「true:ファイル有り, false:ファイル無し」 Function searchFiles(fd As Object, strFileType As String, arrOutput() As String) As Boolean Dim fl As Object Dim sfd As Object Dim sTmp As String sTmp = "" searchFiles = False sTmp = Split(strFileType, ".")(1) sTmp = "." & sTmp For Each fl In fd.Files If UCase(Right(fl.Path, Len(sTmp))) = UCase(sTmp) Then ReDim Preserve arrOutput(UBound(arrOutput, 1) + 1) arrOutput(UBound(arrOutput, 1) - 1) = fl.Path End If Next fl If fd.SubFolders.Count = 0 Then Exit Function End If For Each sfd In fd.SubFolders searchFiles sfd, strFileType, arrOutput Next Set sfd = Nothing searchFiles = True End Function '概要: ファイル内容を取得 '引数: strFileName ファイル名, arrContent ファイル正文配列 '戻り値: readFile「true:正常, false:異常」 Function readFile(strFileName As String, arrContent As Variant) As Boolean Dim fso As Object Dim sFile As Object readFile = False If isFileExists(strFileName) Then 'NULL Else Exit Function End If ReDim arrContent(0) Set fso = CreateObject("Scripting.FileSystemObject") Set sFile = fso.openTextFile(strFileName, 1) Do While Not sFile.AtEndOfStream ReDim Preserve arrContent(UBound(arrContent, 1) + 1) arrContent(UBound(arrContent, 1) - 1) = sFile.readLine Loop sFile.Close Set fso = Nothing Set sFile = Nothing If 0 < UBound(arrContent, 1) Then ReDim Preserve arrContent(UBound(arrContent, 1) - 1) Else Exit Function End If readFile = True End Function '概要: ファイル存在を判定 '引数: strFileName ファイル名 '戻り値: isFileExists「true:存在, false:存在しない」 Function isFileExists(strFileName As String) As Boolean Dim fs As Object Set fs = CreateObject("Scripting.FileSystemObject") If fs.FileExists(strFileName) Then isFileExists = True Else isFileExists = False End If Set fs = Nothing End Function '概要: ファイルに内容を記入 '引数: strFileName:ファイル名, arrContent:記入したい内容, intIoMode:処理モード, booCreate:ファイル存在無し「true:作成, false:作成しない」 '戻り値: 無し Function writeFile(strFileName As String, arrContent() As String, _ ByVal intIoMode As integer, ByVal booCreate As Boolean) As Boolean Dim fso As Object Dim sFile As Object Dim i As Long i = 0 writeFile = False Set fso = CreateObject("Scripting.FileSystemObject") Select Case intIoMode Case 8 '追加 Set sFile = fso.openTextFile(strFileName, 8, booCreate) Case 1 '読む Set sFile = fso.openTextFile(strFileName, 1, booCreate) Case 2 '書く Set sFile = fso.openTextFile(strFileName, 2, booCreate) End Select For i = 0 To UBound(arrContent, 1) sFile.writeLine (arrContent(i)) Next i sFile.Close Set fso = Nothing Set sFile = Nothing writeFile = True End Function '概要: ログタイトルを記入 '引数: strOutFile 目標ファイル名 '戻り値: 無し Function writeLogHeader(strOutFile As String) Dim cOutput() As String ReDim cOutput(0) 'タイトルを指定 cOutput(0) = """" & "No." & """" & vbTab cOutput(0) = cOutput(0) & """" & "Source File" & """" & vbTab cOutput(0) = cOutput(0) & """" & "Row Number" & """" & vbTab cOutput(0) = cOutput(0) & """" & "Row Infor" & """" Call writeFile(strOutFile, cOutput, ForAppending, True) End Function '概要: ログ正文を記入 '引数: strOutFile:ファイル名, lNum:順番, strSrcFile:ソースファイル, lngRownum:行番, strRowContent:行正文 '戻り値: 無し Function writeLog(strOutFile As String, lNum As Long, strSrcFile As String, lngRownum As Long, strRowContent As String) Dim cOutput() As String ReDim cOutput(0) cOutput(0) = Chr(34) & lNum & Chr(34) & vbTab cOutput(0) = cOutput(0) & Chr(34) & strSrcFile & Chr(34) & vbTab cOutput(0) = cOutput(0) & Chr(34) & lngRownum & Chr(34) & vbTab 'cOutput(0) = cOutput(0) & Chr(34) & strRowContent & Chr(34) cOutput(0) = cOutput(0) & strRowContent Call writeFile(strOutFile, cOutput, ForAppending, True) End Function '概要: 指定パスのファイルを消す '引数: strFilePath:目標パス '戻り値: true:操作成功, false:操作失敗 Function deleteAllFiles(ByVal strFilePath As String) As Boolean Dim arrOutput() As String Call makeFileList(strFilePath, arrOutput) For i = 0 To UBound(arrOutput) If isFileExists(arrOutput(i)) Then Kill arrOutput(i) End If Next i deleteAllFiles = True End Function
相关推荐
ExcelVBA操作文件四大方法之二利用VBA文件处理语句来处理文件.pdf
本资源实现了用VBA(宏)代价遍历文件夹中的csv文件,修改代码也可遍历其他格式的excel文件,并实现了将遍历的结果整合到一个新的excel文件中。
本文主要介绍四种常用的方法: 1、利用Excel对象来处理文件; 2、利用VBA文件处理语句来处理文件; 3、利用FileSystemObject对象来处理文件; 4、利用API函数来处理文件。
本文就系统地介绍一下在Excel中应用VBA操作数据文件的方法 1、利用Excel对象来处理文件;...2、利用VBA文件处理语句来处理文件; 3、利用FileSystemObject对象来处理文件; 4、利用API函数来处理文件。
讲述了VBA中操作 window 系统的文件的四种方法 分别是 1、利用Excel对象来处理文件;...2、利用VBA文件处理语句来处理文件; 3、利用FileSystemObject对象来处理文件; 4、利用API函数来处理文件。
本文主要介绍四种常用的方法: 1、利用Excel对象来处理文件; 2、利用VBA文件处理语句来处理文件; 3、利用FileSystemObject对象来处理文件; 4、利用API函数来处理文件。
具体向读者讲述了vba语言基础,vba程序设计网络教学,xml与asp技术,利用vba操纵文件和文件夹,利用vba控制其它应用程序,自定义集合和类模块,调试vba过程和处理错误等等,希望对网友们有帮助。
通过VBA将多个格式相同的Excel文件合并成一个文件,带文件处理
批量文件处理+VBA+文件批量重命名+自动化办公利器
一本非常好的CAD VBA学习教材 第 1 章、VBA入门 1 、了解嵌入和全局VBA工程 2、用VBA管理器组织工程 3、处理宏 4、用VBA IDE编辑工程 5、更多的信息 6、回顾AutoCAD VBA 工程术语 7、回顾AutoCAD VBA 命令 第2 章、...
VBA读取文件夹下所有文件的三种方法,能够读取到子文件夹下所有的文件。
VBA处理文件的高质量速度问题,文件处理,重命名等
利用EXCEL的VBA窗体功能写的一个小程序,用来处理ASN标签文档
在工作中,需要、在几个execl表中取数,合成一个表格,手工操作费时费力,就作了一个宏,效率很高,原来要几个小时的活,不到一分种就ok
第一章 VBA入门 了解嵌入和全局VBA工程 用VBA管理器组织工程 处理宏 用VBA IDE编辑工程 更多的信息 回顾AutoCAD VBA 工程术语 回顾AutoCAD VBA 命令 第二章 理解ActiveX自动操作基础.. 理解AutoCAD对象模型 ...
利用Solidworks的VBA实现了一个变压器的自动建模。
VBA是Office软件所支持的,可以自动处理重复,复杂的流程的脚本。该脚本涉及使用VBA打开另一个excel,并获取数据;使用模糊查询算法获取数据;流程控制语句If、While、Set、With等语句。对于学习和相关用途十分有帮助...
利用excel中的vba可以对电脑中的文件及文件夹做一些常用的操作。 包括复制、重命名、删除等,其中一些简单的示例总结如下。 希望对一些经常需要批量处理文件的朋友有所帮助,也希望感兴趣的朋友多多指教!