Search code examples
excelvbaexcel-tableslistobject

Locate a cell among several tables then increment that cell's value by 1


This is a simplified version of the problem using only 2 tables. My actual setup has 8 tables, but I'm sure I can expand a concept that works with 2 tables to make it work with 8.

The situation:

  • There are two separate tables with similar content. Example given further on.
  • Each is structured as a named table in Excel (Table901 and Table902) with column headers.
  • Each has 2 columns of relevant data: ID and Mark.
  • 1st column: unique numeric ID code. Table901[ID].
  • 2nd column: positive whole number value assigned to that code. Table901[Mark].
  • Cell Q43 has a value in it that should match an ID in one of the tables. Q43 would be changed to a new ID before each use of this macro.

The challenge: I want a macro that will look at the value in cell Q43, locate the matching ID across all tables, and increment the corresponding Mark value by 1.

Example: I want to start with something like this:

|   Table901   |   |    Table902  |   | Cell Q43 |
|  ID   | Mark |   |  ID   | Mark |   |   57551  |
| 84234 |   5  |   | 67485 |  15  |
| 45731 |  17  |   | 98431 |   3  |
| 74693 |  12  |   | 57551 |  10  |

And activate the macro to get this:

|   Table901   |   |    Table902  |   | Cell Q43 |
|  ID   | Mark |   |  ID   | Mark |   |   57551  |
| 84234 |   5  |   | 67485 |  15  |
| 45731 |  17  |   | 98431 |   3  |
| 74693 |  12  |   | 57551 |  11  |

Where the value in the Mark column corresponding to the ID in Q43 has been incremented by 1.

I've been mulling this problem over for a couple years, trying out various formulas for returning a cell address in one table, or a value from the Mark column on any table, but I haven't figured out how to implement any of it in a macro.

The closest I've come is using this formula in a random cell in the sheet (cell V43) to get the intended Mark value: =FILTER(Table901[Mark],Table901[ID]=Q43,FILTER(Table902[Mark],Table902[ID]=Q43))+1

In the example tables above, this would search through the first table, fail to find the ID specified in Q43, continue by searching the second table, successfully find the ID, note that the corresponding Mark value is 10, increment it by 1, and return 11.

However, as convenient as that formula is at locating the exact Mark value I want among multiple tables and performing the increment, it just gives me that new value in whichever cell I have the formula in. I still have to locate the correct cell myself and enter the new value. As I'm sure you can guess, the "+1" part isn't the hard part, it's the "find the correct cell" part that can get difficult when there are potentially thousands of entries across multiple tables.


Solution

  • This can be accomplished by looping through the two tables on the active sheet using VBA. The logic can be easily extended to validate all tables on all worksheet.

    Sub demo()
        Dim sId, arr, oTab As ListObject
        Dim c As Range, i As Long
        sId = [Q43]
        For Each oTab In ActiveSheet.ListObjects
            If oTab.Name = "Table901" Or oTab.Name = "Table902" Then
                arr = oTab.DataBodyRange.Value
                For i = 1 To UBound(arr)
                    If arr(i, 1) = sId Then
                        With oTab.DataBodyRange.Cells(i, 2)
                            .Value = .Value + 1
                        End With
                        Exit For
                    End If
                Next
            End If
        Next
    End Sub