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]
 
Highlighting the part that you overlooked.
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.
There is no attempt to perform any operation on the excluded sheets, the name test will exclude them before that happens, which means no cause for error!
 
Upvote 0

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
I've overlooked more than your comment, jasonb75.
The 5 "IF" sheets are not the ones meant for data extraction, they are excluded.
With that in mind I totally agree with you. Thank's for your feedback! (y)
 
Upvote 0
It's a mistake many of us have made GWteB
Quite often the result of simply not following the method used because it is similar, but not identical to the method that you would use.
If the OP had used the exact same line with 'Or' instead of 'And' then it would have been a recipe for disaster.
 
Upvote 0
I've overlooked more than your comment, @jasonb75.
The 5 "IF" sheets are not the ones meant for data extraction, they are excluded.
With that in mind I totally agree with you. Thank's for your feedback!

Thank you Jasonb75 and GWteB for your comments. Yes the 5 IF sheets are excluded and are not meant for data extraction - most of the code come from a module I have been using for years to summarize annual scores from some 150 sheets - the trouble started when I assumed it would be easy to use the bones of that code for another purpose and that is to loop through the sheets and extract values from given columns from the tables within the sheets for a given date - a date input by the operator.
The idea is that the operator inputs a date, the program checks each sheet (other than the 5 IF sheets as they have been called)and if the input date is found in column B of the table, the program extracts the values from that row of the table and columns Y , Z, AA, Ab, AC and AD. If the date is not found the loop goes to the next sheet.
I have not tried the OFFSET option in place of the array as suggested in post #6 . I will get back to you as soon as i have tried it.
Thanks again for your help and suggestions.
 
Upvote 0
I have tried the OFFSET option and it runs without any error but only picks up the name in cell C1, it does not retrieve the values in Y to AD

1577688501695.png


I have changed the first line to (1,"C") as that cell is constant for all sheets and contains the name of the competitor. If i left it as (j, "C") it retrieves the value in column c row j which is not the name.
So it looks like the program only does one loop through the sheets retrieving one value , that in "C1" Assuming I have followed your instructions correctly @jasonb75 ?
 
Upvote 0
I have tried the OFFSET option and it runs without any error but only picks up the name in cell C1, it does not retrieve the values in Y to AD

View attachment 2891

I have changed the first line to (1,"C") as that cell is constant for all sheets and contains the name of the competitor. If i left it as (j, "C") it retrieves the value in column c row j which is not the name.
So it looks like the program only does one loop through the sheets retrieving one value , that in "C1" Assuming I have followed your instructions correctly @jasonb75 ?
Sorry I did not change the Offset(IntRow) in the above but it does not make the difference - it still does not retrieve the cell values in the Y to AD columns when the Offset values are 2,3,4,5,6 .
 
Upvote 0
With the offset method, you only need the first line with .Resize which should do all 6 columns. I think that this has become more than a little confusing witht the change of method and all of the side discussions.

Assuming that AE:AJ are empty, the last line wksCurr.Cells(j "AD").Resize(1, 6) is copying those emtpy cells and overwriting the data from the first line.

The 'Name' always needs a separate line because it is separate from the rest of the data in the sheet.
For the rest (columns Y:AD), the array method needed 6 lines, 1 for each columns. The offset / resize method only needs 1 line for all 6 columns. (The Resize method should work with an array, I have done it before but don't remember how). In this instance, Offset should be simpler.
The instruction, .Resize(1, 6) expands the range from Cell(i, "Y") to 1 row by 6 columns, copying them all over at once.

If you would prefer, I (or perhaps @GWteB) will redo the whole thing for you later (I'm going to be offline for a few hours), I had the impression from your posts that you wanted to try and do this yourself which is why I have only been giving you small sections to work with, but there is no sense in struggling for too long.
 
Upvote 0
If you have the time it would be very nice if you "re-hash" the code for me. I have been running it on live data and it is almost there, but produces some inconsistent results? Below is a snip of the live results of the MonthlyMedal running through all rows from 54 to 73 , for date 23/02/19 . The program has not retrieved the values in column Y and the values in AA are offset by one column, +1 and should be in Z . AA and AB should be blank and the last 2 are also offset by 1 column +1.
1577723033214.png


Thank you. Much appreciated.
 
Upvote 0
Just like jasonb75 I'm willing to redo (a part of) your project. To be certain (and not overlook something LOL) I'm going to summarize the data you have:
- approx 150 score sheets all with the same layout, one sheet per player (to extract data from);
- on each of those above sheets: cell C1 = players name, different (or not different) dates in column B (field can be empty) and score numbers in six adjacent columns (Y: AD);
- one sheet for displaying those scores of all players on a given date (layout like in #1);
- one sheet for the monthly medal (layout like in #18) which is btw allmost similar (as far as I can see) to the sheet for displaying scores per date.
It's almost 1.00h at night (at least where I live) so I'm off for a while (have to work tomorrow and the next few day's, not knowing when I can be online again). Anyway, if you can confirm the above or have some additions, let us know. In advance Happy New Year to the both of you and all the other board members.
 
Upvote 0
That would be great GWteB, and Jasonb75. I would love either of you guys to tidy this up for me. Thank you. These are the essentials:
- approx 150 score sheets all with the same layout, one sheet per player (to extract data from);
- on each of those above sheets: cell C1 = players name, different (or not different) dates in column B (field can be empty) and score numbers in six adjacent columns (Y: AD); Column AD is a text value , A , B, Etc.
- Column B contains dates (dd/mm/yyyy) and depending, column B could be empty - if a player hasn't recorded a score yet.
-Sheets like #1 and #18 are one and the same. #18 is a screen grab of live data that I have been testing. So the sheet for displaying the scores is only used when a date is input - once or twice a month.
-The table of results must remain until a new date is input via a MsgBox or drop-down list of dates.
- A refinement will be to display the input date - maybe in cell A4?
- On any date there may not be values in Y and Z or in AA and AB. There must always be values in AC and AD.
- Rows 1,2,3,4 in table #18 are reserved for labeling and buttons etc
- Table #18 is a bare skeleton and I will sort it out once I know the values are being collected properly. I will also use form control buttons on all columns of the results table for sorting the columns.
I hope I have covered all necessary information.
Thank you both again. GWteb must be far East of me I Guess, New Year is midnight to night.
Likewise from me, A Happy New Year to both and your families!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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