Populating UserForm Data to an Excel Table - excel

I have created a UserForm as shown by the image. The data from this has to be populated to an excel table named 'Database'. I have copied various VBA Codes from the internet to populate the table but all in vain. The code is as follows:
Sub ResetItem() ' The reset button coding
Sub Submit_Data()
Dim iRow As Long
If adminpanel.txtRowNumber.Value = "" Then
iRow = student.Range("A" & Rows.Count).End(xlUp).Row + 1
Else
iRow = adminpanel.txtRowNumber.Value
End If
With student.Range("A" & iRow)
.Offset(0, 0).Value = "=Row()-1"
.Offset(0, 1).Value = UserForm1.Doc_number.Value
.Offset(0, 2).Value = UserForm1.DocDate.Value
.Offset(0, 3).Value = UserForm1.Order_Number.Value
.Offset(0, 4).Value = UserForm1.Fleet_number.Value
.Offset(0, 5).Value = UserForm1.Maitenance_Type.Value
.Offset(0, 6).Value = UserForm1.ROF.Value
.Offset(0, 7).Value = UserForm1.System_Type.Value
.Offset(0, 8).Value = UserForm1.Asy_Type.Value
.Offset(0, 9).Value = UserForm1.Comments.Value
.Offset(0, 10).Value = UserForm1.OEM.Value
.Offset(0, 11).Value = UserForm1.Part_Number.Value
.Offset(0, 12).Value = UserForm1.SAP_Code.Value
.Offset(0, 13).Value = UserForm1.Unit.Value
.Offset(0, 14).Value = UserForm1.Start_Time.Value
.Offset(0, 15).Value = UserForm1.Finish_Time.Value
.Offset(0, 16).Value = UserForm1.Tech01.Value
.Offset(0, 17).Value = UserForm1.Tech02.Value
.Offset(0, 18).Value = UserForm1.Tech03.Value
.Offset(0, 19).Value = UserForm1.Distance.Value
End With
Call Reset_Form
Application.ScreenUpdating = True
MsgBox "Done"
End With
End Sub

Following code assumes that student is a name of a worksheet.
Sub Submit_Data()
Dim student As Worksheet
Dim iRow As Long
Set student = ThisWorkbook.Worksheets("student") 'assuming sheet name is student
If IsNumeric(adminpanel.txtRowNumber.Value) Then
iRow = CLng(adminpanel.txtRowNumber.Value)
Else
iRow = student.Cells(student.Rows.Count, "A").End(xlUp).Row + 1
End If
With UserForm1
student.Cells(iRow, 1).Formula = "=ROW() - 1"
student.Cells(iRow, 2).Resize(1, 19).Value2 = Array( _
.Doc_number.Value, _
.DocDate.Value, _
.Order_Number.Value, _
.Fleet_number.Value, _
.Maitenance_Type.Value, _
.ROF.Value, _
.System_Type.Value, _
.Asy_Type.Value, _
.Comments.Value, _
.OEM.Value, _
.Part_Number.Value, _
.SAP_Code.Value, _
.Unit.Value, _
.Start_Time.Value, _
.Finish_Time.Value, _
.Tech01.Value, _
.Tech02.Value, _
.Tech03.Value, _
.Distance.Value _
)
End With
Call Reset_Form
'Application.ScreenUpdating = True no need for it
MsgBox "Done"
Set student = Nothing
End Sub

Related

Why am I getting object error 91 when I use printform function in vba

Im having a problem where when I am openning up a userform and printing it out as well as printing out an excel sheet is gives me the error
Object error 91
This is the code for the command button that starts loads the Userform and unloads the current userform
Private Sub CommandButton1_Click()
Cells(10, 2).Value = stageBox.Value
Cells(11, 2).Value = materialBox.Value
Cells(12, 2).Value = POBox.Value
Cells(13, 2).Value = MPIBox.Value
Cells(14, 2).Value = LPIBox.Value
Unload Me
UserForm1.Show
End Sub
After it opens it it runs this
Dim Batch As String
Dim Draw As String
Dim Rev As String
Dim ScrapQR As String
Dim goodQty As String
Dim desc As String
Dim scrapQty As String
Dim stage As String
Dim material As String
Dim po As String
Dim mpi As String
Dim lpi As String
Sub UserForm_Initialize()
Call gatherData
Call popData
Call printStuff
Unload Me
End Sub
Sub gatherData()
Batch = Cells(1, 2).Value
Draw = Cells(3, 2).Value
Rev = Cells(4, 2).Value
ScrapQR = Cells(8, 2).Value
goodQty = Cells(6, 2).Value
scrapQty = Cells(7, 2).Value
desc = Cells(2, 2).Value
stage = Cells(10, 2).Value
material = Cells(11, 2).Value
po = Cells(12, 2).Value
mpi = Cells(13, 2).Value
lpi = Cells(14, 2).Value
End Sub
Sub popData()
BatchBox.Text = Batch
DrawBox.Text = Draw
DescBox.Text = desc
RevBox.Text = Rev
GoodBox.Text = goodQty
ScrapBox.Text = scrapQty
DateBox = Date
QRBox.Text = ScrapQR
sBox.Text = Cells(10, 2).Value
matBox.Text = Cells(11, 2).Value
PBox.Text = Cells(12, 2).Value
MBox.Text = Cells(13, 2).Value
LBox.Text = Cells(14, 2).Value
End Sub
Sub printStuff()
Dim fileName As String
fileName = "Surface_Finish_" & Batch
'Print Sheet1 exactly one page wide and tall
With Worksheets("Surface Finish").PageSetup
.Zoom = False
.FitToPagesTall = 1
.FitToPagesWide = 1
End With
Worksheets("Surface Finish").ExportAsFixedFormat _
Type:=xlTypePDF, _
fileName:="N:\Buckets\Sdata\Dyllon_Dunton\" & fileName, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
From:=1, _
To:=1, _
OpenAfterPublish:=False
PrintForm
End Sub
Any help would be greatly appreciated
You can not use unload command until you use .show, please remove this one.
Private Sub CommandButton1_Click()
Cells(10, 2).Value = stageBox.Value
Cells(11, 2).Value = materialBox.Value
Cells(12, 2).Value = POBox.Value
Cells(13, 2).Value = MPIBox.Value
Cells(14, 2).Value = LPIBox.Value
Unload Me ' this one..................................
UserForm1.Show
End Sub

Why ActiveX Command button does not run all codes in my VBA UserForm?

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.

Userform to search for two criteria, then paste row's data to userform textboxes

I am getting a run-time error '13': type mismatch for one of the lines marked below. I want to be able to have a userform that you can type two criteria into, then it will search for the row that has both of those criteria and paste the corresponding cells' values to the 11 userform textboxes. I'm not sure why it is giving me an error for this line, or if there is a better way to do this.
Private Sub CommandButton1_Click()
txt1.Visible = True
txt2.Visible = True
txt3.Visible = True
txt4.Visible = True
txt5.Visible = True
txt6.Visible = True
txt7.Visible = True
txt8.Visible = True
txt9.Visible = True
txt10.Visible = True
txt11.Visible = True
Dim ws As Worksheet
Set ws = Sheets("The Goods")
ws.Activate
Dim SearchSearch As Variant
SearchSearch = txtsearch.Value
Dim SearchName As Variant
SearchName = txtname.Value
If Trim(txtsearch.Value) = "" Then
MsgBox "Search can't be left blank.", vbOKOnly + vbInformation, "Search"
End If
If Trim(txtname.Value) = "" Then
MsgBox "Name can't be left blank.", vbOKOnly + vbInformation, "Name"
End If
Dim FirstAddress As String, cF As Range
With ThisWorkbook.Sheets("The Goods").Range("D:D") 'txtsearch will be in the range D:D
Set cF = .Find(What:=SearchSearch, _
after:=ActiveCell, _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False) ' line that is giving me an error
With ThisWorkbook.Sheets("The Goods").Range("B:B") 'txtname will be in the range B:B
Set cF = .Find(What:=SearchName, _
after:=ActiveCell, _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
txt1.Value = cF.(0, 5).Value
txt2.Value = cF(0, 3).Value
txt3.Value = cF(0, 6).Value
txt4.Value = cF(0, 7).Value
txt5.Value = cF(0, 8).Value
txt6.Value = cF(0, 9).Value
txt7.Value = cF(0, 10).Value
txt8.Value = cF(0, 11).Value
txt9.Value = cF(0, 12).Value
txt10.Value = cF(0, 13).Value
txt11.Value = cF(0, 14).Value
End With
End With
End Sub
Private Sub CommandButton3_Click()
Dim iExit As VbMsgBoxResult
iExit = MsgBox("Are you sure you want to exit?", vbQuestion + vbYesNo, "Search System")
If iExit = vbYes Then
Unload Me
End If
End Sub
The code below is a simple For Loop, which loops through each cel in Column B and checks for txtname.Value, and using offset to check if Column D value is equal to txtsearch.Value. If both match, then it will write the values for that row into the userform text boxes. You can change the TextBox1 to txt1, etc.
Private Sub CommandButton1_Click()
Dim ws As Worksheet, cel As Range
Set ws = Sheets("The Goods")
For Each cel In ws.Cells(2, 2).Resize(ws.Cells(Rows.Count, 2).End(xlUp).Row).Cells
If cel.Value = Me.txtname.Value And cel.Offset(, 2).Value = Me.txtsearch.Value Then
Me.TextBox1.Value = cel.Offset(, 3).Value 'Change to your textbox naming scheme
Me.TextBox2.Value = cel.Offset(, 1).Value
Me.TextBox3.Value = cel.Offset(, 4).Value
Me.TextBox4.Value = cel.Offset(, 5).Value
Me.TextBox5.Value = cel.Offset(, 6).Value
Me.TextBox6.Value = cel.Offset(, 7).Value
Me.TextBox7.Value = cel.Offset(, 8).Value
Me.TextBox8.Value = cel.Offset(, 9).Value
Me.TextBox9.Value = cel.Offset(, 10).Value
Me.TextBox10.Value = cel.Offset(, 11).Value
Me.TextBox11.Value = cel.Offset(, 12).Value
End If
Next cel
End Sub
I would go with something like this:
Private Sub CommandButton1_Click()
Dim i As Long, rngB As Range, n As Long, arrB, arrD
Dim ws As Worksheet
Dim SearchSearch As Variant, SearchName As Variant
For i = 1 To 11
Me.Controls("txt" & i).Visible = True
Next i
Set ws = ThisWorkbook.Sheets("The Goods")
ws.Parent.Activate
ws.Activate
SearchSearch = Trim(txtsearch.Value)
SearchName = Trim(txtname.Value)
'check the trimmed values
If Len(SearchSearch) = 0 Or Len(SearchName) = 0 Then
MsgBox "'Search' and 'Name' can't be left blank.", vbOKOnly + vbInformation, "Search"
Exit Sub
End If
'get search ranges
Set rngB = ws.Range(ws.Range("B1"), ws.Cells(ws.Rows.Count, "B").End(xlUp))
Set rngD = rngB.Offset(0, 2)
'pull the values into a couple of arrays for faster searching
arrB = rngB.Value
arrD = rngD.Value
'loop over the arrays
For n = 1 To UBound(arrB, 1)
If arrB(n, 1) = SearchName And arrD(n, 1) = SearchSearch Then
'got a hit - populate your textboxes
Set cF = rngB.Cells(n, 1)
txt1.Value = cF.Offset(0, 1).Value 'Col C same row
txt2.Value = cF.Offset(0, 2).Value 'Col D same row
txt3.Value = cF.Offset(0, 3).Value 'Col E same row
'etc etc
'OR do something like this:
With rngB.Cells(n, 1).EntireRow
txt1.Value = .Cells(1, "C").Value
txt1.Value = .Cells(1, "D").Value
txt1.Value = .Cells(1, "E").Value
'etc etc
End With
Exit For
End If
Next
If cF Is Nothing Then MsgBox "No match!"
End Sub

Staying on the active worksheet

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.

Copy-paste cell values to other sheets

I am trying to put together some codes that I found here and there to build up a small inventory, sales program. I am stuck at a point where the customer basket is finalized and sold items in the basket should be saved in relevant sheets.
As an example,basket data is in sheet1 (A4:g22), needs to be written to sheet2 and sheet3 with finding the first empty cell in column A. Thank you very much for your help in advance.
Private Sub EKSKAYDET_Click()
If Not IsNumeric(Me.eksmiktartxt.Value) Then
MsgBox "Miktari Kontrol Ediniz!"
Me.eksmiktartxt.SetFocus
Exit Sub
End If
If Not IsNumeric(Me.eksreznobox.Value) Then
MsgBox "ÜRÜN KODUNU Kontrol Ediniz!"
Me.eksreznobox.SetFocus
Exit Sub
End If
If eksreznobox.Value = "" Then
MsgBox "ÜRÜN KODU Seçmelisiniz!"
Me.eksreznobox.SetFocus
Exit Sub
End If
If TextBox23 = 0 And TextBox19 = 0 And TextBox20 = 0 And TextBox21 = 0 And TextBox22 = 0 Then
MsgBox "ÖDEME MİKTARI Girmelisiniz!": Exit Sub
Me.TextBox19.SetFocus
End If
If TextBox25.Value = 0 Then
MsgBox "SEPET BOŞ!"
Exit Sub
End If
If TextBox19 = "" And TextBox20 = "" And TextBox21 = "" And TextBox22 = "" And TextBox23 = "" Then
MsgBox "Tutar Girmelisiniz!":
Exit Sub
End If
If eksreznobox.ListCount = 0 Then Exit Sub
ry_bul = eksreznobox.ListIndex + 3
eksadI = Sheets("STOKKARTLARI").Range("D" & ry_bul).Value
EKSSOYADI = Sheets("STOKKARTLARI").Range("E" & ry_bul).Value
textbox12 = Sheets("STOKKARTLARI").Range("h" & ry_bul).Value
TextBox15 = Sheets("STOKKARTLARI").Range("F" & ry_bul).Value
ekstutartxt.Value = eksmiktartxt.Value * textbox12.Value
Dim lRow As Long
Dim ws As Worksheet
Set ws = Worksheets("SATISHAREKETLERİ")
lRow = ws.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row
If Trim(Me.eksreznobox.Value) = "" Then
Me.ekreznobox.SetFocus
MsgBox "Lütfen ÜRÜN KODUNU Girin!"
Exit Sub
End If
With ws
.Cells(lRow, 3).Value = Me.eksreznobox.Value
.Cells(lRow, 1).Value = Me.ekstarihtXT.Value
.Cells(lRow, 4).Value = Me.eksadI.Value
.Cells(lRow, 7).Value = Me.eksmiktartxt.Value
.Cells(lRow, 9).Value = Me.ekstutartxt.Value
.Cells(lRow, 8).Value = Me.textbox12.Value
.Cells(lRow, 5).Value = Me.EKSSOYADI.Value
.Cells(lRow, 6).Value = Me.TextBox15.Value
.Cells(lRow, 2).Value = Me.TextBox26.Value
Dim llRow As Long
Dim ws1 As Worksheet
Set ws1 = Worksheets("STOK")
llRow = ws1.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row
If Trim(Me.eksreznobox.Value) = "" Then
Me.ekreznobox.SetFocus
MsgBox "Lütfen ÜRÜN KODUNU Girin!"
Exit Sub
End If
With ws1
.Cells(llRow, 3).Value = Me.eksreznobox.Value
.Cells(llRow, 1).Value = Me.ekstarihtXT.Value
.Cells(llRow, 4).Value = Me.eksadI.Value
.Cells(llRow, 7).Value = Me.eksmiktartxt.Value
.Cells(llRow, 9).Value = Me.ekstutartxt.Value
.Cells(llRow, 8).Value = Me.textbox12.Value
.Cells(llRow, 5).Value = Me.EKSSOYADI.Value
.Cells(llRow, 6).Value = Me.TextBox15.Value
.Cells(llRow, 2).Value = Me.TextBox26.Value
.Cells(llRow, 11).Value = Me.TextBox27.Value
ekstutartxt.Value = eksmiktartxt.Value * textbox12.Value
End With
Dim lllRow As Long
Dim ws2 As Worksheet
Set ws2 = Worksheets("kasa")
lllRow = ws2.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row
If Trim(Me.eksreznobox.Value) = "" Then
Me.ekreznobox.SetFocus
MsgBox "Lütfen ÜRÜN KODUNU Girin!"
Exit Sub
End If
Me.TextBox52.Value = "SATIŞ"
With ws2
.Cells(lllRow, 1).Value = Me.ekstarihtXT.Value
.Cells(lllRow, 5).Value = Me.TextBox19.Value
.Cells(lllRow, 6).Value = Me.TextBox20.Value
.Cells(lllRow, 7).Value = Me.TextBox21.Value
.Cells(lllRow, 9).Value = Me.TextBox23.Value
.Cells(lllRow, 3).Value = Me.TextBox51.Value
.Cells(lllRow, 2).Value = Me.TextBox26.Value
.Cells(lllRow, 4).Value = Me.TextBox52.Value
ekstutartxt.Value = eksmiktartxt.Value * textbox12.Value
End With
With kayit_formu.ListBox6
.BackColor = vbWhite
.ColumnCount = 9
.ColumnWidths = "50;33;45;55;60;55;42;43;60"
.ForeColor = vbBlack
If Sheets("SATISHAREKETLERİ").Range("A1") = Empty Then
.RowSource = Empty
Else
.RowSource = "SATISHAREKETLERİ!a1:i" & [SATISHAREKETLERİ!A1048500].End(3).Row
End If
End With
MsgBox "Bir Kayit Yapildi!"
End With
Me.TextBox25.Text = CStr(ThisWorkbook.Sheets("SEPET").Range("G1").Value)
Me.TextBox24.Text = CStr(ThisWorkbook.Sheets("SEPET").Range("G2").Value)
End Sub
You can try this code.
Worksheets(“Sheet1″).Range(“A1:G22″).Copy _
Destination:=Worksheets(“Sheet2″).Range(“E5″)

Resources