recorded macro throwing 1004 error

ursua

New Member
Joined
Jan 9, 2020
Messages
17
Office Version
  1. 365
Platform
  1. Windows
Hi,
So i'm kinda new to macros so i am recording them to have them done and to learn something.
I have recorded a macro that puts a dropdown (this one is connected to the input of a cell in adjacent column).
The macro looks like this:
VBA Code:
Sub Macro2()
'
' Macro2 Macro
'

'
    Range("AJ2").Select
    Range(Selection, Selection.End(xlDown)).Select
    With Selection.Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:= _
        "=OFFSET(UseCases!$A$1,MATCH(AI2,UseCases!$A:$A,0)-1,1,COUNTIF(UseCases!$A:$A,AI2),1)"
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With
End Sub

So in the column AJ it is making a dropdown (the source for it is in UseCases sheet) depending on an adjacent cell in column AI.
When trying to run this macro it throws run-time error '1004': Aplication-defined or object defined error here:

VBA Code:
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:= _
        "=OFFSET(UseCases!$A$1,MATCH(AI2,UseCases!$A:$A,0)-1,1,COUNTIF(UseCases!$A:$A,AI2),1)"


Can you please advise how to correct it?
Thanks,
ursua
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
I don't have your data so I can only guess.
The problem is with the range definition formula:
option 1: this COUNTIF(UseCases!$A:$A,AI2) is 0
option 2: this MATCH(AI2,UseCases!$A:$A,0) is 0
... or both :)
 
Upvote 0
I think that the problem is that you have to add the sheets name in the formula like so

"=OFFSET(UseCases!$A$1,MATCH(Sheet1!AI2,UseCases!$A:$A,0)-1,1,COUNTIF(UseCases!$A:$A,Sheet1!AI2),1)"

I have the following code and it works ok:

VBA Code:
Sub Macro1()
'
' Macro1 Macro
'

'
    Range("B1").Select
    Range(Selection, Selection.End(xlDown)).Select
    With Selection.Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:="=OFFSET(UseCases!$B$1, MATCH(Sheet1!$A1, UseCases!$A$1:$A$8, 0) - 1, , COUNTIF(UseCases!$A$1:$A$8, Sheet1!$A1))"
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With
End Sub
 
Upvote 0
so what i figured out is when i am starting macro in Sheet1 then everything is ok, when i am starting it in another sheet it doesn't know it.
i tried to change it
VBA Code:
 Worksheets("Sheet1").Range("B1").Select
or
VBA Code:
 Range("Sheet1!B1").Select
but that did not help.
i guess i just start my macro in proper sheet.
thanks.
 
Upvote 0

Forum statistics

Threads
1,215,076
Messages
6,122,987
Members
449,093
Latest member
Mr Hughes

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