Option Explicit
Const strSearchWord As String = "削除" & vbLf & "フラグ"
Const strDefFileName As String = "RPA用集計.xlsx"
Const masterSheet As String = "マスタ"
Const masterStartRow As Integer = 7
Dim strFolderPath As String
Dim fileName As String
Dim shName As String
Dim strCellAddress As String
Dim strRowNum As String
Dim strColName As String
Dim intFileStartRow As Integer
Dim intFileLastRow As Integer
Dim val1 As String
Dim val2 As String
Dim targetPeriod As Date
Dim lngResultNum As Long
Dim blBookOpend As Boolean
Sub main()
Debug.Print "■mainプロシージャ開始■"
Dim lastRow As Integer
Dim i As Integer
Dim strDate As String
Dim repotBook As Workbook
Dim toolBook As Workbook
ThisWorkbook.Activate
Set toolBook = ActiveWorkbook
blBookOpend = sameNameBookOpenCheck(strDefFileName)
If blBookOpend Then
MsgBox strDefFileName & "を閉じてから再実行してください。"
Exit Sub
End If
lastRow = toolBook.Sheets(masterSheet).Cells(Rows.Count, 4).End(xlUp).Row
Debug.Print "最終行:" & lastRow
Application.ScreenUpdating = False
For i = 7 To lastRow
strFolderPath = ""
val1 = ""
val2 = ""
With toolBook.Sheets(masterSheet)
val1 = .Cells(i, 2).Value
val2 = .Cells(i, 3).Value
strFolderPath = .Cells(i, 4).Value
strDate = Cells(3, 5).Value
strDate = strDate & Cells(3, 6).Value
targetPeriod = CDate(strDate)
End With
Debug.Print "val1 : " & val1
Debug.Print "val2 : " & val2
Debug.Print "targetPeriod : " & targetPeriod
Debug.Print "strFolderPath: " & strFolderPath
Debug.Print "テストプロシージャを呼び出し"
Call Process1(strFolderPath)
Debug.Print "◎mainプロシージャに戻った◎"
Debug.Print "strFolderPath : " & strFolderPath
Debug.Print "shName : " & shName
Debug.Print "fileName : " & fileName
Debug.Print "strCellAddress: " & strCellAddress
Debug.Print "intFileLastRow: " & intFileLastRow
Debug.Print "strRowNum; " & strRowNum
With toolBook.Sheets(masterSheet)
.Cells(i, 5) = fileName
.Cells(i, 6) = shName
.Cells(i, 7) = intFileStartRow
.Cells(i, 8) = intFileLastRow
.Cells(i, 9) = lngResultNum
If blBookOpend Then
.Cells(i, 10) = "ファイルが既に開かれている為処理できませんでした。"
End If
End With
Next
Application.ScreenUpdating = False
End Sub
Sub Process1(targetPath)
intFileLastRow = 0
Debug.Print "-------------------------------"
Debug.Print "■testプロシージャ開始■"
Debug.Print "△開始時の値△"
Debug.Print "strFolderPath : " & strFolderPath
Debug.Print "intFileLastRow: " & intFileLastRow
Dim wb As Workbook
fileName = Dir(targetPath & "\" & "*.xlsx")
Do While fileName <> ""
Debug.Print "fileName: " & fileName
blBookOpend = IsBookOpened(targetPath & "\" & fileName)
If blBookOpend = False Then
Set wb = Workbooks.Open(targetPath & "\" & fileName, False)
Debug.Print "searchCellプロシージャ呼び出し"
Debug.Print targetPath & fileName
Call searchCell(wb, strSearchWord)
Debug.Print "◎testプロシージャに戻った◎"
If shName <> "" Then
Call editCellAddress(strCellAddress)
intFileStartRow = intFileStartRow + 4
Debug.Print "◎テストプロシージャに戻った"
Call ReleaseFilter(wb, shName)
intFileLastRow = wb.Sheets(shName).Cells(Rows.Count, 4).End(xlUp).Row
Debug.Print "◎テストプロシージャに戻った"
lngResultNum = fncCalc(wb, shName, intFileStartRow, intFileLastRow)
wb.Close (False)
Set wb = Nothing
Exit Do
End If
fileName = Dir()
wb.Close (False)
Set wb = Nothing
Else
MsgBox "ブックは開かれています"
Exit Sub
End If
Loop
End Sub
Sub searchCell(wb As Workbook, strSearchWord As String)
strCellAddress = ""
shName = ""
Debug.Print "■SearchCellプロシージャ開始■"
Debug.Print "△開始時の値△"
Debug.Print "fileName :" & fileName
Debug.Print "shName :" & shName
Debug.Print "strCellAddress:" & strCellAddress
Debug.Print "strSearchWord :" & strSearchWord
Dim shCount As Integer
Dim sh As Variant
Dim foundCell As Range
shCount = wb.Sheets.Count
For Each sh In wb.Sheets
Set foundCell = wb.Sheets(sh.Name).Cells.Find(what:=strSearchWord, Searchorder:=xlByRows)
If Not foundCell Is Nothing Then
shName = sh.Name
strCellAddress = foundCell.Address
Exit For
End If
Next
Debug.Print "△終了時の値△"
Debug.Print "shName :" & shName
Debug.Print "strCellAddress:" & strCellAddress
Debug.Print "■SearchCellプロシージャ終了■"
End Sub
Sub editCellAddress(strCellAddress)
Dim reg As Object
Set reg = CreateObject("VBScript.RegExp")
strRowNum = ""
intFileStartRow = 0
strColName = ""
With reg
.Pattern = "[A-Z,$]"
.IgnoreCase = False
.Global = True
End With
strRowNum = reg.Replace(strCellAddress, "")
Debug.Print "strRowNum : " & strRowNum
intFileStartRow = CInt(strRowNum)
Debug.Print "intFileStartrow: " & intFileStartRow
With reg
.Pattern = "[0-9,$]"
.IgnoreCase = False
.Global = True
End With
strColName = reg.Replace(strCellAddress, "")
End Sub
Sub ReleaseFilter(wb As Workbook, shName As String)
Dim filterRange As String
Debug.Print "■ReleaseFilter開始■"
Debug.Print "△開始時の値△"
Debug.Print "shName: " & shName
With wb.Sheets(shName)
If .AutoFilterMode Then
filterRange = .AutoFilter.Range.Address
Debug.Print filterRange
Range(filterRange).AutoFilter
End If
End With
Debug.Print "■ReleaseFilter終了■"
End Sub
Function fncCalc(wb As Workbook, shName As String, intFileStartRow As Integer, intFileLastRow As Integer) As Long
Dim i As Integer
Dim strDeleteFlag As String
Dim targetDate As Date
Dim lngTargetNum As Long
Dim lngTotalNum As Long
Dim TargetFlag As Boolean
strDeleteFlag = ""
lngTargetNum = 0
lngTotalNum = 0
Debug.Print "■fncCalc開始■"
Debug.Print "△開始時の値△"
Debug.Print "shName : " & shName
Debug.Print "intFileStartRow: " & intFileStartRow
Debug.Print "intFileLastRow : " & intFileLastRow
Debug.Print "strDeleteFlag : " & strDeleteFlag
Debug.Print "targetDate : " & targetDate
Debug.Print "lngTargetNum : " & lngTargetNum
For i = intFileStartRow To intFileLastRow
With wb.Sheets(shName)
strDeleteFlag = .Cells(i, 3).Value
targetDate = .Cells(i, 5).Value
lngTargetNum = .Cells(i, 4).Value
TargetFlag = fncCheckData(strDeleteFlag, targetDate)
Debug.Print "◎fncCalcに戻り◎"
Debug.Print "targetFlag: " & TargetFlag
If TargetFlag Then
lngTotalNum = lngTotalNum + lngTargetNum
End If
Debug.Print "i: " & i
Debug.Print "lngTargetNum : " & lngTargetNum
Debug.Print "lngTotalNum : " & lngTotalNum
End With
Next
fncCalc = lngTotalNum * 10
Debug.Print "fncCalc : " & fncCalc
End Function
Function fncCheckData(strDeleteFlag As String, targetDate As Date) As Boolean
fncCheckData = True
Debug.Print "■fncCheckData開始■"
Debug.Print "△開始時の値△"
Debug.Print "targetPeriod : " & targetPeriod
Debug.Print "strDeleteFlag : " & strDeleteFlag
Debug.Print "targetDate : " & targetDate
If strDeleteFlag = "削除" Then
fncCheckData = False
End If
If targetDate < targetPeriod Then
fncCheckData = False
End If
Debug.Print "■fncCheckData終了■"
Debug.Print "△終了時の値△"
Debug.Print "fncCheckData : " & fncCheckData
End Function
Function StartCheck(strRange1 As String, strRange2 As String) As Boolean
Dim strErr As String
With ThisWorkbook.Sheets("マスタ")
If .Range(strRange1) = "" Then
strErr = "対象年" & vbCrLf
End If
If .Range(strRange2) = "" Then
strErr = "対象月日"
End If
End With
If strErr <> "" Then
strErr = vbCrLf & "以下の項目をしてから実行1ボタンを押して下さい。"
StartCheck
End If
End Function
Public Function IsBookOpened(ByVal FilePath As String) As Boolean
On Error Resume Next
Open FilePath For Append As #1
Close #1
Debug.Print Err.Number
If Err.Number > 0 Then
IsBookOpened = True
End If
End Function
Function sameNameBookOpenCheck(targetPath As String) As Boolean
Dim ChkBook As Workbook
On Error GoTo ErrHdl
Set ChkBook = Workbooks(targetPath)
sameNameBookOpenCheck = True
Exit Function
ErrHdl:
sameNameBookOpenCheck = False
End Function
ray88.hatenablog.com
ray88.hatenablog.com