Macro To Loop Through Worksheets To Match Value And Return All Results

Some videos you may like

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

rameezl17

Board Regular
Joined
Mar 6, 2018
Messages
105
so when i use your code in my file it gives me the correct roles (Project Support 1 and then Project Support 4, but it does it in column A & B which then removes the project name in Project A. Currently trying to test out to code to see if I can fix it
 

DataBlake

Well-known Member
Joined
Jan 26, 2015
Messages
751
Office Version
2016
Platform
Windows
so when i use your code in my file it gives me the correct roles
i thought you were getting a subscript out of range error?
what fixed it?

Code:
 For k = 1 To 4
        If ary1(j, 6) = ary1(20 + k, 6) Then
            Sheets("Report").Cells(5 + x, 1).Value = ary1(20, 5)
            If k <> 1 Then
            Sheets("Report").Cells(5 + x, 2).Value = "Project Support" & k
            x = x + 1
            Else
            Sheets("Report").Cells(5 + x, 2).Value = "Project Support"
            x = x + 1
            End If
        
        End If
    Next k

the 5 + x, 1 means A5
the 5 + x, 2 means B5


Code:
If ary1(j, 6) = ary1(20, 6) Then
    Sheets("Report").Cells(5 + x, 1).Value = ary1(20, 5)
    Sheets("Report").Cells(5 + x, 2).Value = "Project Lead"
    x = x + 1
Else
End If
same for this


so the "Project lead" and "Project Support"s should not be overwriting the project name found in E20
if you have any errors its because something was changed as i've attached a link to the workbook i'm using in post #41 and it works as intended?
 
Last edited:

DataBlake

Well-known Member
Joined
Jan 26, 2015
Messages
751
Office Version
2016
Platform
Windows
in my supplied workbook running this code

Code:
Sub timesTHREE()
Dim x As Long, i As Long, j As Long, k As Long, p As Long
Dim ary1 As Variant
Dim wsCOUNT As Long
Dim ws As Worksheet
Dim lastROW As Long, lastCol As Long


wsCOUNT = Application.Sheets.Count

'loops through the sheets
For i = 7 To wsCOUNT
    k = 0

'gets the sheets last row and last column
lastROW = Sheets(i).Range("A" & Rows.Count).End(xlUp).Row
lastCol = Sheets(i).Range("A1").SpecialCells(xlCellTypeLastCell).Column

'sets the current sheet as the array given there are no blank rows/columns
ReDim ary1(1 To lastROW, 1 To lastCol)
ary1 = Sheets(i).Range("A1").CurrentRegion.Value2

'loop through the rows of the array
For j = LBound(ary1) To UBound(ary1)
    
'find  matches between A2 and array
If Sheets("Report").Range("A2").Value = ary1(j, 6) Then

'loop to find project supports
    For k = 1 To 4
        If ary1(j, 6) = ary1(20 + k, 6) Then
            Sheets("Report").Cells(5 + x, 1).Value = ary1(20, 5)
            If k <> 1 Then
            Sheets("Report").Cells(5 + x, 2).Value = "Project Support" & k
            x = x + 1
            Else
            Sheets("Report").Cells(5 + x, 2).Value = "Project Support"
            x = x + 1
            End If
        
        End If
    Next k
          
'if to find project leads
If ary1(j, 6) = ary1(20, 6) Then
    Sheets("Report").Cells(5 + x, 1).Value = ary1(20, 5)
    Sheets("Report").Cells(5 + x, 2).Value = "Project Lead"
    x = x + 1
Else
End If
End If
        
Next j
Next i

End Sub
i get this result in sheets "Report"

AB
1
2Boofles
3
4
5Project 1Project Lead
6Project 3Project Support2
7Project 5Project Support

<colgroup><col style="width: 25pxpx"><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Report
 

DataBlake

Well-known Member
Joined
Jan 26, 2015
Messages
751
Office Version
2016
Platform
Windows
You have the project name in D20 not E20

try this
Code:
Sub timesTHREE()
Dim x As Long, i As Long, j As Long, k As Long, p As Long
Dim ary1 As Variant
Dim wsCOUNT As Long
Dim ws As Worksheet
Dim lastROW As Long, lastCol As Long


wsCOUNT = Application.Sheets.Count

'loops through the sheets
For i = 7 To wsCOUNT
    k = 0

'gets the sheets last row and last column
lastROW = Sheets(i).Range("A" & Rows.Count).End(xlUp).Row
lastCol = Sheets(i).Range("A1").SpecialCells(xlCellTypeLastCell).Column

'sets the current sheet as the array given there are no blank rows/columns
ReDim ary1(1 To lastROW, 1 To lastCol)
ary1 = Sheets(i).Range("A1").CurrentRegion.Value2

'loop through the rows of the array
For j = LBound(ary1) To UBound(ary1)
    
'find  matches between A2 and array
If Sheets("Report").Range("A2").Value = ary1(j, 6) Then

'loop to find project supports
    For k = 1 To 4
        If ary1(j, 6) = ary1(20 + k, 6) Then
            Sheets("Report").Cells(5 + x, 1).Value = ary1(20, 4)
            If k <> 1 Then
            Sheets("Report").Cells(5 + x, 2).Value = "Project Support" & k
            x = x + 1
            Else
            Sheets("Report").Cells(5 + x, 2).Value = "Project Support"
            x = x + 1
            End If
        
        End If
    Next k
          
'if to find project leads
If ary1(j, 6) = ary1(20, 6) Then
    Sheets("Report").Cells(5 + x, 1).Value = ary1(20, 4)
    Sheets("Report").Cells(5 + x, 2).Value = "Project Lead"
    x = x + 1
Else
End If
End If
        
Next j
Next i

End Sub
alternatively change:
ary1(20, 4) to ary1(2, 1)
since both are the project name
 
Last edited:

rameezl17

Board Regular
Joined
Mar 6, 2018
Messages
105
So that gave me the correct format but... my name is on 2 of the project tabs and the report is only showing Project 1
 

Forum statistics

Threads
1,089,337
Messages
5,407,672
Members
403,158
Latest member
Limerick2030

This Week's Hot Topics

  • help please
    SORRY NOT ANY GOOD AT EXCEL SO HELP WOULD BE MUCH APPRECIATED this formula is in a sheet called ignore...
  • two formulas needed
    Hello, I'll try my best to explain this: First formula needed in Sheet1 cell A2: If Sheet1 cell B2 = Sheet2 cell B2 then return a 1. If not then...
  • Dynamic Counts
    Good afternoon, we are tidying up some data & the data seems to be growing quicker than we are tidying it up! What we confirm (by reviewing it...
  • Help Excel formula eliminate duplicate values and keep only 2 identical rows.
    as picture below column A has a duplicate value. but the values are not the same as the rule. sometimes 4 rows, sometimes 10 rows or 7 or 9...
  • Macro Compile Error Sub or Function not defined
    Hello, I am trying to run macros from a validation list, all macros have been created and run perfectly on there own but I'm getting a compile...
  • Last row combined with Current Region VBA
    I'm generally happy finding the last row of data through something like Lastrow = Cells(Rows.Count, "D").End(xlUp) but I don't always receive data...
Top