In case I did not make myself understood, a live example:
I have two tables:
Table1 - main table, with lots of data about products we make. Two columns are of interest: Tool and Notes
TableToolNote - table I keep on SetUp sheet, with two columns only: Tool and Notes
The idea is, I populate second table with tools and corresponding notes. I have a macro, that would populate Notes in first table with Notes from second table if Tool from both tables matches.
Code:
Sub FillCustomNotes(wsJ As Worksheet, wsW As Worksheet)
Application.ScreenUpdating = False
Dim cell As Range
Dim rng As Range
Dim tbl As ListObject, tblWtL As ListObject
Dim notesCol As Integer
Dim toolCol As Integer
Dim fillColor As Long
Dim note As String
Dim indexer As Integer
Set tbl = wsJ.ListObjects("TableToolNote")
Set tblWtL = wsW.ListObjects("Table1")
notesCol = wsW.Range("Table1[Notes]").Column
toolCol = wsW.Range("Table1[Tool]").Column
If tbl.DataBodyRange.item(1, 1) <> "" Then
indexer = 0
For Each cell In tbl.ListColumns(1).DataBodyRange
indexer = indexer + 1
Set rng = Nothing
note = tbl.DataBodyRange.item(indexer, 2)
fillColor = tbl.DataBodyRange.item(indexer, 2).Interior.Color
tblWtL.Range.AutoFilter field:=toolCol, Criteria1:=cell.Value, Operator:=xlFilterValues
On Error Resume Next
Set rng = tblWtL.Range.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If Not rng Is Nothing Then
tblWtL.ListColumns(notesCol).DataBodyRange.SpecialCells(xlCellTypeVisible).Value = note
tblWtL.ListColumns(notesCol).DataBodyRange.SpecialCells(xlCellTypeVisible).Interior.Color = fillColor
End If
Next cell
If tblWtL.AutoFilter.FilterMode Then
tblWtL.AutoFilter.ShowAllData
End If
End If
End Sub
Now, I use foreach to go through every element in second table, filter Table1 and populate Table1[Notes] with variable 'note'. But to assign a value to note, I use indexer while looping through second table and retrieve it with line:
note = tbl.DataBodyRange.item(indexer, 2)
My question is, if I can retrieve it, without using indexer and col number. Is there a way of writing a code, that will tell VBA to retrieve value from TableToolNote at the same row level from column name 'Note', something like: TableToolNote[@Note]
By the way, at the beginning, I do a check:
If tbl.DataBodyRange.item(1, 1) <> ""
I also have a macro, that uses a listbox on userform to list all items in TableToolNote, lets user chose and delete item. However, if a user removes last item, the table still has one row, but it is empty. Can I check if table is empty (has that row, but no data) with Is Empty, or other function? So far, all I've tried failed and that wast the only workaround I came up with
Thanks