Basically I need 2 inputbox. First inputbox will ask user to input a value and then prompt the 2nd inputbox after user click ok. The default value in the 2nd inputbox will be the value one cell to the right of the value in the first inputbox. This is what I want to achieve but the problem with inputbox is that the Cancel button which throw me error message or won't exit sub by any means.
So are there any other similar approaches in achieving my goal? Thanks a lot in advance!
InputBox(...) returns an empty string "" whenever the cancel button is pressed.
Knowing this, you could check to see if the input is "" for each input. If at any point along the way you encounter a cancel, you don't go any further.
This is demonstrated below using nested if statements.
Sub Macro1()
Dim x As String
Dim y As String
Dim yDefault As String
x = InputBox("Prompt One", "TITLE ONE")
If (x <> "") Then
yDefault = GetDefaultValue(x)
y = InputBox("Prompt Two", "TITLE TWO", yDefault)
If (y <> "") Then
MsgBox "X: " & x & vbCrLf & "Y: " & y, vbOKOnly, "RESULTS"
End If
End If
End Sub
Function GetDefaultValue(x As String) As String
' Your custom logic to grab the default value (whatever cell) goes here
GetDefaultValue = "DEFAULT(" & x & ")"
End Function
Since it is the values that you want, don't use the InputBox. Use the Application.Inputbox. If you press the magic key F1 in Excel and you type Application.Inputbox then you will see some magic text appear out of nowhere ;)
Here is an example usage.
Ret = Application.InputBox(prompt := "Please enter a value", type := 1)
Notice the type 1.
I can give you an exact answer but I am sure this will get you started ;) If not then post back :)
Related
I am creating a userform with approximately 75 text boxes. I would like to create a common function to test the validity of the data for all text boxes by passing the allowable characters to the function along with the string I wish to validate.
For some boxes, I want to check only for numerics (like a phone number with (), -, and spaces stripped prior to calling the function).
For boxes such as last name, I want to test for upper and lower case alpha as well as allowing embedded spaces and a hyphen.
For other boxes that allow other free form text, I want to restrict input to selected character such as letters, numbers, hyphens, left and right parens "()", etc.
I cannot get the syntax right so that the "Is...Like" works correctly. I get "Run time error 5"..."Invalid procedure call or argument".
Here is my function:
Function IsValidString(strValue As String, strAllowed As String) As Boolean
Dim intPos As Integer
Dim strTemp As String
strTemp = """" & strAllowed & """"
MsgBox "StrValue = " & strValue & vbCrLf & vbCrLf & "StrAllowed = " & strAllowed & vbCrLf & vbCrLf & "strTemp = " & strTemp
For intPos = 1 To Len(strValue)
If Not (Mid(strValue, i, 1) Like strAllowed) Then IsValidString = False
Next intPos
IsValidString = True
End Function
To call the function, I use:
If Not IsValidString(strTemp, "[A-Za-z0-9]/#-") Then MsgBox "You Lose"
The error occurs within the Function code. It does not matter if I test for strAllowed or strTemp; I get the same error.
Thanks for looking at it.
Based upon the answer from #Comintern, I overlooked that I was checking the value of "i" instead of "intPos". Stupid mistake from a guy who is tired. Thanks for all the comments; it helped me solve the issue.
Context: I have 10 text boxes (ID1 To ID10) in a user form. The userform will also have a clear button, which will allow the user to clear all the values previously entered in the text box. For that I have inserted the below mentioned command in the Clear Button.
I have multiple commands with the same nomenclature except the number which varies with every text box. I wish to enter one command which will change number and execute all the commands.
Simple example given below: I wish to only put one command instead of the folloing:
Private Sub btnClear_Click()
'Empty ID1
ID1.Value = ""
'Empty ID2
ID2.Value = ""
'Empty ID3
ID3.Value = ""
.
.
. and so on till
.
'Empty ID10
ID10.Value = ""
End Sub
I know there is a solution to this, but since I am new cannot find on google using the correct key words. Sorry if this already exists. Any help will be appreciated. Thank you.
If these are TextBoxes on a UserForm, you will be able to do the following from a macro in the UserForm's code module:
Private Sub btnClear_Click()
Dim i As Long
For i = 1 To 10
Me.Controls("ID" & i).Value = ""
Next
End Sub
It's not possible to dynamically generate a variable and/or object name but, because the Controls collection can be indexed by the "name" of the control, it gives an alternative way of getting at the objects.
Adding in code for "option boxes" (OptionButtons? `CheckBoxes?) mentioned in comments, and changing the TextBox names to end with a "C":
Private Sub btnClear_Click()
Dim i As Long
For i = 1 To 10
Me.Controls("ID" & i & "C").Value = ""
Me.Controls("OB" & i).Value = False
Next
End Sub
Do you mean something like this?
For i=1 to 4
str="ID" & i & ".Value = ''"
Evaluate(str)
Next
Ok so I have a hyperlink function in a spread sheet cell of the form:
=HYPERLINK(JJmp(I1030), I1030)
With the function JJmp():
Function JJmp(x) As String
dim iint as variant
iint = x
If IsNull(iint) Then GoTo out:
If Left(iint, 1) <> "_" Then GoTo out:
pat1 = """C:\Program Files (x86)\Adobe\Acrobat 10.0\Acrobat\Acrobat.exe"""
'pat1 = """C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe""" (this is the default, and I don't want to change the reg).
pat3 = "D:\__Numerical_Files_by_Page\" & iint & ".pdf"
pat3 = """" & pat3 & """"
Shell pat1 & " " & pat3, vbNormalFocus
JJmp = x
out:
End Function
For whatever reason the hyperlink based on this function has some very odd behavior. As stated in the title dragging the fill handle doesn't just fill the cells but also opens adobe for each one. Secondly, the hyperlink is super sensitive, I don't have to even click on it--just hovering over it will trigger acrobat 10 to open.
In essence its a very unstable hyperlink in that it is nearly self triggering. If I use the function directly I get an entry that requires some return key gymnastics to open acrobat.
I would just like this to respond like a normal hyperlink. TIA
This behaviour is because everytime you hover over the link or you drag it to a new field, excel tries to resolve the link before you click it meaning it executes your function. How else would it be possible for excel to keep your link up to date and provide it before you click it?
-> Your Function is not executed at the time to click at it as excel thinks, your function returns a link, which it then can follow.
You could try something like this:
herber hype2macro.
then don't use Shell in your UDF. What you want is more like this
Function JJmp(x) As String
If Not IsNull(x) And x Like "_*" Then _
JJmp = """C:\Program Files (x86)\Adobe\Acrobat 10.0\Acrobat\Acrobat.exe"" " & _
"""D:\__Numerical_Files_by_Page\" & x & ".pdf"""
End Function
I am working on an excel vba project and have run into an odd issue. I am working inside of a button click sub and want to display the current note as the default while they update the note. Unfortunately, all empty cells show up with a default value of "0". The msgbox just before is for debugging purposes and it shows "" (nothing). Any ideas?
MsgBox Sheet4.Range("E" & stallToAdd + 1)
inputFromUser = Application.InputBox("Update note: ", "Update Note", Sheet4.Range("E" & stallToAdd + 1))
Edit: inputFromUser is a variant, stallToAdd is an integer.
Just convert it to a string like this:
inputFromUser = Application.InputBox("Update note: ", "Update Note", _
CStr(Sheet4.Range("E" & stallToAdd + 1)))
I've got an interesting problem - I'm trying to write VBA code that will take the contents of a cell and put it into a msgbox. The problem is that I can't figure how to put carriage returns into this string.
My code looks something like this.
Dim myMsg as string
myMsg = Worksheets("Data").cells(1,1)
msgBox myMsg
so, if cell A1 contains...
THIS IS MY NEXT AND NOW THIS IS A NEW LINE
... how can I force a carriage return into the middle of that line?
I never used VBA, but a quick search online gave me this:
"some string" & vbCrLf & "next line"
and
"some string " & chr(13) & "next line"
To insert a new line is a message box, use vbCrLF.
So, let's say you wanted to put a new line every time there is a back slash. You would use:
Msgbox Replace(ActiveCell.Value, "\", vbCrLF)
To fit within the bounds of your sample code, use:
Dim myMsg as string
Dim newLineCharacter as string
newLineCharacter = "\" ' Replace with your delimiter
myMsg = Replace(Worksheets("Data").cells(1,1), newLineCharacter, vbCrLf)
msgBox myMsg
This depends what your goal is. If you want to pre-formatting text in Excel prior to displaying on message box, wrap you lines using <Alt+Enter> in Excel, then you VBA will just look like this:
MsgBox Worksheets("Data").cells(1,1).value
If you intend to auto-wrap, write a function to cut the string and supply the length.
MsgBox WrapText(Worksheets("Data").cells(1,1).value, 20)