Need help with input box

TsarMarkI

New Member
Joined
Aug 14, 2019
Messages
20
Good Afternoon all,

I have a VBA macro that allows for data being entered into a text box to be placed in specific areas of my sheet. However, when I click "OK" with no value entered (blank), it ends the sub. I need the sub to stop running when it finds a blank value on the sheet, but when no value is entered into the text box, I need it to be recognized as a "0" value.

I would also like the sub to pick up where the data left off should the sub be ended early (this isn't a requirement for the sheet)

Also, I need the "Cancel" button in the input box to end the sub completely. Any help would be incredibly appreciated; current sub below:

Sub Test()
'
Dim Cl As Range
Dim Res As Variant
Dim i As Long
Dim wks As Worksheet

Application.ScreenUpdating = False

For Each wks In ActiveWorkbook.Worksheets
wks.Visible = xlSheetVisible
Next wks

Sheets("Test").Select


For Each Cl In Range("F2", Range("F" & Rows.Count).End(xlUp))
For i = 1 To 2
Res = InputBox("Please enter the total quantity in " & Cl.Value)
If Cl.Value = "" Then MsgBox "No More Cycle Counts Available"
If Res = "" Then Call Hide
If Res = "" Then Exit Sub
Cl.Offset(, -2).Value = Val(Res)
If Val(Res) = Cl.Offset(, -3).Value Then Exit For
Next i
Next Cl

Application.ScreenUpdating = True

End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
It is not a textbox - it's an input box (using the correct terminology reduces confusion :))

Immediately below
Code:
Res = InputBox ...
insert this line
Code:
If Res = "" Then Res = 0
 
Last edited:
Upvote 0
Thanks Yongle! Sorry about that- totally meant to keep "InputBox" as consistent as possible. The change works great for accepting blank values as zero, but now I have to run through the entire list it's referencing in order to end the macro (I believe it's also treating the "Cancel" button as zero)
 
Upvote 0
It is always better to "figure it out" - you are more likely to remember
:beerchug:
 
Upvote 0
Nevermind, I figured it out based on where you started me. Cheers!
I am glad you figured it out, but thought you might like to see this anyway. Here is a generalized structure you can use for the InputBox that will allow you to react to the user clicking the Cancel button or clicking Enter without filling in anything as well as when the user enters actual data...
Code:
Dim Answer As String
 '....
 Answer = InputBox("Tell me something")
 If StrPtr(Answer) = 0 Then
   MsgBox "The user clicked Cancel, so we will exit the subroutine now."
   Exit Sub
 ElseIf Len(Answer) = 0 Then
   MsgBox "The user clicked OK without entering anything in the InputBox!"
 Else
   MsgBox "The user entered the following..." & vbLf & vbLf & Answer
 End If
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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