hello all, I created a form and have it working well but the form and sub forms are writing to 7 sheets because I did not know how to use a range in the code. so the users add Business problems to Sheet3 and Stakeholders to sheet8 etc... I would like to clean thing up if possible, here is the code I used to get this far
Option Explicit
Dim lCurrentRow As Long
Private Sub UserForm_Activate()
' Read initial values from Row 1:
Sheets(6).Activate
lCurrentRow = 2
LoadRow
End Sub
Private Sub cmdDelete_Click()
Dim smessage As String
smessage = "Are you sure you want to delete " + Me.txtBusinessFirst.Text + " " + Me.txtBusinessLast + " ?"
If MsgBox(smessage, vbQuestion + vbYesNo, _
"Confirm Delete") = vbYes Then
' Delete current row:
Rows(lCurrentRow).Delete
' Show contents of new current row in the form:
LoadRow
End If
End Sub
Private Sub cmdNext_Click()
' Save form contents before changing rows:
SaveRow
' Increment row number:
lCurrentRow = lCurrentRow + 1
' Show contents of new row in the form:
LoadRow
End Sub
Private Sub cmdPrevious_Click()
' Show previous only if not already in first row:
If lCurrentRow > 2 Then
' Save form contents before changing rows:
SaveRow
' Decrement row number:
lCurrentRow = lCurrentRow - 1
' Show contents of new row in the form:
LoadRow
End If
End Sub
Private Sub cmdClose_Click()
' Save form contents before changing rows:
SaveRow
Unload Me
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
Cancel = True
MsgBox "To prevent loss of unsaved records, X is disabled, please use the Close button on the form.", vbCritical
'Me.Caption = "Click the Close Button to close Me!"
End If
End Sub
Private Sub cmdSaveBusOwner_Click()
' Save form contents before changing rows:
SaveRow
' Set current row to first empty row, i.e. one row after
' the last row currently in use:
If Cells(1, 1).Value = "" Then
lCurrentRow = 2 ' (list is empty - start in row 1)
Else
lCurrentRow = ActiveSheet.UsedRange.Rows.Count + 1
End If
' Clear the form for user to add new name:
txtBusinessTile.Text = ""
txtBusinessFirst.Text = ""
txtBusinessLast.Text = ""
txtBusinessBusinessArea.Text = ""
' Set focus to Name textbox:
txtBusinessTile.SetFocus
End Sub
Private Sub LoadRow()
txtBusinessTile.Text = Cells(lCurrentRow, 1).Value
txtBusinessFirst.Text = Cells(lCurrentRow, 2).Value
txtBusinessLast.Text = Cells(lCurrentRow, 3).Value
txtBusinessBusinessArea.Text = Cells(lCurrentRow, 4).Value
End Sub
Private Sub SaveRow()
Cells(lCurrentRow, 1).Value = txtBusinessTile.Text
Cells(lCurrentRow, 2).Value = txtBusinessFirst.Text
Cells(lCurrentRow, 3).Value = txtBusinessLast.Text
Cells(lCurrentRow, 4).Value = txtBusinessBusinessArea.Text
End Sub
Thanks