So I am trying to make sure that all TextBoxes are filled out. However, I only want this script to check a certain page on a MultiPage page on an Excel UserForm. Below is the code I'm using but it is checking every TextBox within the user form rather than the ones on the specific page. Also is a picture to show you. I have circled the Page named "Box" which is also Page 3 which I want to reference when validating the TextBoxes and the TextBoxes of which I would like to validate.
Snapshot of UserForm
Private Sub CommandButton2_Click()
Dim Ctrl As Control
Dim Answer1 As VbMsgBoxResult
' Checks to make sure data has been entered into all fields on the UserForm Page
For Each Ctrl In Me.Controls
If TypeOf Ctrl Is MSForms.TextBox Then
If Ctrl.Value = vbNullString Then
MsgBox "All fields must be completed before the information can be updated.", vbOkay + vbExclamation, "Missing Information"
Exit Sub
End If
End If
Next
' Displays box with Yes or No asking if they are sure they want to save and close document
Answer1 = MsgBox("Doing this will overwrite the previous data that was entered. Are you sure you want to do this?", vbYesNo + vbExclamation, "Update Cabinet Information")
' Writes the new data entered into the workbook
If Answer1 = vbYes Then
Sheet5.Range("B1").Value = TextBox1.Value
If IsDate(TextBox2.Value) Then
Sheet5.Range("B2").Value = Format(TextBox2.Value, "mm/dd/yyyy")
Else
MsgBox "One or more fields have an incorrect date format. Dates must be entered in this formay mm/dd/yyyy."
Exit Sub
End If
Sheet5.Range("B3").Value = TextBox3.Value
Sheet5.Range("G1").Value = TextBox4.Value
If IsDate(TextBox5.Value) Then
Sheet5.Range("G2").Value = Format(TextBox5.Value, "mm/dd/yyyy")
Else
MsgBox "One or more fields have an incorrect date format. Dates must be entered in this formay mm/dd/yyyy."
Exit Sub
End If
Sheet5.Range("G3").Value = TextBox6.Value
Sheet5.Range("J1").Value = TextBox7.Value
If IsDate(TextBox8.Value) Then
Sheet5.Range("J2").Value = Format(TextBox8.Value, "mm/dd/yyyy")
Else
MsgBox "One or more fields have an incorrect date format. Dates must be entered in this formay mm/dd/yyyy."
Exit Sub
End If
Sheet5.Range("J3").Value = TextBox9.Value
Sheet5.Range("M1").Value = TextBox10.Value
If IsDate(TextBox11.Value) Then
Sheet5.Range("M2").Value = Format(TextBox11.Value, "mm/dd/yyyy")
Else
MsgBox "One or more fields have an incorrect date format. Dates must be entered in this formay mm/dd/yyyy."
Exit Sub
End If
Sheet5.Range("M3").Value = TextBox12.Value
Sheet5.Range("P1").Value = TextBox13.Value
If IsDate(TextBox14.Value) Then
Sheet5.Range("P2").Value = Format(TextBox14.Value, "mm/dd/yyyy")
Else
MsgBox "One or more fields have an incorrect date format. Dates must be entered in this formay mm/dd/yyyy."
Exit Sub
End If
Sheet5.Range("P3").Value = TextBox15.Value
Sheet5.Range("S1").Value = TextBox16.Value
If IsDate(TextBox17.Value) Then
Sheet5.Range("S2").Value = Format(TextBox17.Value, "mm/dd/yyyy")
Else
MsgBox "One or more fields have an incorrect date format. Dates must be entered in this formay mm/dd/yyyy."
Exit Sub
End If
Sheet5.Range("S3").Value = TextBox18.Value
End If
End Sub
You'll need to refer to the Pages collection of the MultiPage object. Therefore, let's say that the multipage control is named MultiPage1, you'll need to amend the line as follows...
For Each Ctrl In Me.MultiPage1.Pages(2).Controls
Note that the index for the Pages collection is 0-based.
Related
ive been learning VBA and Excel in the past 2 weeks by my own during my free time, but sometimes we need some help, and currently ive no one to help besides the internet. So ive developed a Form(Sheet1) in a sheet using shapes and excel cells so the user could perform operations like insert,update, new register to the other sheet(Data) which is my Data Sheet or DataTable more specifically. But im struggling to get the update button to work. i could definitely use some help.
Heres my code:
Public Upda As String
Sub Search()
'
' Search Macro
'
Dim Sheet As String, ans
On Error GoTo Erro:
Data.Activate
Sheet = Data.Name
ans = InputBox("Write down the ID", "Search")
If ans = "" Then
Sheet1.Activate
Exit Sub
End If
Dim C
With Worksheets(Data).Range("A:A")
Set C = .Find(ans, LookIn:=xlValues, lookat:=xlWhole)
If Not C Is Nothing Then
C.Activate
Sheet1.Cells(17, 9).Value = C.Value ' Id
Sheet1.Cells(9, 4).Value = C.Offset(0, 1).Value ' Name
' here goes the other fields to be inserted
Sheet1.Activate
Upda = Sheet1.Cells(17, 9).Text
Else
Sheet1.Activate
MsgBox "Insert a valid ID", vbCritical, "Search"
End If
End With
Exit Sub
Erro:
MsgBox "Something went wrong, contact the Adm!", vbCritical, "Erro"
End Sub
'Update macro need to get a fix
Sub Update()
'update macro
Dim Sheet As String
On Error GoTo Erro
If IsEmpty(Range("I17")) Or IsEmpty(Range("D9")) Then ' there are more fields to validate
MsgBox "All the fields must have a value", vbExclamation, "Upda"
If Upda = "" Then
MsgBox "Please retry the search", vbExclamation, "Update"
Exit Sub
End If
Dim C
'
Data.Activate
Sheet = Data.Name
With Worksheets(Sheet).Range("A:A")
Set C = .Find(Upda, LookIn:=xlValues, lookat:=xlWhole)
If Not C Is Nothing Then
C.Activate
ActiveCell.Value = Sheet1.Cells(17, 9).Text ' ID
ActiveCell.Offset(0, 1).Value = Sheet1.Cells(9, 4).Text ' Name
'Update the table with the contents of the form1
Sheet1.Activate
Range("I6:J6").ClearContents
' remaining code to clear the contents of the form sheet1
Upda = ""
'Call clear
Else
MsgBox "ID number not found", vcCritical, "Update"
End If
End With
Exit Sub
Erro:
MsgBox "Something went wrong, contact the Adm!", vbCritical, "ERRO"
End Sub
Sub clear()
'
' clear Macro
'
Range("I17").ClearContents
' remaining code to cleear the contents of the form sheet1
Upda = ""
End Sub
Each one of those macros are associated with a Button(Shape), evrything is working besides the Update one.
Im getting the follow error which makes no sense to me
PS:if u need more information please let me know
You are missing the End if statement for the first If in the below block of code:
If IsEmpty(Range("I17")) Or IsEmpty(Range("D9")) Then ' there are more fields to validate
MsgBox "All the fields must have a value", vbExclamation, "Upda"
End if 'Missing If in the original code
If Upda = "" Then
MsgBox "Please retry the search", vbExclamation, "Update"
Exit Sub
End If
This may or may not be possible but i have two textboxes (textBox20 and Textbox21... yes, i will change their names soon) and after i click a button, a third textbox displays the difference between both dates on days format. (All good so far.)
What i want is to be able to insert inside the database or... in another textbox and send it later to the same database, the day name which corresponds to each of both (textbox20 and textbox21), for example, if the day is: 20/02/2020, in the data base, it should show: thursday, 20/02/2020 or thursday, 20 of feb of 2020. If i do this in the same box, i canĀ“t use the datediff function, and its a mess... I want something simple to transfer to the database, so later i can search for it and display it on another textbox later on... (im doing a little database)
Private Sub TextBox20_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'check date format
With TextBox20
If IsDate(TextBox20.Text) Then 'Format as desired.
TextBox20.Text = Format(TextBox20.Text, "dd/mm/yyyy")
Else
TextBox20.Text = "" 'Clear the TextBox
MsgBox "Please use the following format: DD/MM/YYYY."
Cancel = True
Exit Sub
End If
End With
End Sub
Private Sub TextBox21_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'check date format
With TextBox21
If IsDate(TextBox21.Text) Then 'Format as desired.
TextBox21.Text = Format(TextBox21.Text, "dd/mm/yyyy")
Else
TextBox21.Text = "" 'Clear the TextBox
MsgBox "Please use the following format: DD/MM/YYYY."
Cancel = True
Exit Sub
End If
End With
End Sub
Private Sub cmdCal_Click()
TextBox22.Value = DateDiff("d", (TextBox20.Text), (TextBox21.Text))
End Sub
I have a userform with several commandbuttons on it with unique captions on each button. I intend to use the caption as a variable within code in a module.
The 1st button (commandbutton13) clicked works fine. But when I click the 2nd button (commandbutton14), it still maintains the caption from the 1st button (commandbutton13) as the variable.
Private Sub CommandButton13_Click() 'PRIC Button userform
HoldType = UF_Dashboard.CommandButton13.Caption 'caption is PRIC
HoldText = UF_Dashboard.TextBox5.Value
sCell = UF_Dashboard.TextBox4.Value
'Check for sales order and hold text
If TextBox4.Text = "" Or TextBox5.Text = "" Then
MsgBox ("You must enter a Sales Order and Hold Text to continue.")
Exit Sub
End If
Hold
End Sub
Private Sub CommandButton14_Click() 'CUST Button on userform
HoldType = UF_Dashboard.CommandButton14.Caption 'caption is CUST
HoldText = UF_Dashboard.TextBox5.Value
sCell = UF_Dashboard.TextBox4.Value
'Check for sales order and hold text
If TextBox4.Text = "" Or TextBox5.Text = "" Then
MsgBox ("You must enter a Sales Order and Hold Text to continue.")
Exit Sub
End If
Hold
End Sub
Within the module I have the following:
Public HoldType as String
I need help understanding why HoldType is showing as PRIC after clicking commandbutton14. Even in developer mode I cannot select the line so I assume it's locking in on the 1st definition of HoldType in commandbutton13 code.
I am new to VBA and I want to test a basic If and Then Statement. I want to create a message to say Yes or No based on the data in cell A1 in excel named score. I had tried many things and sometimes just all the messages pop up so wondering how to write this properly?
Here is the actual statement:
Sub IfTest()
Dim Score As Integer
Dim Msgbox As String
Score = Cells(1, 1).Value
If Score = 1 Then
Msgbox = "Yes"
Else
Msgbox = "no"
End If
End Sub
Welcome to VBA!
MsgBox "This is my message", , "This is my Title"
Between the commas, you insert button styles.
Button styles and Title are optional.
So for your example, remove the "="'s and instead do:
If Score = 1 Then
Msgbox "Yes"
Else
Msgbox "no"
End If
Check out the MS link: MsgBox Function
* **EDIT per comintern's comment - remove this line !!!!!!
Dim Msgbox As String
I have a fully functioning userform that transfers values to a worksheet when hitting a 'Submit' button. This has worked fine when data validation was applied to all textboxes (a message box appears asking for a value to be entered), and with no data validation on the list box.
However, since adding in data validation for the list box (see code below), the 'Submit' button transfers only the pre-selected value in the form builder, rather than the selection. E.g. if nothing is selected by default, then I make a selection, it transfers an empty cell. If 'Alex' is selected by default and I select 'Hannah' once the form is open, it will transfer 'Alex'.
Does anyone have any ideas as to why the selected item in the list box isn't being picked up? All other values transfer fine (i have about 10 other textboxes with no issues). Is this an issue with the data validation?
My code looks like this:
Private Sub cbSubmit_Click()
'Name Selection Data Validation
If Not IsAnythingSelected(lbName) Then
MsgBox "Please select your name"
Exit Sub
End If
Unload Me
'Begin Transfer Information and Change Workbook
Dim nwb As Workbook
Set nwb = Workbooks.Open("G:\Test Dataset.xlsx")
'Determine emptyRow
Dim emptyRow As Long
emptyRow = WorksheetFunction.CountA(nwb.Sheets("daily_tracking_dataset").Range("A:A")) + 1
'Transfer Information
With nwb.Sheets("daily_tracking_dataset")
'Date
.Cells(emptyRow, 1).Value = CDate(txtDate.Text)
'Name
.Cells(emptyRow, 2).Value = lbName.Value
'Reference Checkss
.Cells(emptyRow, 3).Value = txtROT.Value
.Cells(emptyRow, 4).Value = txtROP.Value
End With
ActiveWorkbook.Save
ActiveWindow.Close
UserForm1.Hide
ActiveWindow.Close
End Sub
With the following function to determine if there is an item in the list box is selected:
'Function to loop through Name list box and find a selection
Function IsAnythingSelected(lbName As Control) As Boolean
Dim i As Long
Dim selected As Boolean
selected = False
For i = 1 To lbName.ListCount
If lbName.selected(i) Then
selected = True
Exit For
End If
Next i
IsAnythingSelected = selected
End Function
End If
ListBox indexing begins in 0 not 1. So use this instead in your function:
For i = 0 To lbName.ListCount - 1
If lbName.selected(i) Then
selected = True
Exit For
End If
Next I
Edit1:
Try replacing this line:
.Cells(emptyRow, 2).Value = lbName.Value
with this:
.Cells(emptyRow, 2).Value = lbName.List(lbname.ListIndex)