Can't get a macro to run if a criteria is met.

owen4512

Board Regular
Joined
Dec 10, 2014
Messages
71
Here's the Vb Code I used but I keep getting a "Argument Not Optional" message box.

I want for the macro to run if the value equals to "Iawn I Bwcio". But if the value equals to "Dyddiad Anghywir"
then I want a message box to appear and say ;
"Mae yna dyddiad anghywir wedi ei fewnbynnu! Mae angen newid hyn er mwyn cario ymlaen!"
Code:
Private Sub mewbynnuDataSioe(ByVal target As Range)
    If (Range(D5) = "Iawn I Bwcio") Then
    '
    ' Mewnbynnu_Data_Sioe Macro
    '
    ' Keyboard Shortcut: Ctrl+Shift+Q
    '
    Range("E3").Select
    ActiveCell.FormulaR1C1 = "1"
    Range("E4").Select
        ElseIf (Range(D5) = "Dyddiad Anghywir") Then
        MsgBox "Mae yna dyddiad anghywir wedi ei fewnbynnu! Mae angen newid hyn er mwyn cario ymlaen!"
        
    End With
    End If
    Exit Sub
End Sub
Thanks in advanced, Owen.
 
Last edited by a moderator:

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Welcome to MrExcel.

Try removing the red text:

Rich (BB code):
Private Sub mewbynnuDataSioe(ByVal target As Range)
 
Upvote 0
Okay I've done that but I now get another error message.

Compile error:
End With without With
 
Upvote 0
I tried that, but yet another error.

Run-time error '1004'
Method 'Range' of object '_Global' failed

When I press debug the
If (Range (D5) = "Iawn I Bwcio") then
gets highlighted yellow
 
Upvote 0
Try:
Code:
Sub mewbynnuDataSioe()

    '
    ' Mewnbynnu_Data_Sioe Macro
    '
    ' Keyboard Shortcut: Ctrl+Shift+Q
    '
    If Range("D5").Value = "Iawn I Bwcio" Then
    Range("E3").Value = "1"
    Range("E4").Select
        ElseIf Range("D5").Value = "Dyddiad Anghywir" Then
        MsgBox "Mae yna dyddiad anghywir wedi ei fewnbynnu! Mae angen newid hyn er mwyn cario ymlaen!"
       
    End If

End Sub
 
Last edited:
Upvote 0
Argument not optional again :(

As you can tell I'm not very educated in Visual basic.
This macro will be assigned to a button, not sure if that helps.
 
Upvote 0
Sorry missed the amendment to the declaration. I've updated the code.
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

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