I might post my question here and hope for help before I bust my head through the wall.
Here is the deal: I have a date(Data_carga
) and a time(Hora_carga
) for the load the user changed its status. So I want to apply his change to my back-end Loads Table.
My Load table (Table5
) is always custom sorted by date. So what I thought was to xmatch
my Data_carga
against the date column (Worksheets("CargasBD").Range("Table5[DATA]")
) both from first to last (row1
) and from last to first(row2
). And then, using those two rows combined in an indirect function, I would xmatch
the Hora_carga
against that specific interval. Then I would have the map to the same load on the back-end table to update it.
But I cannot get the xmatch
function to simple locate a value in a table on another tab.I keep getting this runtime error 1004.
Both my looked-up value and table are Date type. So I have no idea why I am getting this error.
Code is bellow.
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Dim Data_carga As Date
Dim Hora_carga As Date
Dim Novo_status As String
Dim row, row1, row2 As Integer
Dim addrss, Range_data As String
Set In_range = Application.Intersect(Target, Range("$D$3:$P$11"))
If Not In_range Is Nothing Then
Data_carga = Range("A" & Target.row).Value
Hora_carga = Range(Left(Target.Address, Len(Target.Address) - Len(CStr(Target.row))) & "2").Value
Novo_status = Application.WorksheetFunction.XLookup(Target.Value, Sheets("BD").Range("Table17[Abrev]"), Sheets("BD").Range("Table17[Status das cargas]"), "", 0, 1)
'This is where it breaks
row1 = Application.WorksheetFunction.XMatch(Data_carga, Worksheets("CargasBD").Range("Table5[DATA]"), 0, 1)
row2 = Application.WorksheetFunction.XMatch(Data_carga, Worksheets("CargasBD").Range("Table5[DATA]"), 0, -1)
addrss = Application.WorksheetFunction.Concat("CargasBD!C", 1 + row1, ":C", 1 + row2)
Range_data = Application.WorksheetFunction.INDIRECT(addrss, True)
row = Application.WorksheetFunction.XMatch(Hora_carga, Range_data, 0, 1)
Sheets("CargasBD").Range("D" & row + row1).Value = Novo_status
End If
Application.EnableEvents = True
End Sub
Btw, the date(Data_carga
) and a time(Hora_carga
) are getting fetched okay.
Thank you in advance.