I'm struggling with a VBA subroutine and would appreciate some assistance. The script is intended to perform the following tasks:
- Read values from column
LADUNGSNUMMERin the worksheetNVLcell by cell. - Find each value in column
Transportof the worksheetDATA. - Read the corresponding values from column
Faktura(1:n relationship). - Concatenate these values into a string.
- Write the concatenated string into column
EX-Fakturenof the worksheetNVL.
Unfortunately, I'm encountering a Types incompatible error at the line:
Set transportColumn = .Range("Transport", .Cells(.Rows.Count, "Transport").End(xlUp)
However, I can retrieve the range successfully because Debug.Print returns the correct result. The issue seems to be with the definition. Could someone please take a look and provide some guidance?
Sub NVLFakturenLaden()
Dim ws As Worksheet
Dim loadNumberRange As Range
Dim cell As Range
Dim fakturaValue As String
Dim counter As Long
Dim transportColumn As Range
Dim deliveryColumn As Range
Dim deliveryCell As Range
Dim deliveryValue As String
Dim result As String
' Set the worksheet with the data
Set ws = ThisWorkbook.Sheets("NVL")
' Define the range based on the named range "LADUNGSNUMMER"
Set loadNumberRange = ws.Range("LADUNGSNUMMER")
' Initialize the counter
counter = 0
' Loop through the cells in the named range "LADUNGSNUMMER"
For Each cell In loadNumberRange
' Define the ranges for "Transport" and "Faktura" columns on the "DATA" worksheet
With ThisWorkbook.Sheets("DATA")
If Application.WorksheetFunction.CountA(.Range("Transport")) > 0 Then
' Define the range
Debug.Print Application.WorksheetFunction.CountA(.Range("Transport"))
Set transportColumn = .Range("Transport", .Cells(.Rows.Count, "Transport").End(xlUp))
Else
MsgBox "No data in the 'Transport' range.", vbExclamation
End If
If Application.WorksheetFunction.CountA(.Range("Faktura")) > 0 Then
' Define the range
Set deliveryColumn = .Range("Faktura", .Cells(.Rows.Count, "Faktura").End(xlUp))
Else
MsgBox "No data in the 'Faktura' range.", vbExclamation
End If
End With
' Search for the value in the "Transport" column and concatenate corresponding "Faktura" values
For Each deliveryCell In deliveryColumn
If deliveryCell.value = cell.value Then
deliveryValue = CStr(deliveryCell.value)
' Concatenate the "Faktura" value to the result
If Len(result) > 0 Then
result = result & ", " & deliveryValue
Else
result = deliveryValue
End If
End If
Next deliveryCell
' Assign the result to the cell one column to the right of the current cell
cell.Offset(0, 1).value = result
' Check if a Faktura was loaded
If result <> "" Then
counter = counter + 1
End If
' Reset the result for the next iteration
result = ""
Next cell
MsgBox "Factura loaded to " & counter & " transports.", vbInformation
End Sub

Debugging delivers
Error 13
A Delimited VBA Lookup in Excel (Structured) Tables
If you have MS365, Office 2021, and I'm not sure about Office 2019, you could use the following formula in the first cell of column
EX_Fakturen(clear the column first):