Is there a way to set date format as dd-mmm-yyyy in ActiveX TextBox?
I'm using the code below to separate the quote but there is one error while user is putting month., i.e. 31-May-1993 but sometimes user is putting 31-may-2015.
Because of that unable to fetch the data through server...
Private Sub TextBox1_Change()
If TextBox1.TextLength = 2 Or TextBox1.TextLength = 6 Then
TextBox1.Text = TextBox1.Text + "-"
End If
End Sub
Can I suggest you take a different approach with this and do something like (Note this is untested and not final code just a few suggestions for how you could handle this):
Private Sub Textbox1_LostFocus()
Dim Da as Date
Da = CDate(Textbox1.Text)
Textbox1.Text = Format(Da, "dd-mmm-yyyy")
End Sub
This will:
Take whatever text is entered when the user clicks outside of the active box (LostFocus instead of running every key press)
Convert the value to a Date
Put back into the Textbox in the format that you require (dd-mmm-yyyy)
You could then add error handling in this so that if vba can't convert it to a date it could report to the user that they have entered an incorrect value and to correct it before proceeding.
I'm also not advocating that this is best practice but a way that the OP could get his method to work.
Related
In my excel code I want to add the date after information is entered into the form. I don't want to manually enter the date. It should just go in automatically once and should not be updated unless manually.
You can change the text to the most recently used date every time textbox is opened (or whatever trigger works best for you) like this:
Private Sub UserForm_Initialize()
Me.TextBox1.Value = Sheet1.Range("D" & Rows.Count).End(xlUp).Value
End Sub
And it comes out looking like this:
::
Additional References/Images:
Sub Rectangle1_Click()
UserForm1.Show
End Sub
This seems to be a simplistic task to get the actual date to display in the combobox versus 43466 (a number). The code that everyone seems to be using is the following with
.Text:
Private Sub ComboBox20_Change()
ComboBox20.Text = format(ComboBox20.Text, "dd/mm/yyyy")
End Sub
OR . Value
Private Sub ComboBox20_Change()
ComboBox20.Value = format(ComboBox20.Value, "dd/mm/yyyy")
End Sub
I have even tried with .text and .value. Either one keeps giving me the following error:
Compile Error:
Wrong number of arguments or invalid property assignment
Any thoughts on what I must be missing, having a brain meltdown on the most mundane and seemingly easy task.
Problem approach
The problem seems to be when you are attempting to change the value in the combobox, is there a value to begin with? If it is empty, it is taking the default version of the date.
Solution approach
Try to set the value before it goes into the Change event.
Sample code and demonstration
Private Sub ComboBox1_Change()
ComboBox1.Value = Format(ComboBox1.Value, "dd/mm/yyyy")
End Sub
Private Sub UserForm_Activate()
Dim CounterDate As Long
Dim TxtDate As String
For CounterDate = 1 To 2
TxtDate = DateAdd("d", CounterDate, Now)
ComboBox1.AddItem (TxtDate)
ComboBox1.Value = TxtDate
Next CounterDate
End Sub
Further comments
As you may see as soon as you change it, it enters again the "Change" event, you may turn off the events as soon as you enter the cycle and then turn them on again if it is causing erratic behaviour on your pc -sometimes it does-.
I have knowledge of the RANDBETWEEN() function inside the cells of Excel. This only returns one (1) value. I would like to assign this to a button to return a different value every time the button is hit.
I can create the button, I have the cell. How can I link the two?
Long story short, every time a button is clicked a cell (lets say cell a1) gets a different value between values a to b.
Try below
Private Sub CreateRandom()
'random number between 11 and 100
Debug.Print NewRandom(11, 100)
End Sub
Private Function NewRandom(ByVal FromLimit As Integer, ByVal ToLimit As Integer) As Integer
Randomize
randomNumber = Int((ToLimit - FromLimit) * Rnd) + FromLimit
NewRandom = randomNumber
End Function
Private Sub CommandButton1_Click()
'random number between 30 and 100
Sheets("Sheet2").Range("A1").Value = NewRandom(30, 100)
End Sub
MI Consol:
Sheets:
Private Sub CommandButton1_Click()
Call p
End Sub
Private Sub CommandButton3_Click()
Call save
End Sub
Private Sub CommandButton4_Click()
Call EMail
End Sub
Private Sub CommandButton5_Click()
Call iSMS
End Sub
Private Sub CommandButton6_Click()
Call continue
End Sub
Private Sub CommandButton7_Click()
Call Continue1
End Sub
If you're still using the RANDBETWEEN function and you just want it to generate a new value on command, just press F9 to force Excel to recalculate all formulas. RANDBETWEEN will then generate a new random number. (If it doesn't change value, it's just because it generated the same random number by chance. Keep pressing F9 to get new random numbers.)
Note that changing any formula on the spreadsheet will also cause the RANDBETWEEN function to generate a new value.
If you want a new random number to be generated ONLY when you command it to, then you do need to create a macro, which can be easily called through a button. I'm assuming you're not familiar with macros at all, so the following is a simple solution.
The following website explains how to create a simple button that will run a macro. (Note that this specific method only works with the ActiveX button, not the Forms button.)
https://www.tutorialspoint.com/vba/vba_excel_macros.htm
You will just need to replace the code in their example (MsgBox "Hi") with the following code:
Range("A1") = WorksheetFunction.RandBetween(a,b)
Replace a and b with the numbers you want to get a random number between. You can do this a few ways:
If you always want it between the same two numbers (e.g. 1 and 10), you can hard-code them into the macro:
Range("A1") = WorksheetFunction.RandBetween(1,10)
If you want it between two numbers recorded on the same spreadsheet (e.g. in cells B1 and B2), you can refer to the cells:
Range("A1") = WorksheetFunction.RandBetween(Range("B1"),Range("B2"))
Or you can ask for the numbers in popups:
a = InputBox("Enter a")
b = InputBox("Enter b")
Range("A1") = WorksheetFunction.RandBetween(a, b)
Note that the website misses a couple of important steps you must perform before the button will be able to used:
Close the VBA window (not actually necessary, but there's no need to leave it open)
Exit Design Mode. On the Developer tab in Excel, the Design Mode button should still be highlighted. Click it to un-highlight it. You can now click your button.
Note that if you want to change the code for the button again, you need to click Design Mode again to highlight it again, then you can double click the button to view the code again.
I am calculating a bunch of values like density and volume. I show them on a form and when the user clicks "Update", I save that number to a worksheet.
There are a lot of numbers, so I want to neatly display the rounded version without losing the better precision number.
Like
Sub SetDensity()
dim rDensity as double
rDensity = 0.78022134
me.txtDensity = Format(rDensity,"0.00") ' user sees 0.78
End Sub
When data is finally written to worksheet, I want to be able to store the full precision like:
Sub UpdateWS()
Sheets(1).Range("A:A") = me.txtDensity ' put value 0.78022134 on worksheet
End Sub
Does the textbox have an alternate field where I can store the full number without displaying it, or do I need a hidden textbox for every real number that contains the full value?
Thanks
You could use the TextBox's Tag property to store the value. Here's a simple example:
Private Sub UserForm_Activate()
Me.TextBox1.Tag = 2.38232
Me.TextBox1.Value = Format(Me.TextBox1.Tag, "0.00")
Me.TextBox2.Tag = 4.99999
Me.TextBox2.Value = Format(Me.TextBox2.Tag, "0.00")
End Sub
Private Sub CommandButton1_Click()
MsgBox Me.TextBox1.Tag * Me.TextBox2.Tag
End Sub
What you ideally do in this situation is create an instance of a userform and pass the values into it as Properties, and then when the user clicks "OK" you get the computed value back out as another Form Property. Here's a post from my site, and one from Daily Dose of Excel, that give you the general idea. You could use these concepts and modify to work with textboxes.
Hi all i will give an Excel Document for the user with some basic information as follows
Now if the user leaves a cell which is required i would like to prompt him a message saying that it is a required value with in the excel.
I have referred to some articles
But i am unable to achieve what I required so can any one help. Also I would like to know is it possible to apply Regular expression validators with in the Excel. Something like Date format should be mm/dd/yyyy and SSN should be 9 digited like that..
I tried some thing like but this didn't prompt me any error or Dialog
Private Sub Worksheet_BeforeSave(Cancel As Boolean)
If Sheet1.Range("A3:B3").Value = "" Then
Application.EnableEvents = True
MsgBox "Cannot print until required cells have been completed!"
Cancel = True
End If
End Sub
Part 1
That code has a few issues
You need a Workbook event - there is no WorkSheet_BeforeSave event
You cant test for two blank cells with Sheet1.Range("A3:B3").Value = ""
If the code is running then events are already enabled, so this line Application.EnableEvents = True is redundant
Something like this test for both A3 and B3 being non blank on the leftmost sheet
{code goes in the ThisWorkbook module}
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Application.WorksheetFunction.CountA(Sheets(1).Range("a3:b3")) <> 2 Then
MsgBox "Cannot print until required cells have been completed!"
Cancel = True
End If
End Sub
While you can use a Regex for part 2, plain data validation should work fine,ie
a) You can use an "allow" Date in Data Validation
b) You could use a Whole Number between 100,000,000 and 999,999,999 for a 9 digit number
Answer to Part 2 (Regular Expression thing)
You can write a user-defined function in VBA and use it in Excel. Here's an example that extracts parts of a string using RegEx, but with a bit of tweaking you can use it to do validation.