I have two sheets. One is a table and contains data that I want entered into the other. The other looks almost like a gantt chart, with names down the side and dates across the top (see here).
I want the program to run in the manner specified below but run as is, it returns:
Run-time error '438':
Object doesn't support this property or method
on
For Each Row1 In Resource
I have attempted various fixes but each time I adjust one error, I seem to cause another!
- Check the table column "Resource Allocated" and find the matching name in the first column of the calendar sheet.
- Check the table column "Date Allocated" and find the matching value in the first row of the calendar sheet.
- Select the cell where these intersect (the cell with a column number of "Date Allocated" and a row number of "Resource Allocated").
- Offset the column number according to a third table column, "Time of Day".
- Fill the cell with the RGB colour specified in the code.
- Repeat for every row.
Option Explicit
Sub CalendarSync()
Sheets("Log").Select
Dim Resource As ListColumn
Dim Dates As ListColumn
Dim ToD As ListColumn
Dim Row1 As ListRow
Dim Row2 As ListRow
Dim Row3 As ListRow
Set Resource = ActiveSheet.ListObjects("Table1").ListColumns("Resource Allocated")
Set Dates = ActiveSheet.ListObjects("Table1").ListColumns("Date Allocated")
Set ToD = ActiveSheet.ListObjects("Table1").ListColumns("Time of Day")
Dim ResMatch As Variant
Dim DateMatch As Variant
For Each Row1 In Resource
'Cross Referencing Dates & Resources Allocated
ResMatch = Application.Match(Resource, Worksheets("Calendar").Columns(1), 0)
For Each Row2 In Dates
DateMatch = Application.Match(Dates, Worksheets("Calendar").Rows(1), 0)
'Offsetting to Account for Time of Day
For Each Row3 In ToD
If ToD = "PM" Then
DateMatch.ColumnOffset (1)
End If
If ToD = "EVE" Then
DateMatch.ColumnOffset (1)
End If
'Fill the Cell
Range(ResMatch, DateMatch).Interior.Color = RGB(244, 66, 182)
Next Row3
Next Row2
Next Row1
End Sub
As a thought: while there certainly is a way to loop over your list object, the following might be closer to what you need:
Recordset
-objectRecordset
instead of the list-objectThis...
Field.Names
ListObjects
Here's an example of how to use a recordset:
Notes:
YourRecordsetObject!YourColumn
or (inside aWith
) a simple!YourColumn
to retrieve the value.If ... Then ... Else
and speed up your processHope this helps.
I've done some significal changes in your code. The
Match
function does not work very well in this case, I think using theFind
method gives you a better response. Have a look on these changes: