Hello asha3010,
I made some changes to your macro. There is no need to trap the error as it can be avoided. It will now check if the range is empty by using the CountA function. This will return the number of cells that contain strings, numbers, dates, or formulae. If the range is empty, the macro jumps to Errhandler.
Public Sub ConcatenateArray()
Dim vArray1 'List of companies in the data entry form
Dim vArray2 'List of companies in the database
Dim vArray3 'Concatenated list
Dim i As Integer
Dim iSizeA1 As Integer 'Number of elements in vArray1
Dim iSizeA2 As Integer 'Number of elements in vArray2
'Transpose the lists (single column range) to a one dimensional array
With Names("Test").RefersToRange
If WorksheetFunction.CountA(.Cells) = 0 Then
MsgBox "Empty List. Nothing to update": GoTo Errhandler
Else
vArray1 = WorksheetFunction.Transpose(.Parent.Evaluate("IF(ROW(" & .Address & ")," & .Address & ")"))
End If
End With
With Names("DB").RefersToRange
If WorksheetFunction.CountA(.Cells) = 0 Then
MsgBox "Empty List. Nothing to update": GoTo Errhandler
Else
vArray2 = WorksheetFunction.Transpose(.Parent.Evaluate("IF(ROW(" & .Address & ")," & .Address & ")"))
End If
End With
'Determine the number of elements in vArray1 & vArray2
iSizeA1 = UBound(vArray1)
iSizeA2 = UBound(vArray2)
'Set vArray3 = vArray2 & then add elements from vArray1 to this list
vArray3 = vArray2
ReDim Preserve vArray3(1 To iSizeA1 + iSizeA2)
For i = 1 To iSizeA1
vArray3(iSizeA2 + i) = vArray1(i)
Next i
Errhandler:
vArray3 = vArray1
End Sub
Bookmarks