Userform Data entry "Update" setting wrong values when editing an entry - excel

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.

Related

Show data previously entered on a userform and allow data to be edited

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

VBA Userform Listbox Conditional Logic Not Working as Intended

I have a Userform with a listbox for which I am using conditional logic to determine output values to the sheet of selected or non-selected items in the listbox. The issue is that when the Textbox (Tbl_AliasName) is blank, the code executes this:
ElseIf .Selected(k) = True And Tbl_AliasName = vbNullString Then
lstbxRow = lstbxRow + 1
Worksheets("New TRAX").Cells(lstbxRow, 3) = Trim(Cells(2, 1).Value2) & "." & .Column(1, k)
But if Tbl_AliasName is not blank then the code does nothing, but it is supposed to do this:
ElseIf .Selected(k) = True And Tbl_AliasName <> vbNullString Then
lstbxRow = lstbxRow + 1
Worksheets("New TRAX").Cells(lstbxRow, 3) = Tbl_AliasName & "." & .Column(1, k)
I have used several variations of If statements, and non of which have worked.
Below is My Code:
Option Explicit
Public Tbl_AliasName As String
Tbl_AliasName = Trim(UserForm_Finder.txtConcat.Value)
Private Sub BtnConcat_Click()
Dim k As Long, lstbxRow As Long, LR As Long
lstbxRow = 1
'****************
'This if statement works perfectly
If (Cells(2, 1).Value2 = vbNullString Or Cells(2, 2).Value2 = vbNullString) _
And Tbl_AliasName = vbNullString Then
MsgBox "You must Search for a Table or Column first.", _
vbExclamation, "Error Encountered"
Exit Sub
ElseIf (UserForm_Finder.ListBx_TblsCols.ListCount = 0 And Tbl_AliasName <> vbNullString) Then
MsgBox "You must Search for a Table or Column first.", _
vbExclamation, "Error Encountered"
'(Cells(2, 1).Value2 = vbNullString Or Cells(2, 2).Value2 = vbNullString) And _
Exit Sub
End If
With UserForm_Finder.ListBx_TblsCols
For k = 0 To .ListCount - 1
'****************
This is where the problems begin
If .Selected(k) = False Then
MsgBox "You must Select 1 or more items from the list box.", _
vbExclamation, "Error Encountered"
Exit Sub
ElseIf .Selected(k) = True And Tbl_AliasName <> vbNullString Then
lstbxRow = lstbxRow + 1
Worksheets("New TRAX").Cells(lstbxRow, 3) = Tbl_AliasName & "." & .Column(1, k)
ElseIf .Selected(k) = True And Tbl_AliasName = vbNullString Then
lstbxRow = lstbxRow + 1
Worksheets("New TRAX").Cells(lstbxRow, 3) = Trim(Cells(2, 1).Value2) & "." & .Column(1, k)
End If
Next k
End With
End Sub
My goal is to do the following:
If a Textbox (Tbl_AliasName) is not blank and the user has selected one or more items in the listbox (ListBx_TbleCols) then concatenate the Tbl_AliasName to the selected items in the listbox
If Tbl_AliasName is blank, then use the value in Cells(2,1) to concatenate to the selected Items in the list box.
I have tried the following additions:
Dim LstBxItemSelected As Boolean
'This was placed in the for loop
LstBxItemSelected = True
'this was placed outside the for loop
If LstBxItemSelected = False Then
MsgBox "You must Select 1 or more items from the list box.", _
vbExclamation, "Error Encountered"
Exit Sub
End If
Is there a better way to tell if items are selected, because I feel that the way I have it structured in my loop, the code will throw the error if everything isn't selected? Thank you in advance for any ideas, answers, or suggestions!
Note: The Listbox is populated by the click of another button on the userform which calls the following sub:
Sub FillLstBxCols()
Dim ListBx_Target As MSForms.ListBox
Dim rngSource As Range
Dim LR As Long
If Cells(2, 1).Value2 <> vbNullString Then
LR = Worksheets("New TRAX").Cells(Rows.Count, 2).End(xlUp).Row
'Set reference to the range of data to be filled
Set rngSource = Worksheets("New Trax").Range("A" & 2 & ":" & "B" & LR)
'Fill the listbox
Set ListBx_Target = UserForm_Finder.ListBx_TblsCols
With ListBx_Target
.RowSource = rngSource.Address
End With
End If
End Sub
Hard to say without sample data and expected results, but I think this is what you're looking for:
Private Sub btnConcat_Click()
Dim ws As Worksheet
Dim bSelected As Boolean
Dim sConcat As String
Dim i As Long, lRowIndex As Long
Set ws = ActiveWorkbook.Sheets("New TRAX")
lRowIndex = 1
bSelected = False
sConcat = Trim(Me.txtConcat.Text)
If Len(sConcat) = 0 Then sConcat = Trim(ws.Cells(2, "A").Value)
If Len(sConcat) = 0 Then
MsgBox "You must Search for a Table or Column first.", vbExclamation, "Error Encountered"
Exit Sub
End If
For i = 0 To Me.ListBx_TblsCols.ListCount - 1
If Me.ListBx_TblsCols.Selected(i) Then
If bSelected = False Then
bSelected = True
ws.Range("C2", ws.Cells(ws.Rows.Count, "C")).Clear 'clear previous concat results (delete this line if not needed)
End If
lRowIndex = lRowIndex + 1
ws.Cells(lRowIndex, "C").Value = sConcat & "." & Me.ListBx_TblsCols.List(i)
End If
Next i
If bSelected = False Then MsgBox "Must select at least one item from the list"
End Sub

Excel code, my 'add data' button is not working

I am new to coding and have got this far by googling. I am hoping it is a fairly simple change in the code. I have a form which shows what I want it to. My close button works however my 'add data' button doesn't work. As in it isn't populating the information I enter into the completed form to the worksheet in excel. I get the first column populating always with 0.1, no matter what I enter in. Here is my code if anyone would like to assist. I would be very grateful.
Private Sub cmdbutton_add_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Inputs")
'find first empty row in database
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
'Check for a Name number
If Trim(Me.textbox_lineno.Value) = " " Then
Me.textbox_lineno.SetFocus
MsgBox "Please Complete the form"
Exit Sub
End If
'copy the data to the database
ws.Cells(iRow, 1).Value = Me.textbox_lineno.Value
ws.Cells(iRow, 1).Value = Me.listbox_pipetype.Value
ws.Cells(iRow, 1).Value = Me.textbox_frompit.Value
ws.Cells(iRow, 1).Value = Me.textbox_topitormh.Value
ws.Cells(iRow, 1).Value = Me.textbox_linealm.Value
ws.Cells(iRow, 1).Value = Me.textbox_depthstart.Value
ws.Cells(iRow, 1).Value = Me.textbox_depthend.Value
ws.Cells(iRow, 1).Value = Me.textbox_pipedia.Value
ws.Cells(iRow, 1).Value = Me.textbox_beddingbelow.Value
ws.Cells(iRow, 1).Value = Me.textbox_beddingabove.Value
ws.Cells(iRow, 1).Value = Me.textbox_roadallowance.Value
MsgBox "Data Added", vbOKOnly + vbInformation, "Data Added"
'Clear the data
Me.textbox_lineno.Value = ""
Me.listbox_pipetype.Value = ""
Me.textbox_frompit.Value = ""
Me.textbox_topitormh.Value = ""
Me.textbox_linealm.Value = ""
Me.textbox_depthstart.Value = ""
Me.textbox_depthend.Value = ""
Me.textbox_pipedia.Value = ""
Me.textbox_beddingbelow.Value = ""
Me.textbox_beddingabove.Value = ""
Me.textbox_roadallowance.Value = ""
Me.textbox_lineno.SetFocus
End Sub
Private Sub cmdbutton_close_Click()
Unload Me
End Sub
This line should never validate:
If Trim(Me.textbox_lineno.Value) = " " Then
You never increment your column assignments. If I know that an Object exists then I will usually opt for a With Object statement over using another variable.
I extracted the code for clearing the textboxes into their own sub routine.
Because all the data is on one row and With .Range("A1", .Range("A" & .Rows.Count).End(xlUp)).Offset(1) refers the first cell of that row I use Columns(x) which is analogous to using .Cells(1,x). I fell like it looks cleaner.
I feel like these alterations to consolidate the code; make it easier to read. You don't have to scroll up and down the page to see just what the sub is doing.
Private Sub cmdbutton_add_Click()
'Check for a Name number
If Trim(Me.textbox_lineno.Value) = "" Then
Me.textbox_lineno.SetFocus
MsgBox "Please Complete the form", vbInformation, "Action Cancelled"
Exit Sub
End If
With Worksheets("Inputs")
With .Range("A1", .Range("A" & .Rows.Count).End(xlUp)).Offset(1)
'copy the data to the database
.Columns(1).Value = Me.textbox_lineno.Value
.Columns(2).Value = Me.listbox_pipetype.Value
.Columns(3).Value = Me.textbox_frompit.Value
.Columns(4).Value = Me.textbox_topitormh.Value
.Columns(5).Value = Me.textbox_linealm.Value
.Columns(6).Value = Me.textbox_depthstart.Value
.Columns(7).Value = Me.textbox_depthend.Value
.Columns(8).Value = Me.textbox_pipedia.Value
.Columns(9).Value = Me.textbox_beddingbelow.Value
.Columns(10).Value = Me.textbox_beddingabove.Value
.Columns(11).Value = Me.textbox_roadallowance.Value
End With
End With
MsgBox "Data Added", vbOKOnly + vbInformation, "Data Added"
ClearForm
Me.textbox_lineno.SetFocus
End Sub
Function ClearForm()
'Clear the data
Me.textbox_lineno.Value = ""
Me.listbox_pipetype.Value = ""
Me.textbox_frompit.Value = ""
Me.textbox_topitormh.Value = ""
Me.textbox_linealm.Value = ""
Me.textbox_depthstart.Value = ""
Me.textbox_depthend.Value = ""
Me.textbox_pipedia.Value = ""
Me.textbox_beddingbelow.Value = ""
Me.textbox_beddingabove.Value = ""
Me.textbox_roadallowance.Value = ""
End Function

VBA code Using Data table to populate the userform

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.

Conditional statements on CheckBox & TextBox in Userforms (VBA)

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

Resources