I am trying to get to the next form for my excel macro
right now I have a login form that has user name and password
that works when I press login it goes to next form.
but when I go to my next form and type the information in it it closes and doesn't pull up the next forum.
Can someone please explain to me what I am doing wrong? why wont my next form pop up?
Private Sub cmdAdd_Click()
'Dim iRow As Long
'Dim ws As Worksheet
Set ws = Worksheets("D544 Back Panel")
'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.txtPrd.Value) = "" Then
Me.txtPrd.SetFocus
MsgBox "Please enter a Production Number"
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.txtDate.Value
.Cells(iRow, 2).Value = Me.txtHrs.Value
.Cells(iRow, 3).Value = Me.txtPrd.Value
.Cells(iRow, 4).Value = Me.txtSrp.Value
.Cells(iRow, 5).Value = Me.txtOper.Value
' .Protect Password:="Password"
End With
'clear the data
Me.txtDate.Value = ""
Me.txtHrs.Value = ""
Me.txtPrd.Value = ""
Me.txtSrp.Value = ""
Me.txtOper.Value = ""
Me.txtPrd.SetFocus
Unload Me
End Sub
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
I used
Formname1.Hide
Formname2.Show
This seemed to solve my issue. Thanks for the help guys. Sorry I am very new to this site.
Related
I have been searching for a solution for hours, Can anyone help me?
I have a cmd button to open a user form and create a new entry in a 'master enquiry log' worksheet. I would like the cmd button to autofill the next number to the master log in column 'A' and then return that number to the user form.
I think there may need to be a user form initialization sub added but im not sure what to enter.
Any help will be much appreciated
Private Sub cmdAdd_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Master Enq. Log")
'find first empty row in database
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
'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, 2).Value = Me.txtCustName.Value
.Cells(iRow, 3).Value = Me.txtCustAddr.Value
.Cells(iRow, 4).Value = Date
.Cells(iRow, 5).Value = Me.cboProjEng.Value
.Cells(iRow, 6).Value = Me.cboDrawer.Value
.Cells(iRow, 11).Value = Me.cboSalesPers.Value
' .Protect Password:="password"
End With
'clear the data
Me.txtCustName.Value = ""
Me.txtCustAddr.Value = ""
Me.cboProjEng.Value = "Select"
Me.cboDrawer.Value = "Select"
Me.cboSalesPers.Value = "Select"
Me.txtCustName.SetFocus
End Sub
I wanted to have a code wherein there will be an error or prompt in the userform if the cell in the sheet- wherein the data will be transferred, already has content. As of now, the code I'm using doesn't show any prompt but it succeeds in not updating the cell if a data is to be transferred to it again.
Private Sub CancelButton_Click()
Unload Me
End Sub
Private Sub ClearButton_Click()
Call UserForm_Initialize
End Sub
Private Sub OKButton_Click()
Dim emptyRow As Long
'Make Sheet1 active
Sheet1.Activate
'Determine emptyRow
Dim rFound As Range: Set rFound = Range("B:B").Find(BarcodeTextBox.Value, , , xlWhole)
If rFound Is Nothing Then
emptyRow = Range("B" & Rows.Count).End(xlUp).Row + 1
Else
emptyRow = rFound.Row
End If
'Transfer information
If TimeOptionButton1.Value = True Then
Cells(emptyRow, 5).Value = "Yes"
End If
If TimeOptionButton2.Value = True Then
Cells(emptyRow, 7).Value = "Yes"
End If
If BreakOptionButton1.Value = True Then
Cells(emptyRow, 9).Value = "Yes"
End If
If BreakOptionButton2.Value = True Then
Cells(emptyRow, 11).Value = "Yes"
End If
If BreakOptionButton3.Value = True Then
Cells(emptyRow, 14).Value = "Yes"
End If
If BreakOptionButton4.Value = True Then
Cells(emptyRow, 16).Value = "Yes"
End If
Cells(emptyRow, 2).Value = BarcodeTextBox.Value
Set ws = ActiveSheet
Me.TextBox1 = Application.WorksheetFunction. _
CountIf(ws.Range("$T$2:$E$977"), "IN")
Me.TextBox2 = Application.WorksheetFunction. _
CountIf(ws.Range("$U$2:$U$977"), "LF")
Me.TextBox3 = Application.WorksheetFunction. _
CountIf(ws.Range("$U$2:$U$977"), "READYMAN")
Me.TextBox4 = Application.WorksheetFunction. _
CountIf(ws.Range("$U$2:$U$977"), "B-MIRK")
Me.TextBox5 = Application.WorksheetFunction. _
CountIf(ws.Range("$U$2:$U$977"), "VISITOR")
End Sub
Private Sub UserForm_Initialize()
'Set Time In as default
TimeOptionButton1.Value = True
'Empty BarcodeTextBox
BarcodeTextBox.Value = ""
Set ws = ActiveSheet
Me.TextBox1 = Application.WorksheetFunction. _
CountIf(ws.Range("$T$2:$E$977"), "IN")
Me.TextBox2 = Application.WorksheetFunction. _
CountIf(ws.Range("$U$2:$U$977"), "LF")
Me.TextBox3 = Application.WorksheetFunction. _
CountIf(ws.Range("$U$2:$U$977"), "READYMAN")
Me.TextBox4 = Application.WorksheetFunction. _
CountIf(ws.Range("$U$2:$U$977"), "B-MIRK")
Me.TextBox5 = Application.WorksheetFunction. _
CountIf(ws.Range("$U$2:$U$977"), "VISITOR")
End Sub
Thank you in advance!
Concerning your code, I think that you may add something like this:
If TimeOptionButton1.Value = True Then
if len(cells(emptyRow,5)) = 0 then
MsgBox "Write Error Message here"
else
Cells(emptyRow, 5).Value = "Yes"
end if
End If
For each yes. You may consider building a separate function/sub later to avoid repetition.
I am relatively new to the whole VBA so any help would be greatly appreciated...
I am having issues with getting my Combobox on my form (once you hit the submit button) to input the information to my excel spreadsheet in a specific column which would move down a row each time a new record is entered.
Please see my code below and If you need any further information please ask away :) Many Thanks in advance Paula
Option Explicit
Private Sub cmdAdd_Click()
Dim irow As Long
Dim EorP As String
Dim ComboStaus As ComboBox
Dim ws As Worksheet
Set ws = Worksheets("BS Personal Data")
'find first empty row in spreadsheet
irow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
EorP = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
Combo = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
'copy the data to the spreadsheet
With ws
.Cells(irow, 1).Value = Me.txtmanager.Value
.Cells(irow, 2).Value = Me.txtdivision.Value
.Cells(irow, 3).Value = Me.txtlocation.Value
.Cells(irow, 4).Value = Me.txtsystemname.Value
.Cells(irow, 9).Value = Me.Txtpurpose.Value
.Cells(irow, 10).Value = Me.txtaccess.Value
.Cells(irow, 11).Value = Me.txtdatecompleted.Value
End With
'clear the data
Me.txtmanager.Value = ""
Me.txtdivision.Value = ""
Me.txtlocation.Value = ""
Me.txtsystemname.Value = ""
Me.Txtpurpose.Value = ""
Me.txtaccess.Value = ""
Me.txtdatecompleted.Value = ""
'set option button to input data based on type of information to column 5
With ws
If OptElectronic Then
.Cells(EorP, 5).Value = "Electronic"
Else
.Cells(EorP, 5).Value = "PaperBased"
End If
End With
'set option button to input data based on personal data into column 7
With ws
If PersonalYes Then
.Cells(EorP, 7).Value = "Yes"
Else
.Cells(EorP, 7).Value = "No"
End If
End With
'set option button to input data based on privacy notes into column 8
With ws
If PrivacyYes Then
.Cells(EorP, 8).Value = "Yes"
Else
.Cells(EorP, 8).Value = "No"
End If
End With
End Sub
'combo button setup
Private Sub UserForm_Activate()
ComboStatus.Clear
With ComboStatus 'this loads the combo
.AddItem ""
.AddItem "Live"
.AddItem "Archived"
.AddItem "zzz"
End With
End Sub
'close button on the form
Private Sub cmdClose_Click()
Unload Me
End Sub
I'm a beginner when it comes to VB and i'm having a bit of trouble developing a form. What i'm trying to achieve is for the form, on click,to:
Validate four text boxes and nine combo boxes, ensuring they all have values before being submitted to an MS Excel sheet
If there are null fields, a message box telling the user "Text box(es) and/or drop down box(es) must contain data" (or something to that effect) should appear
Assuming all fields have values, the workbook must be unprotected
Data must then be input into the Excel sheet (each form submission equals one row of data, which must not be overwritten by subsequent entries).
The workbook must be protected again
The form must be hidden once all actions are complete.
Here's my code so far. I'm sure its very simplistic and can be made more efficient -- any suggestions welcome. Thank you in advance for your help.
Private Sub CommandButton1_Click()
Sheet2.Unprotect
Dim LastRow As Object
Set LastRow = Sheet2.Range("a65536").End(xlUp)
LastRow.Offset(1, 0).Value = ExpRecDrop.Text
Set LastRow = Sheet2.Range("b65536").End(xlUp)
LastRow.Offset(1, 0).Value = CPName.Text
Set LastRow = Sheet2.Range("c65536").End(xlUp)
LastRow.Offset(1, 0).Value = ConEntDrop.Text
Set LastRow = Sheet2.Range("d65536").End(xlUp)
LastRow.Offset(1, 0).Value = ResTypDrop.Text
Set LastRow = Sheet2.Range("e65536").End(xlUp)
LastRow.Offset(1, 0).Value = LangDrop.Text
Set LastRow = Sheet2.Range("f65536").End(xlUp)
LastRow.Offset(1, 0).Value = WritDrop.Text
Set LastRow = Sheet2.Range("g65536").End(xlUp)
LastRow.Offset(1, 0).Value = OwnerDrop.Text
Set LastRow = Sheet2.Range("i65536").End(xlUp)
LastRow.Offset(1, 0).Value = BiRiDrop.Text
Set LastRow = Sheet2.Range("j65536").End(xlUp)
LastRow.Offset(1, 0).Value = ERTextBox.Text
Set LastRow = Sheet2.Range("k65536").End(xlUp)
LastRow.Offset(1, 0).Value = DueDatDrop.Text
Set LastRow = Sheet2.Range("l65536").End(xlUp)
LastRow.Offset(1, 0).Value = SubTypDrop.Text
Set LastRow = Sheet2.Range("o65536").End(xlUp)
LastRow.Offset(1, 0).Value = CommText.Text
Sheet2.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
Me.Hide
MsgBox "One record written to the 2014 tab"
End Sub
This is my first post so please let me know if you need any more information.
You could try this. I've created a Boolean (True/False) variable to determine if the update should happen, if any of your controls is blank it will change to false and skip the update. The other change I've made is instead of repeatedly working out the next available cell is to work out the next free row number once and use that as a reference to the row to populate.
Private Sub CommandButton1_Click()
Dim LastRow As Long
Dim isOk As Boolean
sheet2.Unprotect
' Create boolean variable as true and check controls for data, if any data missing, change to false
isOk = True
If ExpRecDrop.Text = "" Then isOk = False
If CPName.Text = "" Then isOk = False
If ConEntDrop.Text = "" Then isOk = False
If ResTypDrop.Text = "" Then isOk = False
If LangDrop.Text = "" Then isOk = False
If WritDrop.Text = "" Then isOk = False
If OwnerDrop.Text = "" Then isOk = False
If BiRiDrop.Text = "" Then isOk = False
If ERTextBox.Text = "" Then isOk = False
If DueDatDrop.Text = "" Then isOk = False
If SubTypDrop.Text = "" Then isOk = False
If CommText.Text = "" Then isOk = False
' if boolean value is still true, update the sheet, else display error message
If Not isOk Then
With sheet2
' get number of the next free row
Set LastRow = .Range("a65536").End(xlUp).Row + 1
' Populate the sheet
.Cells(LastRow, 1).Value = ExpRecDrop.Text
.Cells(LastRow, 2).Value = CPName.Text
.Cells(LastRow, 3).Value = ConEntDrop.Text
.Cells(LastRow, 4).Value = ResTypDrop.Text
.Cells(LastRow, 5).Value = LangDrop.Text
.Cells(LastRow, 6).Value = WritDrop.Text
.Cells(LastRow, 7).Value = OwnerDrop.Text
.Cells(LastRow, 8).Value = BiRiDrop.Text
.Cells(LastRow, 9).Value = ERTextBox.Text
.Cells(LastRow, 10).Value = DueDatDrop.Text
.Cells(LastRow, 11).Value = SubTypDrop.Text
.Cells(LastRow, 12).Value = CommText.Text
.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End With
' Hide userform and confirm
Me.Hide
MsgBox "One record written to the 2014 tab"
Else
' Error message
MsgBox "Some data missing"
End If
End Sub
I have this code, but i want to add the Name etc, directly under the "name" in excel, but this far its only adding it in row 1. Can someone help me?
Example, when i type into the name-box, i want the value to be directly under "Name" in Excel, no matter where the "Name" stands in my Excal sheet.
I'm new here and this is my first question :)
'find first empty row in database
iRow = ws.Cells.Find(What:="Name", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
'check for a part number
If Trim(Me.TxtName.Value) = "" Then
Me.TxtName.SetFocus
MsgBox "Please enter a part number"
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.TxtName.Value
.Cells(iRow, 2).Value = Me.TxtLocation.Value
.Cells(iRow, 3).Value = Me.TxtDate.Value
.Cells(iRow, 4).Value = Me.TxtQuantity.Value
' .Protect Password:="password"
End With
Thanks.
Assuming the OP really wants to populate the first empty row:
Dim pNum, rngName As Range
pNum = Trim(Me.TxtName.Value) 'check for a part number
If Len(pNum) = 0 Then
Me.TxtName.SetFocus
MsgBox "Please enter a part number"
Exit Sub
End If
'find first empty row in database
Set rngName = ws.Cells.Find(What:="Name", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues, LookAt:=xlWhole)
If Not rngName is Nothing then
With ws.cells(rows.count, rngName.Column).End(xlUp).offset(1,0).entirerow
.Cells(1).Value = pNum
.Cells(2).Value = Me.TxtLocation.Value
.Cells(3).Value = Me.TxtDate.Value
.Cells(4).Value = Me.TxtQuantity.Value
End With
Else
msgbox "'Name' header not found!"
End if
Try the below as a substitute for that section of code you posted.
Dim anchorCell As Range
'find first empty row in database
If ws.Cells(1,1).Value = "Name" Then
Set anchorCell = ws.Cells(1,1)
Else
Set anchorCell = ws.Cells.Find(What:="Name", SearchOrder:=xlRows, _
SearchDirection:=xlNext, LookIn:=xlValues)
End If
If Not anchorCell Is Nothing Then
'check for a part number
If Trim(Me.TxtName.Value) = "" Then
Me.TxtName.SetFocus
MsgBox "Please enter a part number"
Exit Sub
End If
'copy the data to the database
'use protect and unprotect lines,
' with your password
' if worksheet is protected
With anchorCell
' .Unprotect Password:="password"
.Offset(1, 0).Value = Me.TxtName.Value
.Offset(1, 1).Value = Me.TxtLocation.Value
.Offset(1, 2).Value = Me.TxtDate.Value
.Offset(1, 3).Value = Me.TxtQuantity.Value
' .Protect Password:="password"
End With
End If