Error 1004 on Worksheet.Match

kodiac9

New Member
Joined
Jun 27, 2011
Messages
13
I get error 1004 on this snippet of code. Plz help!

Code:
   Dim Cert2008, Cert2009, Cert2010, Cert2011, Cert2012, Cert2013, Cert2014, Cert2015, Cert2016, Cert2017, Cert2018, Cert2019, Cert2020, Cert2021, Cert2022, Cert2023, Cert2024, Cert2025, Cert2026, Cert2027, Cert2028, Cert2029, Cert2030 As Integer
   Dim certused(), Check As Variant
   Dim certcounter As Integer
certcounter=0
' ======= SKIPPED WORKING PART=========
 
 
   ' === Get data =======
    ActiveCell.Offset(0, 3).Range("A1").Select
    Check = ActiveCell.Value
    '== Check if exist =======
        lngLoc = Application.WorksheetFunction.Match(Check, certused(certcounter), 0)
    If Not IsEmpty(lngLoc) Then
    Else
          '===== Add to string ===
            ReDim Preserve certused(certcounter + 1)
         certused(certcounter) = ActiveCell.Value
         '=== Prepare for next ======
         certcounter = certcounter + 1
        Cert2008 = Cert2008 + 1
    End If
    ActiveCell.Offset(0, -3).Range("A1").Select
 
If you use WorksheetFunction, then you need an On Error .... statement; if you use the Application. versions, you just assign the result to a variant and then use
Code:
If Iserror(var) Then
syntax
 
Upvote 0

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
How do i make it add the value to the array, if it is missing then because from what i gather, wutever number the active cell has, this will make IsError always true.

Code:
   Dim Cert2008, Cert2009, Cert2010, Cert2011, Cert2012, Cert2013, Cert2014, Cert2015, Cert2016, Cert2017, Cert2018, Cert2019, Cert2020, Cert2021, Cert2022, Cert2023, Cert2024, Cert2025, Cert2026, Cert2027, Cert2028, Cert2029, Cert2030 As Integer
   Dim certused(), Check As Variant
   Dim certcounter As Integer
   Dim lngLoc As Variant
   certcounter = 0
    ReDim certused(certcounter)
         ReDim Preserve certused(certcounter)
         certused(certcounter) = 0
         '=== Prepare for next ======
         certcounter = certcounter + 1
 
   ' === Get data =======
    ActiveCell.Offset(0, 3).Range("A1").Select
    Check = ActiveCell.Value
    '== Check if exist =======
        lngLoc = Application.Match(Check, certused(certcounter), 0)
    If Not IsError(lngLoc) Then
          '===== Add to string ===
            ReDim Preserve certused(certcounter)
         certused(certcounter) = ActiveCell.Value
         '=== Prepare for next ======
         certcounter = certcounter + 1
        Cert2008 = Cert2008 + 1
        MsgBox (certused(certcounter))
    End If
    ActiveCell.Offset(0, -3).Range("A1").Select
 
Upvote 0
Ah, I misread your code - you do actually want to process where the errors occur:
Code:
   Dim Cert2008, Cert2009, Cert2010, Cert2011, Cert2012, Cert2013, Cert2014, Cert2015, Cert2016, Cert2017, Cert2018, Cert2019, Cert2020, Cert2021, Cert2022, Cert2023, Cert2024, Cert2025, Cert2026, Cert2027, Cert2028, Cert2029, Cert2030 As Integer
   Dim certused(), Check As Variant
   Dim certcounter As Integer
   Dim lngLoc As Variant
   certcounter = 0
 
   ' === Get data =======
    ActiveCell.Offset(0, 3).Range("A1").Select
    Check = ActiveCell.Value
    '== Check if exist =======
        lngLoc = Application.Match(Check, certused(certcounter), 0)
    If Not IsError(lngLoc) Then
          '===== Add to string ===
            ReDim Preserve certused(certcounter)
         certused(certcounter) = check
         '=== Prepare for next ======
         certcounter = certcounter + 1
        Cert2008 = Cert2008 + 1
        MsgBox (certused(certcounter))
    End If
    ActiveCell.Offset(0, -3).Range("A1").Select
 
Upvote 0
Now with this code i get runtime error 9 : out of range on this line

Code:
       lngLoc = Application.Match(Check, certused(certcounter), 0)

At first run, not even on duplicate
 
Last edited:
Upvote 0
Code:
   certcounter = 0
    ReDim certused(certcounter)
' more code here


     ' === Get data =======
    ActiveCell.Offset(0, 3).Range("A1").Select
    Check = ActiveCell.Value
    '== Check if exist =======
    If certcounter = 0 Then
        certused(certcounter) = Check
    Else
        lngLoc = Application.Match(Check, certused(), 0)
        If Not IsError(lngLoc) Then
              '===== Add to string ===
            certcounter = certcounter + 1
            ReDim Preserve certused(certcounter)
             certused(certcounter) = Check
             '=== Prepare for next ======
            Cert2008 = Cert2008 + 1
            MsgBox (certused(certcounter))
        End If
    End If
    ActiveCell.Offset(0, -3).Range("A1").Select
 
Upvote 0
That appears to be because the test only gets done once, due to the flow of your logic. It is inside the Select Case statement and only gets run for 2008 as far as I can see at a quick glance. (far too many Select statements in there to really see what's going on easily)
 
Upvote 0
Code:
    ' === Get data =======
    ActiveCell.Offset(0, 3).Range("A1").Select
    Check = ActiveCell.Value
    '== Check if exist =======
    lngLoc = Application.Match(Check, certused(), 0)
    If IsError(lngLoc) Then
        '===== Add to string ===
        certcounter = certcounter + 1
        ReDim Preserve certused(certcounter)
        certused(certcounter) = Check
        '=== Prepare for next ======
        Cert2008 = Cert2008 + 1
    End If
    ActiveCell.Offset(0, -3).Range("A1").Select

Dunno wut i changed in there but now it works flawlessly

Thanks a lot for your help!
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,713
Members
452,939
Latest member
WCrawford

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