So I am doing some coding for the payroll application. When I run the macro and enter EmployeeNumber in textbox1, I get sent to debugging and third row of code "c=application.worksheet...." is highlighted in yellow. Basically, when I enter employee number in textbox 1, the subsequent information of that employee should get populated by itself. Can someone please tell me what I am doing wrong in the code below?
Private Sub CommandButton1_Click()
Me.TextBox2.Enabled = True
Me.TextBox3.Enabled = True
Me.TextBox4.Enabled = True
Me.TextBox5.Enabled = True
Me.TextBox6.Enabled = True
Me.CommandButton2.Visible = True
Me.CommandButton1.Visible = False
End Sub
Private Sub CommandButton2_Click()
m = MsgBox("Do You Want To Update Employee Information?", vbQuestion + vbYesNo, "Confirm Update")
If m = vbNo Then Exit Sub
EmployeeNumber = Val(Me.TextBox1.Value)
c = Application.WorksheetFunction.CountIf(MasterData.Range("A:A"), EmployeeNumber)
If c = 0 Then Exit Sub
r = Application.WorksheetFunction.Match(EmployeeNumber, MasterData.Range("A:A"), 0)
MasterData.Range("B" & r).Value = Me.TextBox2.Value
MasterData.Range("C" & r).Value = Me.TextBox3.Value
MasterData.Range("D" & r).Value = Me.TextBox4.Value
MasterData.Range("E" & r).Value = Me.TextBox5.Value
MasterData.Range("F" & r).Value = Me.TextBox6.Value
Me.TextBox2.Enabled = False
Me.TextBox3.Enabled = False
Me.TextBox4.Enabled = False
Me.TextBox5.Enabled = False
Me.TextBox6.Enabled = False
Me.CommandButton2.Visible = False
Me.CommandButton1.Visible = True
End Sub
Private Sub CommandButton3_Click()
Unload.Me
End Sub
Private Sub Label1_Click()
End Sub
Private Sub Label2_Click()
End Sub
Private Sub Label5_Click()
End Sub
Private Sub TextBox1_Change()
Dim wks As Worksheet
Set wks = Worksheets("MasterData")
EmployeeNumber = Val(Me.TextBox1.Value)
c = Application.WorksheetFunction.CountIf(MasterData.Range("A:A"), EmployeeNumber)
If c = 0 Then
Me.TextBox2.Value = ""
Me.TextBox3.Value = ""
Me.TextBox4.Value = ""
Me.TextBox5.Value = ""
Me.TextBox6.Value = ""
Exit Sub
End If
r = Application.WorksheetFunction.Match(EmployeeNumber, MasterData.Range("A:A"), 0)
Me.TextBox2.Value = MasterData.Range("B" & r).Value
Me.TextBox3.Value = MasterData.Range("C" & r).Value
Me.TextBox4.Value = MasterData.Range("D" & r).Value
Me.TextBox5.Value = MasterData.Range("E" & r).Value
Me.TextBox6.Value = MasterData.Range("F" & r).Value
End Sub
Private Sub UserForm_Click()
End Sub
This must not compile - fix that first:
Syntax error in
r = Application.worksheerfunction.Match
Fixed:
r = Application.WorksheetFunction.Match
EDIT: Fix more syntax errors
Replace MasaterData with MasterData
MasaterData.Range("B" & r).Value = Me.TextBox2.Value
MasaterData.Range("C" & r).Value = Me.TextBox3.Value
MasaterData.Range("D" & r).Value = Me.TextBox4.Value
MasaterData.Range("E" & r).Value = Me.TextBox5.Value
MasaterData.Range("F" & r).Value = Me.TextBox6.Value
You can do simple troubleshooting yourself to find the problem by simplifying your code to get one item to work - then expanding the code that works.
The simplest way to troubleshoot VBA problems yourself is to add MSGBOX or DEBUG.PRINT statements to your code to trace what's happening
Currently the issue you have is that you have two locations that handle your text box - the one that's causing you a problem right now is in TextBox_Change - that's going to fire once for every character you type in.
You either want to remove that completely or move it to the TextBox AfterUpdate event - and then decide if you want to remove the duplicate code in the Command Click event
Well Apparently, the userform wasn't enabled "True/False" from properties. That was the reason, I couldn't type anything in textbox 1. I'd like to thanks DbMitch for teaching me the step by step debug approach. The form does its intended function now.
Related
So I've got a userform for entering and looking up customer information, Everythign seems to currently work, except for the "Update" Button. When utilizing the update button, its changing my city and state values to my zipcode value, aswell as adjusting whatever else was changed. City and state should not be changing to my zipcode value. city and state values are populated utilizing a vlookup within a zipcodedata tab.
Below is the code for my update button:
Private Sub UpdateButton_Click()
If Me.CustID.Value = "" Then
MsgBox "Select customer record to update!"
Exit Sub
End If
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Customer Database")
Dim Selected_Row As Long
Selected_Row = Application.WorksheetFunction.Match(CLng(Me.CustID.Value), ThisWorkbook.Worksheets("Customer Database").Range("A:A"), 0)
'Validations-----------------------------------------------------
If Me.CustomerName.Value = "" Then
MsgBox "Plese enter the customers name!", vbCritical
Exit Sub
End If
'-------
If Me.CustomerBusiness.Value = "" Then
MsgBox "Plese enter the customers business!", vbCritical
Exit Sub
End If
'-------
If Me.CustomerAddress.Value = "" Then
MsgBox "Plese enter the customers address!", vbCritical
Exit Sub
End If
'-------
If Me.CustomerPhone.Value = "" Then
MsgBox "Plese enter the customers phone number!", vbCritical
Exit Sub
End If
'----------------------------------------------------------------
sh.Range("B" & Selected_Row).Value = Me.CustomerName.Value
sh.Range("C" & Selected_Row).Value = Me.CustomerBusiness.Value
sh.Range("D" & Selected_Row).Value = Me.CustomerAddress.Value
sh.Range("E" & Selected_Row).Value = Me.City.Value
sh.Range("F" & Selected_Row).Value = Me.State.Value
sh.Range("G" & Selected_Row).Value = Me.Zipcode.Value
sh.Range("H" & Selected_Row).Value = Me.CustomerPhone.Value
sh.Range("I" & Selected_Row).Value = Me.CustomerEmail.Value
sh.Range("J" & Selected_Row).Value = Me.MachineSerial1.Value
sh.Range("K" & Selected_Row).Value = Me.MachineSerial2.Value
sh.Range("L" & Selected_Row).Value = Me.MachineSerial3.Value
'----------------------------------------------------------------
Me.CustomerName.Value = ""
Me.LCaseName.Value = ""
Me.CustomerBusiness.Value = ""
Me.CustomerAddress.Value = ""
Me.Zipcode.Value = ""
Me.City.Value = ""
Me.State.Value = ""
Me.CustomerPhone.Value = ""
Me.CustomerEmail.Value = ""
Me.MachineSerial1.Value = ""
Me.MachineSerial2.Value = ""
Me.MachineSerial3.Value = ""
Me.CustID.Value = ""
'----------------------------------------------------------------
Call Refresh
End Sub
Here is the code for populating the City and State text boxes:
Private Sub Zipcode_Change()
Dim rng As Range
Dim a As Integer
On Error Resume Next
a = 0
Set rng = Worksheets("ZipCodeData").Range("ZipCodes")
Lastrow = Worksheets("ZipCodeData").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To Lastrow
If Me.Zipcode = Worksheets("ZipCodeData").Cells(i, 1).Value Then
a = a + 1
End If
Next
If a >= 1 Then
Me.City.Value = Application.WorksheetFunction.VLookup(Zipcode.Value, rng, 2, 0)
Me.State.Value = Application.WorksheetFunction.VLookup(Zipcode.Value, rng, 3, 0)
End If
If a = 0 Then
Me.City.Value = ""
Me.State.Value = ""
End If
End Sub
I've tried commenting out my "Zipcode_Change" sub, as I thought perhaps it was having issues interpreting the vlookup values when utilizing the update feature. I have a "add" button (which is identical to the update button, except the Selected_Row is a Last_Row. The "Add" button functions and works as intended, so I recopied my "Add" button code and readjusted it for the update button, and the issue persists.
I am creating a yard management system to keep track of parking spots in a lot.
The userform allows the user to click on a spot in the map (this would be the command button) that is open and then click update which will pull up another userform to fill out information about the truck going to that spot.
Once a user fills in the second userform, the spot changes from green to red to show that it is filled. Right now, there is no way to edit data entered for a spot.
For example, I might need to change the truck status from empty to full.
I think the best way to do this would be
if a spot is red (full and has information entered in the userform), then it will automatically pull up the second userform with the data already entered showing, so if something needs to be changed they can just type in the textbox and press update again.
if the spot is empty, it should pull up the first userform asking to update or clear the spot.
I am not sure if this is possible or the most efficient way to accomplish my goal of editing the already entered information, without having to fill out the entire userform again.
Private Sub cmdCancel_Click()
Unload Me
End Sub
Private Sub cmdClear_Click()
ActiveCell.ClearContents
ActiveCell.Interior.Color = vbGreen
Unload Me
End Sub
Private Sub cmdUpdate_Click()
UpdateInfo.Show
Unload Me
End Sub
Private Sub CommandButton1_Click()
UpdateInfo.Show
Unload Me
End Sub
Private Sub UserForm_Initialize()
lblInfo = ActiveCell.Value
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Set rngBDock = Range("BX7:CO7")
Set rngBulk = Range("BZ21:CG21")
Set rngTransT = Range("BF17:BY17")
Set rngTransT1 = Range("BG21:BY21")
Set rngTDock = Range("BL7:BW7")
Set rngEDock = Range("CP7:CT7")
Set rngNDock = Range("CU7:DC7")
Set rngFence = Range("CQ13:CV13")
Set rngNSide = Range("CW13:DB13")
Set rngGEO = Range("BG28:DD28")
Set rngNight = Range("CH21:DD21")
Set rngNewT = Range("DK31:DK65")
Set rngNewTl = Range("DI31:DI65")
Set rngOff = Range("BN40:CL40")
Set rngOffl = Range("BN42:CL42")
Set rng = Union(rngBDock, rngBulk, rngTransT, rngTransT1, rngTDock, rngEDock, rngNDock, rngFence, rngNSide, rngGEO, rngNight, rngNewT, rngNewTl, rngOff, rngOffl)
If Not Intersect(Target, rng) Is Nothing Then
CellInfo.Show
'ActiveCell.Value = cellFill
If Not IsEmpty(ActiveCell.Value) Then
Call RealTimeTracker
End If
End If
Private Sub cmdOkUpdate_Click()
Dim i As Integer, j As Integer
For i = 0 To lbxOption.ListCount - 1
If lbxOption.Selected(i) Then j = j + 1
Next i
If j = 0 Then
MsgBox "Please select an option. ", , "Warning"
Unload Me
UpdateInfo.Show
ElseIf j = 1 Then
NoFill = False
End If
strBOL = txtBOL.Value
strID = txtID.Value
details = txtDet.Value
opt = lbxOption.Value
currtime = time()
today = Format(Now(), "MM/DD/YYYY")
emp = TextBox1.Value
With ActiveCell
spot = .Offset(-1, 0)
If Len(spot) = 0 Then
spot = .Offset(1, 0)
Else
spot = spot
End If
End With
If NoFill = True Then
cellFill = ""
ElseIf NoFill = False Then
With Sheet5
.Range("A1").Value = "Time"
.Range("B1").Value = "Date"
.Range("C1").Value = "Location"
.Range("D1").Value = "Category"
.Range("E1").Value = "BOL"
.Range("f1").Value = "Trailer #"
.Range("g1").Value = "Details"
.Range("H1").Value = "EE Name"
.Range("A2").EntireRow.Insert
.Range("A2").Value = currtime
.Range("B2").Value = today
.Range("C2").Value = spot
.Range("D2").Value = opt
.Range("E2").Value = strBOL
.Range("F2").Value = strID
.Range("G2").Value = details
.Range("H2").Value = emp
.Columns("A:H").AutoFit
End With
If Not IsEmpty(opt) Then
cellFill = opt & " " & vbCrLf & "BOL (last 5 digits): " & strBOL & " " & vbCrLf & "Trailer # " & strID & " " & vbCrLf & details & "EE Name" & emp & " " & vbCrLf
ActiveCell.Value = cellFill
Call RealTimeTracker
End If
End If
Unload Me
Sheet1.Activate
End Sub
I have a problem when trying to type in my combo box (in a userform) in order to find a match. When I type a wrong letter/number it immediately gives a Mismatch Error and directs me to the VBA code. How can I avoid that? Is there something I can add to my code or to change in the properties? Because for the user it is common to type something wrong and I don't want to redirect the users to the code.
This is the code for my combo box:
Private Sub ComboBox3_Change()
If Me.ComboBox3.Value <> "" Then
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("11")
Set ph = ThisWorkbook.Sheets("22")
Dim i As String
i = Application.Match((Me.ComboBox3.Value), sh.Range("A:A"), 0)
Me.TextBox8.Value = ph.Range("D" & i).Value
Me.TextBox13.Value = ph.Range("P" & i).Value
Me.TextBox41.Value = ph.Range("B" & i).Value
End If
End Sub
Private Sub UserForm_Activate()
Dim i As Integer
Me.ComboBox3.Clear
Me.ComboBox3.AddItem ""
For i = 2 To sh.Range("A" & Application.Rows.Count).End(xlUp).Row
Me.ComboBox3.AddItem sh.Range("A" & i).Value
Next i
You need to use error handling statement to skip the part that is generating the error.
Private Sub ComboBox3_Change()
If Me.ComboBox3.Value <> "" Then
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("11")
Set ph = ThisWorkbook.Sheets("22")
Dim i As String
or error go to MyHandler
i = Application.Match((Me.ComboBox3.Value), sh.Range("A:A"), 0)
Me.TextBox8.Value = ph.Range("D" & i).Value
Me.TextBox13.Value = ph.Range("P" & i).Value
Me.TextBox41.Value = ph.Range("B" & i).Value
End If
MyHandler:
' Expected behavior on error
End Sub
I am new to VBA and my problem that I'm struggling with is to autofill a combobox in my form.
For example, my combobox has listed these values:
"apple", "tree", "juice"
I'm asking how to make it search through my list and when I write "app" to provide me with the "apple" result.
This is my code:
Private Sub ComboBox3_Change()
If Me.ComboBox3.Value <> "" Then
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("11")
Set ph = ThisWorkbook.Sheets("22")
Dim i As String
i = Application.Match((Me.ComboBox3.Value), sh.Range("A:A"), 0)
Me.TextBox8.Value = ph.Range("D" & i).Value
Me.TextBox13.Value = ph.Range("P" & i).Value
Me.TextBox41.Value = ph.Range("B" & i).Value
End If
End Sub
Private Sub UserForm_Activate()
Dim i As Integer
Me.ComboBox3.Clear
Me.ComboBox3.AddItem ""
For i = 2 To sh.Range("A" & Application.Rows.Count).End(xlUp).Row
Me.ComboBox3.AddItem sh.Range("A" & i).Value
Next i
I cannot reproduce your issue. I made a ComboBox with the items as shown below:
If I start typing App it looks like below, where it selected Apple automatically
So it does exactly what you are asking for by default.
I am new to coding UserForms in VBA and require a bit of assistance with coding a conditional statement on a certain number of pairs of CheckBoxs and TextBoxs.
Clearly a loop will be involved, but I just cant seem to incorporate it. Here is one of the methods I have tried and failed with:
For i = 1 To 12
If CheckBox(i).Value = True And TextBox(i) = "" Or CheckBox(i).Value = False And TextBox(i).Value <> "" Then
MsgBox ("Warning")
End If
Next i
Thank you for any help!!!!!!
Regards
In a UserForm are not CheckBox or TextBox collections. Thats why CheckBox(index) or TextBox(index) will not work.
But there is a Controls collection.
For i = 1 To 12
'If CheckBox(i).Value = True And TextBox(i) = "" Or CheckBox(i).Value = False And TextBox(i).Value <> "" Then
If Me.Controls("CheckBox" & i).Value = True And Me.Controls("TextBox" & i).Value = "" _
Or Me.Controls("CheckBox" & i).Value = False And Me.Controls("TextBox" & i).Value <> "" Then
MsgBox ("Warning")
End If
Next
Assuming all CheckBoxes have corresponding TextBoxes with same numbers, CeckBox1 - TextBox1, CeckBox2 - TextBox2, ... then:
Dim oControl As Control
Dim sTextBoxName As String
For Each oControl In Me.Controls
If LCase(TypeName(oControl)) = "checkbox" Then
sTextBoxName = Replace(oControl.Name, "checkbox", "textbox", , , vbTextCompare)
If oControl.Value = (Me.Controls(sTextBoxName).Value = "") Then
MsgBox "Warning " & Me.Controls(sTextBoxName).Name & ", " & oControl.Name
End If
End If
Next