I have 2 sheet on my excel workbook.
1 is Stock In sheet and 1 is Stock Out sheet.
I wish to store the information in Stock Out sheet when the data is found in Stock In.
Stock In sheet:
Stock Out sheet:
For example, the Stock Out sheet only will able to accept the data when the PT# and the Rack is tally with the detail in Stock In sheet.
As below will be the code for my delete button inside my userform:
Private Sub TrackOut_Click()
Sheets("Stock Out").Activate
Dim cDelete As VbMsgBoxResult
With Me
If Len(.TextBox1.Value) * Len(.PT.Value) *
Len(.Rack2.Value) * _
Len(.Operator2.Value) = 0 Then
MsgBox "Please Complete All Fields Before Submit"
Else
cDelete = MsgBox("Are you sure that you want to delete this record", vbYesNo + vbDefaultButton2, "Track Out")
If cDelete = vbYes Then
If Sheets("Stock In").Columns(2).Find(What:=PT.Text) Is Nothing Then
MsgBox "No stock inventory for this PT#"
Exit Sub
End If
eRow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Cells(eRow, 1).Value = TextBox1.Text
Cells(eRow, 2).Value = PT.Text
Cells(eRow, 3).Value = Rack2.Text
Cells(eRow, 4).Value = Operator2.Text
Else
If cDelete = vbNo Then
Unload Me
End If
End If
End If
End With
End Sub
OK - this is what I understand and what the code adjustment below does:
Operator completes a userform and inputs Date, PT#, Rack No and Operator. This gets looked-up in 'Stock In' sheet (all fields must match). If operator confirms then the record gets transferred to 'Stock Out' sheet on next available row and deleted from 'Stock In' sheet and other records moved up.
If you only want say two fields to match (userform and 'Stock In') then see the code for the adjustments to make: commented as '**
Private Sub TrackOut_Click()
Sheets("Stock Out").Activate
Dim cDelete As VbMsgBoxResult
Dim fndItm As Range
Dim orow As Long, irow As Long
Dim reqStock As String, itmStock As String
Dim stockArr(4) As String
With Me
If Len(.TextBox1.Value) * Len(.PT.Value) * Len(.Rack2.Value) * Len(.Operator2.Value) = 0 Then
MsgBox "Please Complete All Fields Before Submit."
Else
'collect requested (userform) data
'** reqStock should include those fields you require to match with Stock In record
'** currently set to check all fields
reqStock = .TextBox1.Value & .PT.Value & .Rack2.Value & .Operator2.Value
Set fndItm = Sheets("Stock In").Columns(2).Find(What:=PT.Text)
If Not fndItm Is Nothing Then
'if PT# found collect stock row data
With Sheets("Stock In")
irow = fndItm.Row
stockArr(0) = Format(.Cells(irow, 1).Value, "dd/mm/yyyy")
stockArr(1) = .Cells(irow, 2).Value
stockArr(2) = .Cells(irow, 3).Value
stockArr(3) = .Cells(irow, 4).Value
'** itmStock should include those fields you require to match with userform fields
'** these should match reqStock
'** currently set to check all fields
itmStock = stockArr(0) & stockArr(1) & stockArr(2) & stockArr(3)
End With
'compare requested (userfrom) data with Stock In data
If reqStock = itmStock Then
cDelete = MsgBox("Are you sure that you want to delete this record from stock?", vbYesNo) ' + vbDefaultButton2, _
"Track Out")
If cDelete = vbYes Then
'xfer record to Stock Out sheet
With Sheets("Stock Out")
orow = .Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
.Range(.Cells(orow, 1), .Cells(orow, 4)) = stockArr
End With
'delete record from Stock In sheet
With Sheets("Stock In")
.Range(.Cells(irow, 1), .Cells(irow, 4)).Delete xlShiftUp
End With
End If
'clear userform fields for next entry
.TextBox1.Value = ""
.PT.Value = ""
.Rack2.Value = ""
.Operator2.Value = ""
Else
MsgBox "PT# found but requested information does not match."
End If
Else
MsgBox "No stock inventory for this PT#."
Exit Sub
End If
End If
End With
End Sub
Related
My first Sub uses a value that I enter in a userform to search a table on another sheet and update the userform with the currently entered values from that sheet.
I have a second Sub that will write the updated values back to the same sheet. I have a checkbox that the user checks when the the related project is complete and we no longer need the data in the row and would like it deleted.
When this is checked and the user clicks update, the sub is to delete that Row from the sheet.
The main sheet is just a replica of the sheet that contains the actual data. That sheet receives updates from Zapier. I have macros that hide rows and columns and this messes with connected apps when they try to add data which is the reason for the second sheet.
I have spent all day today trying to figure out how to delete the row that has been selected
by the search function in the userform. The sub performs the same search when updating the row so I thought if I added an IF statement to either update or delete depending on the status of the checkbox.
Here is my code as it stands:
Private Sub UpdateRecord()
Dim RecordRow As Long
Dim RecordRange As Range
Dim Answer As VbMsgBoxResult
Dim DeleteRow As Long
Dim DeleteRange As Range
Dim ws As String
' Find the row in the table that the record is in
RecordRow = Application.Match(CLng(TextBoxWO.Value), Range("JobSheetData[W/O]"), 0)
' Set RecordRange to the first cell in the found record
Set RecordRange = Range("JobSheetData").Cells(1, 1).Offset(RecordRow - 1, 0)
If CheckBoxDelete = "True" Then
'---------------------True = Delete-------------------
' Find the row in the table that the record is in
DeleteRow = Application.Match(CLng(TextBoxWO.Value), Range("JobSheetData[W/O]"), 0)
' Set RecordRange to the first cell in the found record
Set DeleteRange = Range("JobSheetData").Cells(1, 1).Offset(RecordRow - 1, 0)
Answer = MsgBox("Are you sure you want to PERMANENTLY DELETE this job from the job list?" & vbCrLf & vbCrLf & "This action cannot be undone!", vbOKCancel + vbDefaultButton2, "Confirm removal of job from list")
If Answer = vbYes Then
ActiveWorkbook.Worksheets("Job List Import").ListObjects("JobSheetData").ListRows(DeleteRange).Delete
End If
'---------------------False = Update-------------------
Else
RecordRange(1, 1).Offset(0, 5).Value = TextBoxHold.Value
RecordRange(1, 1).Offset(0, 7).Value = TextBoxDays.Value
RecordRange(1, 1).Offset(0, 9).Value = CheckBoxLocate.Value
RecordRange(1, 1).Offset(0, 13).Value = TextBoxFirst.Value
RecordRange(1, 1).Offset(0, 14).Value = TextBoxOveride.Value
RecordRange(1, 1).Offset(0, 15).Value = CheckBoxBell.Value
RecordRange(1, 1).Offset(0, 16).Value = CheckBoxGas.Value
RecordRange(1, 1).Offset(0, 17).Value = CheckBoxHydro.Value
RecordRange(1, 1).Offset(0, 18).Value = CheckBoxWater.Value
RecordRange(1, 1).Offset(0, 19).Value = CheckBoxCable.Value
RecordRange(1, 1).Offset(0, 20).Value = CheckBoxOther1.Value
RecordRange(1, 1).Offset(0, 21).Value = CheckBoxOther2.Value
RecordRange(1, 1).Offset(0, 22).Value = CheckBoxOther3.Value
End If
End Sub
I also tried to incorporate this code but could not get it to work either:
Dim Answer As VbMsgBoxResult
Answer = MsgBox("Are you sure you want to PERMANENTLY DELETE this job from the job list?" & vbCrLf & vbCrLf & "This action cannot be undone!", vbOKCancel + vbDefaultButton2, "Confirm removal of job from list")
If Answer = vbYes Then
If JobSheetInputForm.ListRows.Count < 1 Then Exit Sub
With ActiveSheet.ListObjects("JobSheet")
JobSheetInputForm.ListRows(CurrentRow).Delete
If JobSheetInputForm.ListRows.Count > 0 Then
If CurrentRow > JobSheetInputForm.ListRows.Count Then
CurrentRow = JobSheetInputForm.ListRows.Count
End If
JobSheetInputForm.ListRows(CurrentRow).Range.Select
Else
CurrentRow = 0
End If
End With
End If
I am beat at this point and need your Help! Thank you!
Something like this:
Sub Test()
Dim rngSrch As Range, lo As ListObject, DeleteRow, v
Set lo = ActiveWorkbook.Worksheets("Job List Import").ListObjects("JobSheetData")
Set rngSrch = lo.ListColumns("W/O").DataBodyRange 'range to search in
v = CLng(TextBoxWO.Value) 'value to search
DeleteRow = Application.Match(v, rngSrch, 0) 'try to match a row
If Not IsError(DeleteRow) Then 'got match?
If MsgBox("Are you sure you want to PERMANENTLY DELETE this job from the job list?" & _
vbCrLf & vbCrLf & "This action cannot be undone!", _
vbOKCancel + vbDefaultButton2, "Confirm removal of job from list") = vbOK Then
lo.ListRows(DeleteRow).Delete
End If
Else
MsgBox "Value not found!" 'no match
End If
End Sub
I am using a userform to update data in a worksheet, I have an update command button to copy the data from the 'data' worksheet to the 'archive' and replace in the 'data' worksheet (essentially the 'archive' is a log of all previous lines and the 'data' is the most recent information)
The information is changed in text boxes and combo boxes
What Im struggling with is for the 'update' cmdbutton to first check if any changes where made before copying the data, if not I want a msg box to read 'no change in data, please close form'
Here is the code for the userform so far:
Private Sub cmdUpdate_Click()
' To write edited info of userform2 to Sheets("Data")
Dim LastRow As Long
Dim ABnum As Double
Dim ABrng As Range
Dim WriteRow As Long
'error statement
On Error GoTo errHandler:
'hold in memory and stop screen flicker
Application.ScreenUpdating = False
' Make sure we're on the right sheet
With Sheets("Data")
' Get the last row used so can set up the search range
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
' Set the range to search for the AB number
Set ABrng = .Range("A1:A" & LastRow)
' Get the AB number from what is selected on userform2
ABnum = txtup1.Value
' Get the row of sheet for this AB number
WriteRow = Application.Match(ABnum, ABrng, 0)
' Make this AB number the active cell
With .Cells(WriteRow, 1)
' Write in all the editable options
Sheets("Archive").Range("A" & Rows.Count).End(xlUp)(2).Resize(, 14).Value = .Resize(, 14).Value
.Offset(0, 4) = cboup3.Value
.Offset(0, 5) = cboup4.Value
.Offset(0, 6) = cboup5.Value
.Offset(0, 7) = cboup6.Value
.Offset(0, 8) = Date
.Offset(0, 9) = txtrev.Value
.Offset(0, 12) = txtup9.Value
.Offset(0, 13) = txtup8.Value
End With
End With
' Filter the Data
FilterMe
' Close the form
Unload Me
MsgBox ("Enquiry E0" + Me.txtup1.Text + " has been updated")
errHandler:
'Protect all sheets if error occurs
'Protect_All
'show error information in a messagebox
If Err.Number <> 0 Then
MsgBox "Error " & Err.Number & " just occured."
End If
End Sub
The easiest way would be to write a function to compare the values.
Private Sub cmdUpdate_Click()
' To write edited info of userform2 to Sheets("Data")
Dim LastRow As Long
Dim ABnum As Double
Dim ABrng As Range
Dim WriteRow As Long
'error statement
On Error GoTo errHandler:
'hold in memory and stop screen flicker
Application.ScreenUpdating = False
' Make sure we're on the right sheet
With Sheets("Data")
' Get the last row used so can set up the search range
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
' Set the range to search for the AB number
Set ABrng = .Range("A1:A" & LastRow)
' Get the AB number from what is selected on userform2
ABnum = txtenqup.Value
' Get the row of sheet for this AB number
WriteRow = Application.Match(ABnum, ABrng, 0)
' Make this AB number the active cell
With .Cells(WriteRow, 1)
'Check for changes
If Not hasValuePairsChanges(.Offset(0, 4).Value, cboup3.Value, _
.Offset(0, 5).Value, cboup4.Value, _
.Offset(0, 6).Value, cboup5.Value, _
.Offset(0, 7).Value, cboup6.Value, _
CDate(.Offset(0, 8).Value), Date, _
CDbl(.Offset(0, 9).Value), CDbl(txtrev.Value), _
.Offset(0, 12).Value, txtnotes.Value, _
.Offset(0, 13).Value, txtdtime.Value) Then
MsgBox "No Change in Data", vbInformation, ""
Exit Sub
End If
' Write in all the editable options
Sheets("Archive").Range("A" & Rows.Count).End(xlUp)(2).Resize(, 14).Value = .Resize(, 14).Value
.Offset(0, 4) = cboup3.Value
.Offset(0, 5) = cboup4.Value
.Offset(0, 6) = cboup5.Value
.Offset(0, 7) = cboup6.Value
.Offset(0, 8) = Date
.Offset(0, 9) = txtrev.Value
.Offset(0, 12) = txtnotes.Value
.Offset(0, 13) = txtdtime.Value
End With
End With
' Filter the Data
FilterMe
' Close the form
Unload Me
MsgBox ("Enquiry E0" + Me.txtenqup.Text + " has been updated")
errHandler:
'Protect all sheets if error occurs
'Protect_All
'show error information in a messagebox
If Err.Number <> 0 Then
MsgBox "Error " & Err.Number & " just occured."
End If
End Sub
Function hasValuePairsChanges(ParamArray Args() As Variant) As Boolean
Dim n As Long
For n = 0 To UBound(Args) Step 2
If Not Args(n) = Args(n + 1) Then
hasValuePairsChanges = True
Exit Function
End If
Next
End Function
I have three forms.
Form1(login)
Form 2(informationAdd)
Form 3(Information add to different sheet)
For some reason my macro is not entering my data into excel when I press submit for the third form.
My second form enters information but when entering the third form the information is no where to be found.
Private Sub cmdSubmit_Click()
'Dim iRow As Long
'Dim ws As Worksheet
Set ws = Worksheets("Scrap")
'find first empty row in database
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
'check for a part number
If Trim(Me.txtPress.Value) = "" Then
Me.txtPress.SetFocus
MsgBox "Please enter press scrap"
Exit Sub
End If
'copy the data to the database
'use protect and unprotect lines,
' with your password
' if worksheet is protected
With ws
' .Unprotect Password:="Password"
.Cells(iRow, 1).Value = Me.txtDelam.Value
.Cells(iRow, 2).Value = Me.txtCuts.Value
.Cells(iRow, 3).Value = Me.txtBurns.Value
.Cells(iRow, 4).Value = Me.txtDents.Value
.Cells(iRow, 5).Value = Me.txtStaple.Value
.Cells(iRow, 6).Value = Me.txtGlue.Value
.Cells(iRow, 7).Value = Me.txtPress.Value
' .Protect Password:="Password"
End With
'clear the data
Me.txtDelam.Value = ""
Me.txtCuts.Value = ""
Me.txtBurns.Value = ""
Me.txtDents.Value = ""
Me.txtStaple.Value = ""
Me.txtGlue.Value = ""
Me.txtPress.Value = ""
Me.txtPress.SetFocus
Unload Me
End Sub
Did you send all Form 2 textbox values to Form 3 ?
I tried this way.
You may be have this kind of code in your Form 2 :
Private Sub CommandButton1_Click()
' I send the value from Form2 to Form3
YourForm3.txtPress.Value = YourForm2.TextBox1.Value
YourForm3.Show
Unload Me
End Sub
In the next step, the Form3 is opened with the Form2 informations, I run your code and it's working.
To my test, I just modified your iRow variables to iRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1
I recently started learning how to use VBA since I suddenly got put into a position where I need to know it. I've been doing research on coding and such but I feel like I'm messing up every step of the way. I was asked to create a userform where people could enter their time, date, production, task, and a comment box. I was also asked to have both the input sheet and the data sheet locked, on top of that my boss wanted the sheet itself to save when closed so when workers entered their information they wouldn't save.
Private Sub cmdAdd_Click()
Dim lRow As Long
Dim lPart As Long
Dim ws As Worksheet
Set ws = Worksheets("Data")
'find first empty row in database
lRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
'check for a part number
If Trim(Me.txtComment.Value) = "" Then
Me.txtComment.SetFocus
MsgBox "Comment is required"
End If
'check for a part number
If Trim(Me.ComboBox1.Value) = "" Then
Me.ComboBox1.SetFocus
MsgBox "Date is required!"
End If
'check for a part number
If Trim(Me.txtTime.Value) = "" Then
Me.txtTime.SetFocus
MsgBox "Time is required"
End If
'check for a part number
If Trim(Me.cboProject.Value) = "" Then
Me.cboProject.SetFocus
MsgBox "Project is required"
End If
'check for a part number
If Trim(Me.cboTask.Value) = "" Then
Me.cboTask.SetFocus
MsgBox "Task is required"
Exit Sub
End If
'copy the data to the database
'use protect and unprotect lines,
' with your password
' if worksheet is protected
With ws
' .Unprotect Password:="password"
.Cells(lRow, 1).Value = Me.ComboBox1.Value
.Cells(lRow, 2).Value = Me.txtTime.Value
.Cells(lRow, 3).Value = Me.txtCount.Value
.Cells(lRow, 4).Value = Me.cboUser.Value
.Cells(lRow, 5).Value = Me.cboProject.Value
.Cells(lRow, 6).Value = Me.cboTask.Value
.Cells(lRow, 7).Value = Me.txtComment.Value
' .Protect Password:="password"
'clear the data
Me.cboUser.Value = ""
Me.ComboBox1.Value = ""
Me.txtTime.Value = ""
Me.txtCount.Value = ""
Me.cboProject.Value = ""
Me.cboTask.Value = ""
Me.txtComment.Value = ""
Me.ComboBox1.Value = Format(Date, "Short Date")
MsgBox ("Work Submitted, proceed to close the form and save the workbook.")
End With
End Sub
Apologies in advance for how awful the code probably is, but it seemed to work just enough for what I was going, the only problem was that when I was recently testing it even if I had a message saying comment required or task required it would still paste information into the datasheet which I don't want to happen.
I want to record user data using a userform.
In first textbox, I will insert user's name.
Second textbox is their ID.
In the third textbox I want to generate a 5 character ID/code (mixed number & alphabet) by clicking the 'Generate' button (but I have no idea what is the coding).
Once I click 'Add user', I would like the data to be populated in the Excel sheet. I would like to insert number 1, 2, 3... in Column A, today's date (when the user details added) in Column B. Followed by the data added in the Userform in Column C, D & E.
Here is what I want the data to look like:
Here is code I copied from the site.
Private Sub CommandButton2_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Database")
'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.TextBox1.Value) = "" Then
Me.TextBox1.SetFocus
MsgBox "Please complete the form"
Exit Sub
End If
'copy the data to the database
ws.Cells(iRow, 1).Value = Me.TextBox1.Value
ws.Cells(iRow, 2).Value = Me.TextBox2.Value
ws.Cells(iRow, 3).Value = Me.TextBox3.Value
MsgBox "Data added", vbOKOnly + vbInformation, "Data Added"
'clear the data
Me.TextBox1.Value = ""
Me.TextBox2.Value = ""
Me.TextBox3.Value = ""
Me.TextBox1.SetFocus
End Sub
This will generate a five character random string of numbers and letters. You could assign to your button and output to your form rather than a message box.
Sub x()
Dim vOut(1 To 5), i As Long, n As Long
Randomize
For i = 1 To 5
n = WorksheetFunction.RandBetween(1, 2)
If n = 1 Then
vOut(i) = WorksheetFunction.RandBetween(0, 9)
Else
vOut(i) = Chr(64 + WorksheetFunction.RandBetween(1, 26))
End If
Next i
MsgBox Join(vOut, "")
End Sub