Created
April 23, 2013 01:37
-
-
Save fornext1119/5440166 to your computer and use it in GitHub Desktop.
フォルダ内の全Excelファイルの特定のセル内容を書き換える
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Option Explicit | |
Private fs: Set fs = WScript.CreateObject("Scripting.FileSystemObject") | |
Private folder: Set folder = fs.GetFolder(WScript.Arguments(0)) | |
Private xl: Set xl = CreateObject("Excel.Application") | |
xl.Visible = True | |
xl.DisplayAlerts = False '警告メッセージをOFF | |
'ファイル一覧を取得 | |
Call filelist(folder) | |
xl.Quit | |
Set xl = Nothing | |
Set fs = Nothing | |
'ファイル一覧を取得 | |
Private Sub filelist(folder) | |
Dim file | |
For Each file In folder.Files | |
'WScript.Echo file.Name | |
'WScript.Echo fs.GetExtensionName(file) | |
If LCase(fs.GetExtensionName(file)) = "xls" Then | |
'WScript.Echo fs.GetAbsolutePathName(file) | |
'シート一覧を取得 | |
sheetlist(fs.GetAbsolutePathName(file)) | |
End If | |
Next | |
Dim subFolder | |
For Each subFolder In folder.SubFolders | |
filelist(subFolder) | |
Next | |
End Sub | |
'シート一覧を取得 | |
Private Sub sheetlist(abs_path) | |
WScript.StdOut.Write abs_path | |
Dim bk: Set bk = xl.Workbooks.Open(abs_path) | |
Dim st | |
'For Each st In bk.Sheets | |
' WScript.Echo st.Name | |
'Next | |
Set st = Nothing | |
On Error Resume Next | |
Set st = bk.Sheets("あああああ") | |
On Error GoTo 0 | |
If Not (st Is Nothing) Then | |
If updateSheet(st) Then | |
bk.Save | |
WScript.StdOut.WriteLine " 修正" | |
Else | |
WScript.StdOut.WriteLine " 修正対象行がない" | |
End If | |
Else | |
WScript.StdOut.WriteLine " 修正対象シートがない" | |
End If | |
bk.Close | |
Set bk = Nothing | |
End Sub | |
'シート内容修正 | |
Private Function updateSheet(st) | |
updateSheet = False | |
Dim row | |
For row = 1 To 99 | |
If st.Cells(row, 1) = "いいいいい" Then | |
st.Cells(row, 1) = "ううううう" | |
updateSheet = True | |
Exit For | |
End If | |
Next | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment