Reputation: 15
I'm trying to save data entered in a userform into to differnet sheets.
The problem I'm currently having is that, one of the sheets VBA has to look up the specific row where it sould be added, but the other sheet is going to be a history of the data inserted, so it needs to insert the data on the next free row.
I have this code which works for looking up and inserting into the first sheet:
Private Sub pSave()
Dim rw As Integer
Dim ws As Worksheet
Set ws = Worksheets("Hardware")
'Takting the inserted values from the userform and inserting them into the spreadsheet
totRows = Worksheets("Hardware").Range("A4").CurrentRegion.Rows.Count
For i = 2 To totRows
If Trim(Worksheets("Hardware").Cells(i, 1)) = Trim(ComboBox_PCNameChoose.Value) Then
'Inserting them into the Hardware sheet (The main sheet)
Worksheets("Hardware").Cells(i, 12).Value = TextBox_Name.Text
Worksheets("Hardware").Cells(i, 13).Value = TextBox_Email.Text
Worksheets("Hardware").Cells(i, 14).Value = TextBox_PhoneNumber.Text
Worksheets("Hardware").Cells(i, 15).Value = DTPicker_Borrow.Value
Worksheets("Hardware").Cells(i, 16).Value = DTPicker_Return.Value
Exit For
End If
Next i
I know this works in another userform for inserting data into the next free row, but I cant figure out how to get it to work when saving in two sheets at the same time
Dim rw As Integer
Dim ws2 As Worksheet
Set ws2 = Worksheets("Rental_History")
If rw = ws2.Cells.Find(What:="*", Searchorder:=xlRows, SearchDirection:=Previous, LookIn:=xlValues).Row + 1 Then
ws2.Cells(rw, 10).Value = TextBox_Name.Text
ws2.Cells(rw, 11).Value = TextBox_Email.Text
ws2.Cells(rw, 12).Value = TextBox_PhoneNumber.Text
ws2.Cells(rw, 13).Value = DTPicker_Borrow.Value
ws2.Cells(rw, 14).Value = DTPicker_Return.Value
End If
In Advance, thank you for your time and help! :)
Best Regards - Kira
Upvotes: 0
Views: 2940
Reputation: 7735
I believe the following will achieve what you expect, instead of using a For Loop to find the row where you want to add the first bit of data I used the .Find method, as this would be faster, instead of looping through every row until you find a match, the find method would quickly jump to the matched row.
Also it is important to note that I changed the declaration of rw from Integer to Long, as there are more cells in Excel than an Integer variable can handle:
Private Sub pSave()
Dim rw As Long
Dim ws As Worksheet: Set ws = Worksheets("Hardware")
Dim ws2 As Worksheet: Set ws2 = Worksheets("Rental_History")
Dim foundval As Range
'Taking the inserted values from the userform and inserting them into the spreadsheet
Set foundval = ws.Range("A:A").Find(What:=Trim(ComboBox_PCNameChoose.Value)) 'find the value that matches
If Not foundval Is Nothing Then 'if found, use that row to insert data
'Inserting them into the Hardware sheet (The main sheet)
ws.Cells(foundval.Row, 12).Value = TextBox_Name.Text
ws.Cells(foundval.Row, 13).Value = TextBox_Email.Text
ws.Cells(foundval.Row, 14).Value = TextBox_PhoneNumber.Text
ws.Cells(foundval.Row, 15).Value = DTPicker_Borrow.Value
ws.Cells(foundval.Row, 16).Value = DTPicker_Return.Value
End If
rw = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row + 1
'get the next free row
ws2.Cells(rw, 10).Value = TextBox_Name.Text
ws2.Cells(rw, 11).Value = TextBox_Email.Text
ws2.Cells(rw, 12).Value = TextBox_PhoneNumber.Text
ws2.Cells(rw, 13).Value = DTPicker_Borrow.Value
ws2.Cells(rw, 14).Value = DTPicker_Return.Value
End Sub
Upvotes: 1
Reputation: 83
Dim rw As Integer
Dim ws As Worksheet
Set ws = Worksheets("Hardware")
Dim rw1 As Integer
Dim ws2 As Worksheet
Set ws2 = Worksheets("Rental_History")
'Takting the inserted values from the userform and inserting them into the spreadsheet
totRows = Worksheets("Hardware").Range("A4").CurrentRegion.Rows.Count
For i = 2 To totRows
If Trim(Worksheets("Hardware").Cells(i, 1)) = Trim(ComboBox_PCNameChoose.Value) Then
'Inserting them into the Hardware sheet (The main sheet)
rw = ws2.Cells.Find(What:="*", Searchorder:=xlRows, SearchDirection:=Previous, LookIn:=xlValues).Row + 1 'updates rw as it changes at each loop
ws.Cells(i, 12).Value = TextBox_Name.Text
ws2.Cells(rw, 10).Value = ws.Cells(i, 12).Value
ws.Cells(i, 13).Value = TextBox_Email.Text
ws2.Cells(rw, 11).Value = ws.Cells(i, 13).Value
ws.Cells(i, 14).Value = TextBox_PhoneNumber.Text
ws2.Cells(rw, 12).Value = ws.Cells(i, 14).Value
ws.Cells(i, 15).Value = DTPicker_Borrow.Value
ws2.Cells(rw, 13).Value = ws.Cells(i, 15).Value
ws.Cells(i, 16).Value = DTPicker_Return.Value
ws2.Cells(rw, 14).Value = ws.Cells(i, 16).Value
End If
Next i
Upvotes: 0