I am trying to build a macro that loops through every worksheet in a workbook and records the "bill area" each worksheet is about and some other information in each workbook. My program works for the first worksheet but doesn't work for the second worksheet. The same standardized text that I am searching for is there, yet Range.Find is not working. Here is the code:
Sub DenialsReport() Dim heatWB, denialWB As Workbook Dim heatWS, denialWS As Worksheet Dim folderName, fileName, extName, wbName, billArea, postPeriod As String Dim a, b, lastRow, countCol, volumeCol, startRow, billAreaRow, billAreaColumn, grandRow As Long Dim postPeriodRange, billAreaRange, grandRange As Range Set heatWB = ActiveWorkbook Set heatWS = heatWB.Sheets("Denials") postPeriod = InputBox("Please enter the current post period in this form: yyyymm") Application.ScreenUpdating = False folderName = heatWB.Sheets("Support->").Cells(19, 3).Value fileName = heatWB.Sheets("Support->").Cells(20, 3).Value extName = heatWB.Sheets("Support->").Cells(21, 3).Value wbName = folderName + "\" + fileName + "." + extName Set denialWB = Workbooks.Open(wbName) b = 0 For Each denialWS In Worksheets denialWS.Cells.UnMerge Next denialWS For Each denialWS In Worksheets b = b + 1 denialWB.Sheets(b).Cells.UnMerge lastRow = heatWS.Cells(Rows.Count, 1).End(xlUp).Row + 1 ' find the bill area Set billAreaRange = denialWB.Sheets(b).Range("A1:G10").Find(What:="For Bill Area: ", LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False) billAreaRow = billAreaRange.Row billAreaColumn = billAreaRange.Column ' get the bill area billArea = denialWB.Sheets(b).Cells(billAreaRow, billAreaColumn).Value billArea = Replace(billArea, "For Bill Area: ", "") billArea = Left(billArea, InStr(billArea, "(") - 1) billArea = Trim(billArea) ' locate the count and amount columns along with the start and end of the rejection categories Set postPeriodRange = d enialWB.Sheets(denialWS.Index).Range("A1:AZ15").Find(What:=postPeriod, LookIn:=xlValues, LookAt:=xlWhole) countCol = postPeriodRange.Column amountCol = countCol + 1 startRow = postPeriodRange.Row + 2 Set grandRange = denialWB.Sheets(denialWS.Index).Range("A:A").Find(What:="Grand Total", LookIn:=xlValues, LookAt:=xlWhole) grandRow = grandRange.Row ' copy the information over to heatWS For a = startRow To grandRow - 1 heatWS.Cells(lastRow, 1).Value = billArea heatWS.Cells(lastRow, 2).Value = postPeriod heatWS.Cells(lastRow, 3).Value = denialWS.Cells(a, 1).Value heatWS.Cells(lastRow, 4).Value = denialWS.Cells(a, countCol).Value heatWS.Cells(lastRow, 5).Value = denialWS.Cells(a, amountCol).Value heatWS.Cells(lastRow, 6).Value = denialWS.Cells(a, amountCol).Value / denialWS.Cells(grandRow, amountCol).Value lastRow = heatWS.Cells(Rows.Count, 1).End(xlUp).Row + 1 Next a Next denialWS denialWB.Close SaveChanges:=False heatWS.Range("A:F").AutoFit Application.ScreenUpdating = True End Sub The problem occurs on this line:
Set billAreaRange = denialWB.Sheets(b).Range("A1:G10").Find(What:="For Bill Area: ", LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False) I have also tried to hardcode the program like this (value lies in cell C7):
billArea = denialWB.Sheets(b).Cells(7,3).Value I believe this means that VBA isn't recognizing that there is a value in the cell. However, I am able to physically enter the cell and make edits. Any help is appreciated. Thank you.
denialWSreference.denialWS.Range("A1:G10").Find.For Eachlooping over the worksheets and then manually trying to keep the index in sync is silly. It's also error prone, because an enumerated collection isn't required to return them in index order.