Looping through sheets and retrieving data from specific cells

Alemap

New Member
Joined
Dec 29, 2019
Messages
30
Office Version
  1. 2010
Platform
  1. Windows
Good Day everyone, I'm trying to loop through many sheets in a workbook and retrieve data from a table array of size A54:AD73 on a particular date and post the values into a table array in a results sheet. (see table below)
The values to be collected against a Name, which is in cell C1 of every sheet, will be in columns Y to AD in the row with the date, which could be in different rows for each sheet. The date if it has been entered, resides in Column B of every sheet.

1577614507831.png

I Have being trying to combine code that I already have that loops through all sheets and retrieves summary data from the same cells of each sheet.

The code below runs but it only retrieves data from row B54 in which I entered the date for each sheet for testing but am unable to proceed with looping throgh the sheets, match the date and post the data in the results sheet

[VBA Code]
Sub MonthlyMedal()


ActiveSheet.Unprotect Password:="xxxx"
Dim i As Long
Dim j As Long
Dim MatchDay As Date
Dim Lastrow As Long

Dim wksMonthlyMedal As Worksheet
Dim wksCurr As Worksheet
Dim arrData() As Variant
Dim intRow As Integer
'MatchDay = InputBox("Match Date Is")
MatchDay = "19/01/19"


intRow = 0
ReDim arrData(1 To ThisWorkbook.Worksheets.Count - 1, 1 To 7)

Set wksResults = ThisWorkbook.Worksheets("Results")
Set wksLady_Players = ThisWorkbook.Worksheets("Lady_Players")
Set wksSurvey = ThisWorkbook.Worksheets("Survey")
Set wksTemplate = ThisWorkbook.Worksheets("Template")
Set wksMonthlyMedal = ThisWorkbook.Worksheets("MonthlyMedal")

Dim screenUpdateState
Dim calcState
Dim eventsState

'check functionality status

screenUpdateState = Application.ScreenUpdating
calcState = Application.Calculation
eventsState = Application.EnableEvents

'turn off screen updating to stop flicker & increase speed
'turn off automatic recalculating mode
'turn off events processing

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False



For Each wksCurr In ThisWorkbook.Worksheets

If wksCurr.Name <> wksResults.Name _
And wksCurr.Name <> wksLady_Players.Name _
And wksCurr.Name <> wksSurvey.Name _
And wksCurr.Name <> wksMonthlyMedal.Name _
And wksCurr.Name <> wksTemplate.Name Then


'For j = 54 To 56
' If wksCurr.Range(j, "B") = MatchDay Then
' MsgBox ActiveWorkbook.Worksheets(i).Cells(j, "B").Value
' MsgBox wksCurr.Range(j, "B").Value
' Next
'Lastrow = Lastrow + 1
'End If
'
intRow = intRow + 1
arrData(intRow, 1) = wksCurr.Range("C1") 'B Name
arrData(intRow, 2) = wksCurr.Range("Y54") 'C Score 1
arrData(intRow, 3) = wksCurr.Range("Z54") 'D Score 2
arrData(intRow, 4) = wksCurr.Range("AA54")'E Score 3
arrData(intRow, 5) = wksCurr.Range("AB54") 'F Score 4
arrData(intRow, 6) = wksCurr.Range("AC54") 'G Putts
arrData(intRow, 7) = wksCurr.Range("AD54") 'H Division
'

End If
Next wksCurr
wksMonthlyMedal.Range("B5").Resize(UBound(arrData), UBound(arrData, 2)) = arrData
Set wksCurr = Nothing
Set wksMonthlyMedal = Nothing

Application.ScreenUpdating = screenUpdateState
Application.Calculation = calcState
Application.EnableEvents = eventsState
ActiveSheet.Protect Password:="7410", DrawingObjects:=True, Contents:=True, Scenarios:=True


End Sub [/Code]
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Assuming that all of your variables are valid, try changing this section
VBA Code:
'For j = 54 To 56
' If wksCurr.Range(j, "B") = MatchDay Then
' MsgBox ActiveWorkbook.Worksheets(i).Cells(j, "B").Value
' MsgBox wksCurr.Range(j, "B").Value
' Next
'Lastrow = Lastrow + 1
'End If
'
intRow = intRow + 1
arrData(intRow, 1) = wksCurr.Range("C1") 'B Name
arrData(intRow, 2) = wksCurr.Range("Y54") 'C Score 1
arrData(intRow, 3) = wksCurr.Range("Z54") 'D Score 2
arrData(intRow, 4) = wksCurr.Range("AA54")'E Score 3
arrData(intRow, 5) = wksCurr.Range("AB54") 'F Score 4
arrData(intRow, 6) = wksCurr.Range("AC54") 'G Putts
arrData(intRow, 7) = wksCurr.Range("AD54") 'H Division
'
to
VBA Code:
For j = 54 To 56
    If wksCurr.Range(j, "B") = MatchDay Then
        intRow = intRow + 1
        arrData(intRow, 1) = wksCurr.Cells(j, "C") 'B Name
        arrData(intRow, 2).Resize(1, 6) = wksCurr.Cells(j, "Y")
    End If
Next
'

I haven't checked the rest of the code for any potential errors.
 
Upvote 0
Thank you for your prompt reply jason75,unfortunately it comes up with error 1004 , object defined error on the second line of your suggested replacement code?
 
Upvote 0
Oops, I forgot to change Range to Cells
VBA Code:
For j = 54 To 56
    If wksCurr.Cells(j, "B") = MatchDay Then
        intRow = intRow + 1
        arrData(intRow, 1) = wksCurr.Cells(j, "C") 'B Name
        arrData(intRow, 2) = wksCurr.Cells(j, "Y").Resize(1, 6) ' Columns Y:AD
    End If
Next
'
I just noticed that arrData is an array, not an indexed range as I first thought (my fault for not reading the rest of the code). I've made a slight change to one of the other lines as well, but I suspect that it will not work this way and that the 5th line will need to be re-written the long way with 1 line for each column rather than 1 to do all of them.

Also, I left the first line as 54 To 56 as this was what you had in your original code, but I notice that you mentioned a range that went from rows 54 To 73 in post 1, so you will need to change that to whichever of the 2 is correct.
 
Upvote 0
Hi jasonb75, thank you still have a problem. I replaced the section of code with below, but still comes up with the error.
I have left line 4 of your code as ....... = wksCurr.cells(j , "B") because that is the row that the date is found in?
1577644149817.png


Code:
For j = 54 To 56
      If wksCurr.Range(j, "B") = MatchDay Then
      
            intRow = intRow + 1
            arrData(intRow, 1) = wksCurr.Cells("C1") 'B Name
            arrData(intRow, 2) = wksCurr.Cells(j, "Y").Resize(1, 6)
            arrData(intRow, 3) = wksCurr.Cells(j, "Z").Resize(1, 6)  '= wksCurr.Range("Z54")
            arrData(intRow, 4) = wksCurr.Cells(j, "AA").Resize(1, 6) ' = wksCurr.Range("AA54") 
            arrData(intRow, 5) = wksCurr.Cells(j, "AB").Resize(1, 6) ' = wksCurr.Range("AB54")
            arrData(intRow, 6) = wksCurr.Cells(j, "AC").Resize(1, 6) ' = wksCurr.Range("AC54") 
            arrData(intRow, 7) = wksCurr.Cells(j, "AD").Resize(1, 6) ' = wksCurr.Range("AD54") 
      
      End If
    Next
[Code/]
 
Upvote 0
What you're showing me in that screen capture is a break point that you've added to the code. An error would be in yellow, not red.
The only potential error(s) that I can see are the .Resize() commands which shouldn't be there. I had used .Resize to capture all 6 columns together when I thought that arrData was a range to paste to rather than an array.

You either need
VBA Code:
For j = 54 To 56
      If wksCurr.Range(j, "B") = MatchDay Then
      
            intRow = intRow + 1
            arrData(intRow, 1) = wksCurr.Cells("C1") 'B Name
            arrData(intRow, 2) = wksCurr.Cells(j, "Y")
            arrData(intRow, 3) = wksCurr.Cells(j, "Z") '= wksCurr.Range("Z54")
            arrData(intRow, 4) = wksCurr.Cells(j, "AA") ' = wksCurr.Range("AA54") 
            arrData(intRow, 5) = wksCurr.Cells(j, "AB") ' = wksCurr.Range("AB54")
            arrData(intRow, 6) = wksCurr.Cells(j, "AC")' = wksCurr.Range("AC54") 
            arrData(intRow, 7) = wksCurr.Cells(j, "AD"). ' = wksCurr.Range("AD54")  
      End If
Next
or you need to drop the arrData array in favour of writing the results to the other sheet line by line, with something like
VBA Code:
For j = 54 To 56
    If wksCurr.Cells(j, "B") = MatchDay Then
        intRow = intRow + 1
        wksMonthlyMedal.Range("B5").offset(introw) = wksCurr.Cells(j, "C") 'B Name
        wksMonthlyMedal.Range("B5").offset(introw,1) = wksCurr.Cells(j, "Y").Resize(1, 6) ' Columns Y:AD
    End If
Next
 
Upvote 0
Yes OK, I'll change it for the offset option , try it and come back to you later tomorrow morning as it is passed my bedtime. Thanks again for your kind help.
.
 
Upvote 0
@ Alemap
Basically the post #4 code of jasonb75 does the job, but your proc in its entirety needs a small revision.
The snippet below might have raised the error.
VBA Code:
If wksCurr.Name <> wksResults.Name _
And wksCurr.Name <> wksLady_Players.Name _
And wksCurr.Name <> wksSurvey.Name _
And wksCurr.Name <> wksMonthlyMedal.Name _
And wksCurr.Name <> wksTemplate.Name Then
This IF-END IF statement results in the folowing: the 5 mentioned sheets will be skipped and data will be retrieved from the other worksheets (if present) in ThisWorkbook.
The solution is simple (IF-ELSE-END IF) but the layout of the 5 mentioned worksheets is preferred to be similar (location of date, name, scores, etcetera), otherwise an Object-error can occur.
As far as I understand, the RESULTS worksheet is not a source sheet rather a target sheet. When this sheet (by change) is the first one in your FOR-EACH-loop in where data retrieval takes place, another Object-error is lurking.
 
Upvote 0
but the layout of the 5 mentioned worksheets is preferred to be similar (location of date, name, scores, etcetera), otherwise an Object-error can occur.
That is not true, an error would only occur in such cases if you tried to perform an operation on one of those sheets that doesn't match the layout used in the code, or on a sheet that doesn't exist.
With the way that the code is written, the only possible error that could be caused by one of the excluded sheets would be a Runtime Error 9 at the point of setting each sheet to an object variable, which happens before the If - Else name testing.

Given that each name is typed into the code anyway, I would personally use If InStr("/" & wksCurr.Name" & "/","/Results/Lady_Players/Survey/Template/MonthlyMedal/") Then to eliminate some unnecessary varaibles and condense approx 10 lines of code into 1.
 
Upvote 0
That is not true, an error would only occur in such cases if you tried to perform an operation on one of those sheets that doesn't match the layout used in the code, or on a sheet that doesn't exist.
That's exactly what I meant :)
The code expects a certain layout but it turns out to be a (completely) different one.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,021
Latest member
pingpong7117

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top