I am trying to add a button command to save data to ta table in excel. I get Error:9: Subscript out of range. I don't know how to fix it. The data entry sheet in my work book is called "Studies"
Here is the code:
Private Sub CommandButtonSave_Click()
Dim iRow As Long
Dim iCode As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Studies")
'find first empty row in database
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
iCode = Me.txtcode.Value
'check for a part number
If Trim(Me.txtcode.Value) = "" Then
Me.txtcode.SetFocus
MsgBox "Please enter study"
Exit Sub
End If
'copy the data to the database
ws.Cells(iRow, 1).Value = Me.txtcode.Value
ws.Cells(iRow, 2).Value = Me.cboreport.Value
ws.Cells(iRow, 3).Value = Me.txttitle.Value
ws.Cells(iRow, 4).Value = Me.cboprincipalinvestigator.Value
ws.Cells(iRow, 5).Value = Me.txtstartdate.Value
ws.Cells(iRow, 6).Value = Me.txtenddate.Value
ws.Cells(iRow, 7).Value = Me.cbotreatment.Value
ws.Cells(iRow, 8).Value = Me.cboSSI.Value
ws.Cells(iRow, 9).Value = Me.cbolotnumber.Value
ws.Cells(iRow, 10).Value = Me.cbodilution.Value
ws.Cells(iRow, 11).Value = Me.cbofrequency.Value
ws.Cells(iRow, 12).Value = Me.cbotmethodofinduction.Value
ws.Cells(iRow, 13).Value = Me.cbostartday.Value
ws.Cells(iRow, 14).Value = Me.cboendday.Value
ws.Cells(iRow, 15).Value = Me.cbodiseasemodel.Value
ws.Cells(iRow, 16).Value = Me.cbodose.Value
ws.Cells(iRow, 17).Value = Me.cbommethodofinduction.Value
ws.Cells(iRow, 18).Value = Me.cbobleedpoints.Value
ws.Cells(iRow, 19).Value = Me.cbotakedownpoints.Value
ws.Cells(iRow, 20).Value = Me.cboimmunecells.Value
ws.Cells(iRow, 21).Value = Me.cbochemotoxins.Value
ws.Cells(iRow, 22).Value = Me.cbocellmarkers.Value
ws.Cells(iRow, 23).Value = Me.cboqpcr.Value
ws.Cells(iRow, 24).Value = Me.cbotissue.Value
ws.Cells(iRow, 25).Value = Me.cboother.Value
ws.Cells(iRow, 26).Value = Me.cbostrain.Value
ws.Cells(iRow, 27).Value = Me.cbosupplier.Value
ws.Cells(iRow, 28).Value = Me.txtdateofbirth.Value
ws.Cells(iRow, 29).Value = Me.txtfindings.Value
ws.Cells(iRow, 30).Value = Me.cbotechnicalissues.Value
ws.Cells(iRow, 31).Value = Me.cbokeyword.Value
ws.Cells(iRow, 32).Value = Me.txtnotes.Value
'clear the data
Me.txtcode.Value = ""
Me.cboreport.Value = ""
Me.txttitle.Value = ""
Me.cboprincipalinvestigator.Value = ""
Me.txtstartdate.Value = ""
Me.txtenddate.Value = ""
Me.cbotreatment.Value = ""
Me.cboSSI.Value = ""
Me.cbolotnumber.Value = ""
Me.cbodilution.Value = ""
Me.cbofrequency.Value = ""
Me.cbotmethodofinduction.Value = ""
Me.cbostartday.Value = ""
Me.cboendday.Value = ""
Me.cbodiseasemodel.Value = ""
Me.cbodose.Value = ""
Me.cbommethodofinduction.Value = ""
Me.cbobleedpoints.Value = ""
Me.cbotakedownpoints.Value = ""
Me.cboimmunecells.Value = ""
Me.cbochemotoxins.Value = ""
Me.cbocellmarkers.Value = ""
Me.cboqpcr.Value = ""
Me.cbotissue.Value = ""
Me.cboother.Value = ""
Me.cbostrain.Value = ""
Me.cbosupplier.Value = ""
Me.txtdateofbirth.Value = ""
Me.txtfindings.Value = ""
Me.cbotechnicalissues.Value = ""
Me.cbokeyword.Value = ""
Me.txtnotes.Value = ""
Me.txtstartdate.Value = Format(Date, "Medium Date")
Me.txtenddate.Value = Format(Date, "Medium Date")
Me.txtdateofbirth.Value = Format(Date, "Medium Date")
Me.txtcode.SetFocus
End Sub
Related
I want to enter data from my userform to a table. I foolishly thought I could do this the same way as when I entered it into a range. EDIT-my script actually does work to add the info to a table. I was mistaken. I also need to start my entry at the first blank cell in column A, and right now I have it set to the first blank row. I appreciate any help! Here is the code I have currently:
Option Explicit
Private Sub cmdfir_Click()
Dim lRow As Long
Dim lPart As Long
Dim ws As Worksheet
Set ws = Worksheets("PQR data")
'find first empty row in database
''lRow = ws.Cells(Rows.Count, 1) _
'' .End(xlUp).Offset(1, 0).Row
'revised code to avoid problems with
'Excel lists and tables in newer versions
lRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
'copy the data to the database
With ws
.Cells(lRow, 1).Value = Me.cbodatecoated.Value
.Cells(lRow, 2).Value = Me.cbodatepacked.Value
.Cells(lRow, 3).Value = Me.cboline.Value
.Cells(lRow, 4).Value = Me.cbocustomer.Value
.Cells(lRow, 5).Value = Me.cbopartnumber.Value
.Cells(lRow, 6).Value = Me.cbopartrev.Value
.Cells(lRow, 7).Value = Me.cbopowdernumberused.Value
.Cells(lRow, 8).Value = Me.cbopowderlotnumber.Value
.Cells(lRow, 9).Value = Me.cboboxnumbers.Value
.Cells(lRow, 10).Value = Me.cbocont.Value
.Cells(lRow, 11).Value = Me.cbolight.Value
.Cells(lRow, 12).Value = Me.cboheavy.Value
.Cells(lRow, 13).Value = Me.cbofisheye.Value
.Cells(lRow, 14).Value = Me.cboseeds.Value
.Cells(lRow, 15).Value = Me.cbodirt.Value
.Cells(lRow, 16).Value = Me.cboorangepeel.Value
.Cells(lRow, 17).Value = Me.cbochemicals.Value
.Cells(lRow, 18).Value = Me.cbospits.Value
.Cells(lRow, 19).Value = Me.cboyarn.Value
.Cells(lRow, 20).Value = Me.cboscratch.Value
.Cells(lRow, 21).Value = Me.cbodrops.Value
.Cells(lRow, 22).Value = Me.cbotabs.Value
.Cells(lRow, 23).Value = Me.cboother.Value
.Cells(lRow, 25).Value = Me.cbometal.Value
.Cells(lRow, 26).Value = Me.cbopackedgood.Value
End With
'clear the data
Me.cboline.Value = 1
Me.cbocustomer.Value = "Gentex"
Me.cbopartnumber.Value = "350-0010-001"
Me.cbopartrev.Value = ""
Me.cbopowdernumberused.Value = "PY"
Me.cbopowderlotnumber.Value = ""
Me.cboboxnumbers.Value = ""
Me.cbocont.Value = ""
Me.cbolight.Value = ""
Me.cboheavy.Value = ""
Me.cbofisheye.Value = ""
Me.cboseeds.Value = ""
Me.cbodirt.Value = ""
Me.cboorangepeel.Value = ""
Me.cbochemicals.Value = ""
Me.cbospits.Value = ""
Me.cboyarn.Value = ""
Me.cboscratch.Value = ""
Me.cbodrops.Value = ""
Me.cbotabs.Value = ""
Me.cboother.Value = ""
Me.cbometal.Value = ""
Me.cbopackedgood.Value = ""
Me.cboline.SetFocus
End Sub
Private Sub UserForm_Click()
End Sub
I have an Excel (365) spreadsheet with two Comboboxes - Combobox1 and Combobox2 both are used to list the same data for different purposes on different tabs. My problem is that although I have changed listrows in both to the same number (20) only Combobox2 shows 20 rows. Combobox1 only shows the previous number of rows (15) although set to display 20. Anyone know how to get it to behave properly?
EDIT I have solved my origional problem but I would however be interested in using VBA to automatically update both listfillrange and listrows when a new row of data is added. I do have a macro that I use to add new data to my data table that could be adapted to also update listfillrange and listrows
Sub add_to_table_sa_3()
'Written by Keith Cooper 27/10/2021
Dim NewRow As Integer
NewRow = Worksheets("input").Range("E1").Value + 1
If Worksheets("input").Range("F1").Value <> 0 Then
MsgBox "There are errors. No data has been added!", vbOKOnly, "Warning!"
Exit Sub
End If
Worksheets("Data").Cells(NewRow, 1).Value = Worksheets("input").Range("B3").Value
Worksheets("Data").Cells(NewRow, 2).Value = Worksheets("input").Range("B4").Value
Worksheets("Data").Cells(NewRow, 3).Value = Worksheets("input").Range("B5").Value
Worksheets("Data").Cells(NewRow, 4).Value = Worksheets("input").Range("B6").Value
Worksheets("Data").Cells(NewRow, 5).Value = Worksheets("input").Range("B7").Value
Worksheets("Data").Cells(NewRow, 6).Value = Worksheets("input").Range("B8").Value
Worksheets("Data").Cells(NewRow, 7).Value = Worksheets("input").Range("B9").Value
Worksheets("Data").Cells(NewRow, 8).Value = Worksheets("input").Range("B10").Value
Worksheets("Data").Cells(NewRow, 9).Value = Worksheets("input").Range("B11").Value
Worksheets("Data").Cells(NewRow, 10).Value = Worksheets("input").Range("B12").Value
Worksheets("Data").Cells(NewRow, 11).Value = Worksheets("input").Range("B13").Value
Worksheets("Data").Cells(NewRow, 12).Value = Worksheets("input").Range("B14").Value
Worksheets("Data").Cells(NewRow, 13).Value = Worksheets("input").Range("B15").Value
Worksheets("Data").Cells(NewRow, 14).Value = Worksheets("input").Range("B16").Value
Worksheets("Data").Cells(NewRow, 15).Value = Worksheets("input").Range("B17").Value
Worksheets("Data").Cells(NewRow, 16).Value = Worksheets("input").Range("B18").Value
Worksheets("Data").Cells(NewRow, 17).Value = Worksheets("input").Range("B19").Value
Worksheets("Data").Cells(NewRow, 18).Value = Worksheets("input").Range("B20").Value
Worksheets("Data").Cells(NewRow, 19).Value = Worksheets("input").Range("B21").Value
Worksheets("Data").Cells(NewRow, 20).Value = Worksheets("input").Range("B22").Value
Worksheets("Data").Cells(NewRow, 21).Value = Worksheets("input").Range("B23").Value
Worksheets("Data").Cells(NewRow, 22).Value = Worksheets("input").Range("B24").Value
Worksheets("Data").Cells(NewRow, 23).Value = Worksheets("input").Range("B25").Value
Worksheets("Data").Cells(NewRow, 24).Value = Worksheets("input").Range("B26").Value
Worksheets("Data").Cells(NewRow, 25).Value = Worksheets("input").Range("B27").Value
Worksheets("Data").Cells(NewRow, 26).Value = Worksheets("input").Range("B28").Value
Worksheets("Data").Cells(NewRow, 27).Value = Worksheets("input").Range("B29").Value
Worksheets("Data").Cells(NewRow, 28).Value = Worksheets("input").Range("B30").Value
Worksheets("Data").Cells(NewRow, 29).Value = Worksheets("input").Range("B31").Value
Worksheets("Data").Cells(NewRow, 30).Value = Worksheets("input").Range("B32").Value
Worksheets("Data").Cells(NewRow, 31).Value = Worksheets("input").Range("B33").Value
Worksheets("Data").Cells(NewRow, 32).Value = Worksheets("input").Range("B34").Value
Worksheets("Data").Cells(NewRow, 33).Value = Worksheets("input").Range("B35").Value
Worksheets("Data").Cells(NewRow, 34).Value = Worksheets("input").Range("B36").Value
Worksheets("Data").Cells(NewRow, 35).Value = Worksheets("input").Range("B37").Value
'Range("B38") is a heading
Worksheets("Data").Cells(NewRow, 36).Value = Worksheets("input").Range("B39").Value
Worksheets("Data").Cells(NewRow, 37).Value = Worksheets("input").Range("B40").Value
Worksheets("Data").Cells(NewRow, 38).Value = Worksheets("input").Range("B41").Value
Worksheets("Data").Cells(NewRow, 39).Value = Worksheets("input").Range("B42").Value
Worksheets("Data").Cells(NewRow, 40).Value = Worksheets("input").Range("B43").Value
Worksheets("Data").Cells(NewRow, 41).Value = Worksheets("input").Range("B44").Value
Worksheets("Data").Cells(NewRow, 42).Value = Worksheets("input").Range("B45").Value
Worksheets("Data").Cells(NewRow, 43).Value = Worksheets("input").Range("B46").Value
Worksheets("Data").Cells(NewRow, 44).Value = Worksheets("input").Range("B47").Value
Worksheets("Data").Cells(NewRow, 45).Value = Worksheets("input").Range("B48").Value
Worksheets("Data").Cells(NewRow, 46).Value = Worksheets("input").Range("B49").Value
Worksheets("Data").Cells(NewRow, 47).Value = Worksheets("input").Range("B50").Value
Worksheets("Data").Cells(NewRow, 48).Value = Worksheets("input").Range("B51").Value
Worksheets("Data").Cells(NewRow, 49).Value = Worksheets("input").Range("B52").Value
Worksheets("Data").Cells(NewRow, 50).Value = Worksheets("input").Range("B53").Value
Worksheets("Data").Cells(NewRow, 51).Value = Worksheets("input").Range("B54").Value
Worksheets("Data").Cells(NewRow, 52).Value = Worksheets("input").Range("B55").Value
Sheets("Input").Select
Range("C2").Value = "Data added"
MsgBox "Data added", vbOKOnly, "Transfer Data"
Worksheets("input").Range("E1").Value = NewRow
Worksheets("input").Range("B3").Select
End Sub
Consider replacing the 52 lines transposing the data with one line inside a For/Next loop
Option Explicit
Sub add_to_table_sa_3()
Dim NewRow As Long, i as Long, arData
With Sheets("Input")
If .Range("F1").Value <> 0 Then
MsgBox "There are errors. No data has been added!", vbOKOnly, "Warning!"
Exit Sub
End If
arData = .Range("B3:B55").Value
NewRow = .Range("E1").Value + 1
End With
With Sheets("Data")
For i = 1 To 35
.Cells(NewRow, i) = arData(i, 1)
Next
'Range("B38") is a heading
For i = 37 To 65
.Cells(NewRow, i - 1) = arData(i, 1)
Next
'Range("B68") is a heading
For i = 67 To UBound(arData)
.Cells(NewRow, i - 2) = arData(i, 1)
Next
Sheet1.ComboBox1.ListFillRange = "'" & .Name & "'!A2:A" & NewRow
Sheet6.ComboBox2.ListFillRange = "'" & .Name & "'!A2:A" & NewRow
End With
With Sheets("Input")
.Range("C2").Value = "Data added"
.Range("E1").Value = NewRow
.Activate
.Range("B3").Select
End With
MsgBox "Data added row " & NewRow, vbOKOnly, "Transfer Data"
End Sub
I am a complete novice at Excel VBA and I am currently attempting a project on Excel VBA. I have created a UserForm that would allow the user to enter data onto the Excel Sheet by completing the fields in the UserForm. I have tested all the codes individually and they have worked fine.
For the user to access the UserForm, I have added an ActiveX Command Button on a separate sheet on the same workbook. However, when accessing the UserForm from the ActiveX Command Button, some of the codes do not run (mainly the code that flags out the duplicate entry, as well as the code that generates serial numbers).
Where did I go wrong in my code?
This is my code to adding new data as well as the code to flag out duplicate entries. When opening the UserForm from the ActiveX Command Button, adding new data works fine but it does not flag out duplicate entries in the data. (However, testing the code itself in VBA works perfectly fine).
Private Sub cmdAddNewCustomer_Click()
Dim count As Long
Dim lastrow As Long
Dim lCustomerID As String
Dim ws As Worksheet
Set ws = Worksheets("Customer Data")
'find first empty row in database
lrow = ws.Cells.Find(what:="*", searchorder:=xlRows, _
Searchdirection:=xlPrevious, LookIn:=xlValues).Row + 1
lCustomerID = txtCustomerID
count = 0
With ws
For currentrow = 1 To lrow
If lCustomerID = Cells(currentrow, 1) Then
count = count + 1
End If
If count > 1 Then
.Cells(currentrow, 1).Value = ""
.Cells(currentrow, 2).Value = ""
.Cells(currentrow, 3).Value = ""
.Cells(currentrow, 4).Value = ""
.Cells(currentrow, 5).Value = ""
.Cells(currentrow, 6).Value = ""
.Cells(currentrow, 7).Value = ""
.Cells(currentrow, 8).Value = ""
.Cells(currentrow, 9).Value = ""
.Cells(currentrow, 10).Value = ""
.Cells(currentrow, 11).Value = ""
.Cells(currentrow, 12).Value = ""
.Cells(currentrow, 13).Value = ""
.Cells(currentrow, 14).Value = ""
MsgBox ("CustomerID already exists!")
End If
If count = 0 Then
.Cells(lrow, 1).Value = Me.txtCustomerID.Value
.Cells(lrow, 2).Value = Me.txtCustomerName.Value
.Cells(lrow, 3).Value = Me.cboCustomerStatus.Value
.Cells(lrow, 4).Value = Me.txtContactPerson.Value
.Cells(lrow, 5).Value = Me.cboDepartment.Value
.Cells(lrow, 6).Value = Me.txtPosition.Value
.Cells(lrow, 7).Value = Me.cboRoleType.Value
.Cells(lrow, 8).Value = Me.txtofficeHP1.Value
.Cells(lrow, 9).Value = Me.txtOfficeHP2.Value
.Cells(lrow, 10).Value = Me.txtMobileHP1.Value
.Cells(lrow, 11).Value = Me.txtMobileHP2.Value
.Cells(lrow, 12).Value = Me.txtEmail1.Value
.Cells(lrow, 13).Value = Me.txtEmail2.Value
.Cells(lrow, 14).Value = Me.txtEmail3.Value
End If
Next currentrow
End With
'clear the data
Me.txtCustomerName.Value = ""
Me.cboCustomerStatus.Value = ""
Me.txtContactPerson.Value = ""
Me.cboDepartment.Value = ""
Me.txtPosition.Value = ""
Me.cboRoleType.Value = ""
Me.txtofficeHP1.Value = ""
Me.txtOfficeHP2.Value = ""
Me.txtMobileHP1.Value = ""
Me.txtMobileHP2.Value = ""
Me.txtEmail1.Value = ""
Me.txtEmail2.Value = ""
Me.txtEmail3.Value = ""
End Sub
This is the code to generate serial numbers. (Same problem, does not work when accessed via ActiveX Command Button but works fine when tested individually in VBA).
Sub FindCustomerID()
Dim lastrow
Dim lastnum As Long
Dim ws As Worksheet
Set ws = Worksheets("Customer Data")
If Me.cboCountry = "" Or Me.txtCustomerName = "" Then
Exit Sub
End If
serialno = 1
lastrow = ws.Cells(Rows.count, 1).End(xlUp).Row
CountryCode = UCase(Left(Me.cboCountry, 3))
CustomerCode = UCase(Left(Me.txtCustomerName, 10))
'assemble them into CustomerID
CustomerID = CountryCode & CustomerCode & serialno
For currentrow = 2 To lastrow
If CustomerID = Cells(currentrow, 1) Then
'find last number that applies
serialno = serialno + 1
End If
're-assign customerID with new serial number
CustomerID = CountryCode & CustomerCode & serialno
Next currentrow
Me.lblCustomerID = CustomerID
End Sub
And lastly, this is the code from the ActiveX Command Button that brings out the UserForm.
Private Sub cmdNCustomerData_Click()
frmCustomerdata.Show
End Sub
The cause of the problem you described is a missing . to qualify Cells(currentrow, 1). Because you added the ActiveX button to a different sheet, the line
If lCustomerID = Cells(currentrow, 1) Then
accesses Cells(currentrow, 1) of that sheet. To fix this the range needs to be qualified with a . to become
If lCustomerID = .Cells(currentrow, 1) Then
I would also take
If count = 0 Then
.
.
.
End If
outside the loop. You are repeating these lines many times unnecessarily.
The first block of code then becomes:
Private Sub cmdAddNewCustomer_Click()
Dim count As Long
Dim lastrow As Long
Dim lCustomerID As String
Dim ws As Worksheet
Set ws = Worksheets("Customer Data")
'find first empty row in database
lrow = ws.Cells.Find(what:="*", searchorder:=xlRows, _
Searchdirection:=xlPrevious, LookIn:=xlValues).Row + 1
lCustomerID = txtCustomerID
count = 0
With ws
' Count backward to delete rows completely
For currentrow = lrow - 1 To 1 Step -1
If lCustomerID = .Cells(currentrow, 1) Then
count = count + 1
End If
If count > 1 Then
.Cells(currentrow, 1).Resize(1, 14).ClearContents
' Uncomment the following line to delete the whole row completely
'.Rows(currentrow).Delete
End If
Next currentrow
If count > 1 Then
MsgBox (count - 1 " duplicates of CustomerID found and cleared!")
ElseIf count = 0 Then
.Cells(lrow, 1).Value = Me.txtCustomerID.Value
.Cells(lrow, 2).Value = Me.txtCustomerName.Value
.Cells(lrow, 3).Value = Me.cboCustomerStatus.Value
.Cells(lrow, 4).Value = Me.txtContactPerson.Value
.Cells(lrow, 5).Value = Me.cboDepartment.Value
.Cells(lrow, 6).Value = Me.txtPosition.Value
.Cells(lrow, 7).Value = Me.cboRoleType.Value
.Cells(lrow, 8).Value = Me.txtofficeHP1.Value
.Cells(lrow, 9).Value = Me.txtOfficeHP2.Value
.Cells(lrow, 10).Value = Me.txtMobileHP1.Value
.Cells(lrow, 11).Value = Me.txtMobileHP2.Value
.Cells(lrow, 12).Value = Me.txtEmail1.Value
.Cells(lrow, 13).Value = Me.txtEmail2.Value
.Cells(lrow, 14).Value = Me.txtEmail3.Value
End If
End With
'clear the data
Me.txtCustomerName.Value = ""
Me.cboCustomerStatus.Value = ""
Me.txtContactPerson.Value = ""
Me.cboDepartment.Value = ""
Me.txtPosition.Value = ""
Me.cboRoleType.Value = ""
Me.txtofficeHP1.Value = ""
Me.txtOfficeHP2.Value = ""
Me.txtMobileHP1.Value = ""
Me.txtMobileHP2.Value = ""
Me.txtEmail1.Value = ""
Me.txtEmail2.Value = ""
Me.txtEmail3.Value = ""
End Sub
In the FindCustomerID subroutine you have exactly the same problem with the line
If CustomerID = Cells(currentrow, 1) Then
as Cells(currentrow, 1) is not qualified and therefore, should become
If CustomerID = ws.Cells(currentrow, 1) Then
You are also reassigning the CustomerID many times unnecessarily. I would take the reassignment inside the If statement and the loop will become
For currentrow = 2 To lastrow
If CustomerID = ws.Cells(currentrow, 1) Then
'find last number that applies
serialno = serialno + 1
're-assign customerID with new serial number
CustomerID = CountryCode & CustomerCode & serialno
End If
Next currentrow
This way CustomerID is only reassigned if and only if serialno changes.
I have a workbook with monthly worksheets. One for Emails and one for Calls and I have created two userForms for data entry, one for Emails and one for Calls.
The forms do the job and they enter date in the right place but if I have selected the "August 18 Email" sheet and use the Email form, once the form is submitted it jumps to display the "August 18 Calls" sheet.
I just want it to stay in the selected worksheet, in this case "August 18 Email".
The code for the Emails form is the one below and the one for the Calls is nearly the same but only changing this line : Set ws = Sheets(Format(Date, "mmmm yy") & " calls")
Private Sub CommandButton2_Click()
Dim lRow As Long
Dim ws As Worksheet
Set ws = ActiveSheet
Set ws = Sheets(Format(Date, "mmmm yy") & " emails")
lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
With ws
If Me.txtDateBox.Value = "" Then
.Cells(lRow, 1).Value = Format(Date, "dd/mmm/yy")
Else
.Cells(lRow, 1).Value = Me.txtDateBox.Value
End If
myVar = ""
For x = 0 To Me.ListBox2.ListCount - 1
If Me.ListBox2.Selected(x) Then
If myVar = "" Then
myVar = Me.ListBox2.List(x, 0)
Else
myVar = myVar & "," & Me.ListBox2.List(x, 0)
End If
End If
Next x
.Cells(lRow, 11).Value = myVar
myVarSign = ""
For x = 0 To Me.ListBox3.ListCount - 1
If Me.ListBox3.Selected(x) Then
If myVarSign = "" Then
myVarSign = Me.ListBox3.List(x, 0)
Else
myVarSign = myVarSign & "," & Me.ListBox3.List(x, 0)
End If
End If
Next x
.Cells(lRow, 12).Value = myVarSign
myVarTheme = ""
For x = 0 To Me.ListBox1.ListCount - 1
If Me.ListBox1.Selected(x) Then
If myVarTheme = "" Then
myVarTheme = Me.ListBox1.List(x, 0)
Else
myVarTheme = myVarTheme & "," & Me.ListBox1.List(x, 0)
End If
End If
Next x
.Cells(lRow, 14).Value = myVarTheme
.Cells(lRow, 2).Value = Me.Time.Value
.Cells(lRow, 3).Value = Me.ComboBox1.Value
.Cells(lRow, 4).Value = Me.ComboBox2.Value
.Cells(lRow, 5).Value = Me.ComboBox3.Value
.Cells(lRow, 6).Value = Me.ComboBox4.Value
.Cells(lRow, 7).Value = Me.ComboBox5.Value
.Cells(lRow, 8).Value = Me.ComboBox15.Value
.Cells(lRow, 9).Value = Me.ComboBox6.Value
.Cells(lRow, 10).Value = Me.ComboBox7.Value
.Cells(lRow, 13).Value = Me.ComboBox11.Value
.Cells(lRow, 15).Value = Me.ComboBox16.Value
.Cells(lRow, 16).Value = Me.TextBox2.Value
End With
Me.txtDateBox.Value = ""
Me.Time.Value = ""
Me.ComboBox1.Value = ""
Me.ComboBox2.Value = ""
Me.ComboBox3.Value = ""
Me.ComboBox4.Value = ""
Me.ComboBox5.Value = ""
Me.ComboBox6.Value = ""
Me.ComboBox7.Value = ""
Me.ComboBox11.Value = ""
Me.ComboBox16.Value = ""
Me.ComboBox15.Value = ""
Me.TextBox2.Value = ""
Dim iCount As Integer
For iCount = 0 To Me!ListBox1.ListCount
Me!ListBox1.Selected(iCount) = False
Next iCount
For iCount = 0 To Me!ListBox2.ListCount
Me!ListBox2.Selected(iCount) = False
Next iCount
For iCount = 0 To Me!ListBox3.ListCount
Me!ListBox3.Selected(iCount) = False
Next iCount
End Sub
It could be improved a lot but I am happy if after submission the worksheet in view stays instead to jumping to another one.
As you can see I am only beginning (I have managed to create this with help of others).
If you remove any instances of .Select or .Activate on worksheet, range, or cell objects, your sheet shouldn't change.
If that is not an option, another solution would be to note what sheet you are on when the code is called and then Activate that sheet before ending your sub. Since we do not see all of the userform code, you will have to strategically decide where this goes (as mentioned by #K.Davis, nothing shown switches the sheet so it must be happening in some other code).
When the macro/userform is launched:
Dim StartSheet as Worksheet
Set StartSheet = ActiveSheet
Then, before exiting macro/userform:
StartSheet.Activate
You may have to pass this along as a parameter depending on how your code is structured.
I don't know how to program in this language so I have to rely on Google. Taken bits from here and there and I suspect that I have two puzzles that don't fit at the moment. I am getting runtime error 9 subscript out of range because of this line:
ThisWorkbook.Sheets("Sheet4").Range("c2").End(xlDown).Select = myVar
Here's my code:
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Skráningar")
'Taeki gögn í gagnagrunn
myVar = ""
For X = 0 To Me.taeki.ListCount - 1
If Me.taeki.Selected(X) Then
If myVar = "" Then
myVar = Me.taeki.List(X, 0)
Else
myVar = myVar & "," & Me.taeki.List(X, 0)
End If
End If
Next X
ThisWorkbook.Sheets("Sheet4").Range("c2").End(xlDown).Select = myVar
'find first empty row in database
'iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _ SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
iRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row + 1
'check for a Name number
If Trim(Me.timutbox.Value) = "" Then
Me.dagsbox.SetFocus
MsgBox "Vinsamlega skráðu hversu lengi tækið var í notkun"
Exit Sub
End If
Me.Hide
'copy the data to the database
ws.Cells(iRow, 1).Value = Me.dagsbox.Value
ws.Cells(iRow, 2).Value = Me.timutbox.Value
ws.Cells(iRow, 5).Value = Me.sandbox.Value
ws.Cells(iRow, 6).Value = Me.vedurbox.Value
ws.Cells(iRow, 8).Value = Me.bilunbox.Value
ws.Cells(iRow, 7).Value = Me.athbox.Value
ws.Cells(iRow, 3).Value = Me.taeki.Value
ws.Cells(iRow, 4).Value = Me.svaedi.Value
MsgBox "Data added", vbOKOnly + vbInformation, "Data Added"
'clear the data
Me.dagsbox.Value = ""
Me.timutbox.Value = ""
Me.sandbox.Value = ""
Me.vedurbox.Value = ""
Me.bilunbox.Value = ""
Me.athbox.Value = ""
Me.taeki.Value = ""
Me.svaedi.Value = ""
Me.dagsbox.SetFocus
I think I understand what you are trying to do:
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Skráningar")
'Taeki gögn í gagnagrunn
myVar = ""
For X = 0 To Me.taeki.ListCount - 1
If Me.taeki.Selected(X) Then
If myVar = "" Then
myVar = Me.taeki.List(X, 0)
Else
myVar = myVar & "," & Me.taeki.List(X, 0)
End If
End If
Next X
'find first empty row in database
'iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _ SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
iRow = ws.range("A" & rows.count).End(xlUp).Row + 1
ws.Range("C" & iRow).value = myVar
'check for a Name number
If Trim(Me.timutbox.Value) = "" Then
Me.dagsbox.SetFocus
MsgBox "Vinsamlega skráðu hversu lengi tækið var í notkun"
Exit Sub
End If
Me.Hide
'copy the data to the database
ws.Cells(iRow, 1).Value = Me.dagsbox.Value
ws.Cells(iRow, 2).Value = Me.timutbox.Value
ws.Cells(iRow, 5).Value = Me.sandbox.Value
ws.Cells(iRow, 6).Value = Me.vedurbox.Value
ws.Cells(iRow, 8).Value = Me.bilunbox.Value
ws.Cells(iRow, 7).Value = Me.athbox.Value
ws.Cells(iRow, 3).Value = Me.taeki.Value
ws.Cells(iRow, 4).Value = Me.svaedi.Value
MsgBox "Data added", vbOKOnly + vbInformation, "Data Added"
'clear the data
Me.dagsbox.Value = ""
Me.timutbox.Value = ""
Me.sandbox.Value = ""
Me.vedurbox.Value = ""
Me.bilunbox.Value = ""
Me.athbox.Value = ""
Me.taeki.Value = ""
Me.svaedi.Value = ""
Me.dagsbox.SetFocus
EDIT:
I have changed how iRow is calculated, and removed reference to sheet4
Does that do what you need?
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Skráningar")
'Taeki gögn í gagnagrunn
myVar = ""
For X = 0 To Me.taeki.ListCount - 1
If Me.taeki.Selected(X) Then
If myVar = "" Then
myVar = Me.taeki.List(X, 0)
Else
myVar = myVar & "," & Me.taeki.List(X, 0)
End If
End If
Next X
'find first empty row in database
'iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _ SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
iRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1
ws.Range("C" & iRow).Value = myVar
'check for a Name number
If Trim(Me.timutbox.Value) = "" Then
Me.dagsbox.SetFocus
MsgBox "Vinsamlega skráðu hversu lengi tækið var í notkun"
Exit Sub
End If
Me.Hide
'copy the data to the database
ws.Cells(iRow, 1).Value = Me.dagsbox.Value
ws.Cells(iRow, 2).Value = Me.timutbox.Value
ws.Cells(iRow, 5).Value = Me.sandbox.Value
ws.Cells(iRow, 6).Value = Me.vedurbox.Value
ws.Cells(iRow, 8).Value = Me.bilunbox.Value
ws.Cells(iRow, 7).Value = Me.athbox.Value
ws.Cells(iRow, 3).Value = Me.taeki.Value
ws.Cells(iRow, 4).Value = Me.svaedi.Value
MsgBox "Data added", vbOKOnly + vbInformation, "Data Added"
'clear the data
Me.dagsbox.Value = ""
Me.timutbox.Value = ""
Me.sandbox.Value = ""
Me.vedurbox.Value = ""
Me.bilunbox.Value = ""
Me.athbox.Value = ""
Me.taeki.Value = ""
Me.svaedi.Value = ""
Me.dagsbox.SetFocus
This is the current code.