VBA: Invalid Procedure,Call or Argument

SAMUSER

Board Regular
Joined
Jul 27, 2012
Messages
82
Hi Everyone,

I am trying to add a simple property to the macro. If someone pastes information, there is a run-time 9 error. I want to have an "If Error" statement that alerts the user, and tells them to press ctrl+z to undo, then the procedure works fine.

However, the If Error statement is causing me problems. When I run it, I get a Runtime 5: Invalid procedure, call or argument. I have highlighted the line with the bug.

Any help would be much appreciated!

Thanks a lot!


Code:
Sub getResults()


ActiveSheet.Unprotect Password:="Syncos123!"


Sheets("selector").Activate


Dim resultDrop As Range
Set resultDrop = Sheets("Selector").Range("A12")


Dim outcomeDrop As Range
Set outcomeDrop = Sheets("Selector").Range("A24")


Dim dOutcomeSelect As String
dOutcomeSelect = Sheets("Selector").Range("A8").Value


'Sheets("Master List").Activate


Dim dOutcomeList As Range
Set dOutcomeList = Sheets("Master List").Range("C2:BD4")


Dim masterRange As Range
Set masterRange = Sheets("Master List").Range("C2:BD28")


Dim dOutcomePos As Range
Set dOutcomePos = masterRange.Find(dOutcomeSelect)


Dim resultQuant As Long


[COLOR=#b22222]If Error Then MsgBox "You cannot paste information. Press ctrl+Z to undo, and then select from the drop"[/COLOR]


resultQuant = dOutcomePos.Offset(2, 0).Value


Dim outcomeQuant As Long
outcomeQuant = dOutcomePos.Offset(2, 1).Value


Dim resultRange As Range
Set resultRange = Range(dOutcomePos.Offset(3, 0), dOutcomePos.Offset(resultQuant + 2, 0))


Dim outcomeRange As Range
Set outcomeRange = Range(dOutcomePos.Offset(3, 1), dOutcomePos.Offset(outcomeQuant + 2, 1))


ActiveWorkbook.Names.Add Name:="ValListR", RefersTo:=resultRange
 
ActiveWorkbook.Names.Add Name:="ValListO", RefersTo:=outcomeRange


With resultDrop.Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=ValListR"
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With




With outcomeDrop.Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=ValListO"
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With


resultDrop.Value = resultRange(1, 1).Value
outcomeDrop.Value = outcomeRange(1, 1).Value


MsgBox "The 'Results' and 'Outcomes' have been updated based on your selection of the 'Desired Outcome'. Please select the applicable Result and Outcome from the drop-down menu and copy it to the clipboard using the button before pasting it into GAIMS"
ActiveSheet.Protect Password:="Syncos123!", DrawingObjects:=True, Contents:=True, Scenarios:=True


End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Change The If Error Line With This:
Rich (BB code):
On Error Goto ErrorHandler

ErrorHandler:
MsgBox "You cannot paste information. Press ctrl+Z to undo, and then select from the drop"

Your Problem that the code DOESN'T Know what "Error" is; It considers it as an undifined variant.

ZAX
 
Upvote 0
ZAX- Thanks a lot! I totally forgot that I am using VBA- still sticking to Excel syntax.

I have tried your suggestion, and it works. However, the error handler comes up every time (even after the normal loop has ended). Do you know why this may be happening?

Regards,
Saim
 
Upvote 0
Typically error handlers are after exiting the sub, so that any previous code runs through to its end.

Something like this.

Code:
On Error Goto ErrorHandler
'run a bunch of code

Finish:
Exit Sub  

ErrorHandler:
MsgBox "You cannot paste information. Press ctrl+Z to undo, and then select from the drop"
 
Upvote 0
Thanks a lot, but I am struggling to incorporate this into my code. Any clues as to how I can do it? Please let me know.

Thanks!

If pasting is an issue, you might also want to consider handling the pasting issue before it becomes an issue.

Solved: Disable Cut, Copy, Paste Macro for One Column - VBA Express Forum

This thread was just updated last week with a much better code than the original author wrote, so you might want to start at the bottom and read upwards, rather than starting at the top.
 
Upvote 0
Which part are you struggling with? The error handler or the disabling of pasting?

If you're looking at disabling pasting then look at SNB's post. Copy the whole thing into the code for ThisWorkbook.

The only thing you need to change is this section:
Code:
Sub ChkSelection(ByVal Sh As Object)
    Select Case Sh.Name
    Case "Sheet1"
        ToggleCutCopyAndPaste Intersect(Selection, Sh.Columns(1)) Is Nothing
    Case "Sheet2"
        ToggleCutCopyAndPaste Not Intersect(Selection, Sh.Range("G1:G20")) Is Nothing
    Case Else
        ToggleCutCopyAndPaste True
    End Select
End Sub

Case "Sheet1" needs to change to the sheet name you want to disable pasting on. If you don't need a 2nd sheet, delete that section and you'll need to change this line....

Code:
ToggleCutCopyAndPaste Not Intersect(Selection, Sh.Range("G1:G20")) Is Nothing

Change Range("G1:G20") to the range you do not want them to paste into.

So lets say your sheet name is MyData and you don't want them to paste into Anything from A1 to F110. you would change it to look like this.

Code:
Sub ChkSelection(ByVal Sh As Object)
    Select Case Sh.Name
    Case "MyData"
        ToggleCutCopyAndPaste Intersect(Selection, Sh.Range("A1:F110")) Is Nothing
    Case Else
        ToggleCutCopyAndPaste True
    End Select
End Sub

You don't need to change anything else.

If you're still having trouble, post what you have and we'll take a look at it.
 
Upvote 0
Hey! I'm sorry- I did figure it out (it was some syntax issue). Thanks so much for your help though!

Which part are you struggling with? The error handler or the disabling of pasting?

If you're looking at disabling pasting then look at SNB's post. Copy the whole thing into the code for ThisWorkbook.

The only thing you need to change is this section:
Code:
Sub ChkSelection(ByVal Sh As Object)
    Select Case Sh.Name
    Case "Sheet1"
        ToggleCutCopyAndPaste Intersect(Selection, Sh.Columns(1)) Is Nothing
    Case "Sheet2"
        ToggleCutCopyAndPaste Not Intersect(Selection, Sh.Range("G1:G20")) Is Nothing
    Case Else
        ToggleCutCopyAndPaste True
    End Select
End Sub

Case "Sheet1" needs to change to the sheet name you want to disable pasting on. If you don't need a 2nd sheet, delete that section and you'll need to change this line....

Code:
ToggleCutCopyAndPaste Not Intersect(Selection, Sh.Range("G1:G20")) Is Nothing

Change Range("G1:G20") to the range you do not want them to paste into.

So lets say your sheet name is MyData and you don't want them to paste into Anything from A1 to F110. you would change it to look like this.

Code:
Sub ChkSelection(ByVal Sh As Object)
    Select Case Sh.Name
    Case "MyData"
        ToggleCutCopyAndPaste Intersect(Selection, Sh.Range("A1:F110")) Is Nothing
    Case Else
        ToggleCutCopyAndPaste True
    End Select
End Sub

You don't need to change anything else.

If you're still having trouble, post what you have and we'll take a look at it.
 
Upvote 0

Forum statistics

Threads
1,216,106
Messages
6,128,863
Members
449,473
Latest member
soumyahalder4

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