Excel user form combobox once submitted output to excel - vba

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

Related

How do i get to next form in visual basic

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.

Adding new autofill cell and returning cell value to userform VBA Excel

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

VBA Runtime Error 9 - Subscript out of range

I am attempting to create a user form that will propagate the first available row of the excel sheet with information. I am struggling to get the range correct, and the form is currently showing 'Subscript out of range' ; 'Runtime error 9'. There also seems to be a problem with the 'if' statement, but following the standard advice online doesn't seem to have helped solve the problem.
If anyone has any idea where I'm going wrong (I'm very new to this) that would be great.
Private Sub UserForm_Initialize()
BusinessAreaBox.List = Array("option one", "option two")
End Sub
Private Sub CommandButton1_Click()
Dim RowCount As Long
RowCount = Sheets("Sheet1").Range.Sheets("Sheet1").Cells(2, "A")
With ThisWorkbook.Sheets("Sheet1").Range("A2")
.Offset(RowCount, 0).Value = BusinessArea1.Value
.Offset(RowCount, 1).Value = BusinessContact1.Value
.Offset(RowCount, 2).Value = LPSContact1.Value
.Offset(RowCount, 4).Value = ProjectedFTE1.Value
.Offset(RowCount, 5).Value = DateOfMostRecentMeeting1.Value
.Offset(RowCount, 6).Value = FTEComment1.Value
.Offset(RowCount, 7).Value = ProposedMove1.Value
.Offset(RowCount, 8).Value = DeskUtilisation1.Value
.Offset(RowCount, 9).Value = OtherComment1.Value
.Offset(RowCount, 10).Value = Actions1.Value
If RegularMeeting1.Value = True Then
.Offset(RowCount, 3).Value = "Yes"
Else
.Offset(RowCount, 3).Value = "No"
End If
RegularMeeting1.Value = True Or False
End With
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub
I guess you may be after this
Private Sub CommandButton1_Click()
With ThisWorkbook.Sheets("Sheet1")
.Cells(.Rows.Count, 1).End(xlUp).Offset(1).Resize(, 11).Value = Array(BusinessArea1.Value, _
BusinessContact1.Value, _
LPSContact1.Value, _
IIf(RegularMeeting1.Value, "Yes", "No"), _
ProjectedFTE1.Value, _
DateOfMostRecentMeeting1.Value, _
FTEComment1.Value, _
ProposedMove1.Value, _
DeskUtilisation1.Value, _
OtherComment1.Value, _
Actions1.Value)
End With
RegularMeeting1.Value = True Or False '<--| what is this supposed to do?
End Sub
Try writing to the cells this way. You need to active the sheet and cell first.
Dim myArr As Variant
myArr = Array(BusinessArea1.Value, _
LPSContact1.Value, _
ProjectedFTE1.Value, _
DateOfMostRecentMeeting1.Value, _
FTEComment1.Value, _
ProposedMove1.Value, _
DeskUtilisation1.Value, _
OtherComment1.Value, _
Actions1.Value)
sheet1.activate
sheet1.range("A2").activate
for I = 0 to ubound(myArr)
activecell.value = myArr(I)
activecell.Offset(1,0).activate
Next I

Form validates data before submitting

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

How do I add Value directly below a certain field in Excel VBA?

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