runtime error 5 invalid procedure call or argument

wCJanssen

New Member
Joined
Feb 22, 2009
Messages
24
Hi,

The following code is intended to loop through a list of dates, format them into "YYYY"-format, check if there's an identical item in the array yet, add any new items to the array and write it to a worksheet. It returns 'runtime error 5 invalid procedure call or argument', though.

Code:
Private Sub cmd_try_Click()
Dim dataRange As Range
Dim oneCell As Range
Dim newYear As String
Dim arr_years() As String
Dim i As Integer
 
Set dataRange = Range(Sheets("Overzicht_aanmeldingen").Cells(4, 10), Sheets("Overzicht_aanmeldingen").Cells(4, 10).End(xlDown))

For Each oneCell In dataRange
If Not IsError(Application.Match(oneCell.Value, arr_years, 0)) Then
i = i + 1
newYear = CStr(Year(oneCell.Value))
ReDim Preserve arr_years(1 To i)
arr_years(i) = newYear
Else: Exit For
End If
Next oneCell

Sheets("Aanmeldingen_per_maand").Range("B2", Range("B2").End(xlToRight)) = arr_years
End Sub

Could you help me get this to work, please? Thanks!
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Assuming this line:

If Not IsError(Application.Match(oneCell.Value, arr_years, 0)) Then

causes the error, the worksheet function Match requires a range object as the second argument, not an array.

Can you elaborate on exactly what you want your code to do?
 
Upvote 0
worksheet function Match requires a range object as the second argument, not an array

That's not true JoeMo, as shown here:

Code:
Sub Test()
    Dim arr_years As Variant
    arr_years = Array(2012, 2013, 2014)
    MsgBox Application.Match(2013, arr_years)
End Sub
 
Upvote 0
That's not true JoeMo, as shown here:

Code:
Sub Test()
    Dim arr_years As Variant
    arr_years = Array(2012, 2013, 2014)
    MsgBox Application.Match(2013, arr_years)
End Sub
Thanks for that correction Andrew, I learned something new! The description of the Match function in Excel Help indicates the lookup array should be a range of cells, but your routine proves otherwise.
 
Upvote 0
I’ve found a solution that works, using the DistinctValues-formula from CPearson.com:

Code:
Private Sub cmd_try_Click()
Dim dataRange As Range
Dim oneCell As Range
Dim newYear As String
Dim arr_years() As Variant
Dim i As Integer
 
Set dataRange = Range(Sheets("Overzicht_aanmeldingen").Cells(4, 10), Sheets("Overzicht_aanmeldingen").Cells(4, 10).End(xlDown))
For Each oneCell In dataRange
i = i + 1
newYear = CStr(Year(oneCell.Value))
ReDim Preserve arr_years(1 To i)
arr_years(i) = newYear
Next oneCell
'verwijder identieke items
arr_years = DistinctValues(arr_years, True)
Thanks for the input.
 
Upvote 0
PHP:
FirstTeam = Left(Fullmatch, LinePos - 1)

The above code also gives runtime error 5. Beneath the full code. Any ideas where I went wrong here ?


PHP:
Private Sub CommandButton1_Click()

Dim Fullmatch As String, FirstTeam As String, SecondTeam As String
Dim LinePos As Integer
Dim i As Range

For Each i In Sheet1.Range("B8:B58")

Fullmatch = Sheets("matches").Range("B" & i.Row).Value
LinePos = InStr(Fullmatch, "-")

FirstTeam = Left(Fullmatch, LinePos - 1)
SecondTeam = Right(Fullmatch, Len(Fullmatch) - Len(FirstTeam) - 1)

Sheets("matches").Range("H" & i.Row).Value = FirstTeam
Sheets("matches").Range("I" & i.Row).Value = SecondTeam


Next

Sheets("matches").Range("B5:H55").RemoveDuplicates Columns:=Array(1, 2), Header:=xlYes

End Sub
 
Upvote 0
Yes that seemed to be the case. There where some empty columns which probably caused the error. Thanks for your help.
 
Upvote 0

Forum statistics

Threads
1,215,343
Messages
6,124,404
Members
449,156
Latest member
LSchleppi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top