Advertisement
Guest User

Copy userform data to table VBA

a guest
Jun 15th, 2015
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. Private Sub Cmdbutton_edit_Click()
  2. Dim iRow As Long
  3. Dim ws As Worksheet
  4. Set ws = Worksheets("Sheet1")
  5.  
  6. 'find first empty row in database
  7. iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
  8.  
  9. 'check for a Name number
  10. If Trim(Me.TextBox_CN.Value) = "" Then
  11. Me.TextBox_CN.SetFocus
  12. MsgBox "Please complete the form"
  13. Exit Sub
  14. End If
  15.  
  16. 'copy the data to the database
  17. ws.Cells(iRow, 1).Value = Me.Question1.Value
  18. ws.Cells(iRow, 2).Value = Me.Question2.Value
  19. ws.Cells(iRow, 3).Value = Me.Question3.Value
  20. ws.Cells(iRow, 4).Value = Me.Question4.Value
  21. ws.Cells(iRow, 5).Value = Me.Question5.Value
  22. ws.Cells(iRow, 6).Value = Me.Question6.Value
  23. ws.Cells(iRow, 7).Value = Me.Question7.Value
  24.  
  25. MsgBox "Data edited", vbOKOnly + vbInformation, "Data Added"
  26. 'clear the data
  27. Value = Me.Question1.Value
  28. Value = Me.Question2.Value
  29. Value = Me.Question3.Value
  30. Value = Me.Question4.Value
  31. Value = Me.Question5.Value
  32. Value = Me.Question6.Value
  33. Value = Me.Question7.Value
  34.  
  35. Me.TextBox_CN.SetFocus
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement