Type Mismatch when click on ok and cancel

ShaunJ

New Member
Joined
Mar 10, 2022
Messages
10
Office Version
  1. 2016
Platform
  1. Windows
1646890168451.png
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Welcome to the MrExcel board!

When posting vba code in the forum please post your actual code not a picture of it. We cannot copy from a picture to test.
My signature block below has more information about posting code.

You have not shown how Wafersclick is given a value but whatever it is the yellow part below can never be true since Wafersclick cannot hold two different values at the same time.

Assuming that you want the user to able to exit the whole code with Cancel or no entry at all then perhaps something like this?

VBA Code:
Sub AddLot()
  Dim Wafers As Variant
  
  Do
    Wafers = InputBox("How many? 1 to 25")
    If Wafers = "" Then Exit Sub
    If IsNumeric(Wafers) Then
      If Wafers >= 1 And Wafers <= 25 Then Exit Do
    End If
  Loop
End Sub
 
Upvote 0
Welcome to the MrExcel board!

When posting vba code in the forum please post your actual code not a picture of it. We cannot copy from a picture to test.
My signature block below has more information about posting code.

You have not shown how Wafersclick is given a value but whatever it is the yellow part below can never be true since Wafersclick cannot hold two different values at the same time.

Assuming that you want the user to able to exit the whole code with Cancel or no entry at all then perhaps something like this?

VBA Code:
Sub AddLot()
  Dim Wafers As Variant
 
  Do
    Wafers = InputBox("How many? 1 to 25")
    If Wafers = "" Then Exit Sub
    If IsNumeric(Wafers) Then
      If Wafers >= 1 And Wafers <= 25 Then Exit Do
    End If
  Loop
End Sub
Hi Peter,
Thank you for replying. Here comes another error, and it says that do without loop.

VBA Code:
Sub AddLot()

Dim Wafers As Variant

Do

Wafers = InputBox("How many Wafers will be processed:", "Inputs", "1 to 25")

If Wafers = "" Then Exit Sub

If IsNumeric(Wafers) Then

    If Wafers <= 25 And Wafers >= 1 Then Exit Do

    MsgBox "Please enter a valid number within 1 to 25"

End If

Loop

Do

End Sub
 
Upvote 0
When posting vba code in the forum, please use the available code tags. As I mentioned above my signature block below has more details. ;)
I fixed it for you this time. :)

You have an extra "Do" as the second last line in the code you posted. That "Do" is not in my code.
 
Upvote 0
Hello Shaun,

I am not sure what the box is for but the following adds the inputted value and 'LOOPS' until a valid answer is given, without looping.

VBA Code:
Sub WithoutLoop()
Dim strName As String
strName = InputBox(Prompt:="How many wafers will be produced?", Title:="Wafers", Default:="1 to 25")
    If Not IsNumeric(strName) Then
    Call WithoutLoop
    Else
    If strName < 1 Then
    Call WithoutLoop
    Else
    If strName > 25 Then
    Call WithoutLoop
    Else
    If IsNumeric(strName) Then
    Worksheets("Sheet6").Range("a1").Value = strName
    Else
    If vbCancel Then
    Exit Sub
    End If
    End If
    End If
    End If
    End If
End Sub

Jamie
 
Upvote 0
Hi Peter,
Thank you for replying. Here comes another error, and it says that do without loop.

VBA Code:
Sub AddLot()

Dim Wafers As Variant

Do

Wafers = InputBox("How many Wafers will be processed:", "Inputs", "1 to 25")

If Wafers = "" Then Exit Sub

If IsNumeric(Wafers) Then

    If Wafers <= 25 And Wafers >= 1 Then Exit Do

    MsgBox "Please enter a valid number within 1 to 25"

End If

Loop

[B][U][COLOR=rgb(226, 80, 65)]Do[/COLOR][/U][/B]

End Sub
Hello,

There is a Do after the loop - delete that. :)

Jamie
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,521
Members
449,088
Latest member
RandomExceller01

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