Sub SubmitFormData()
Dim sws As Worksheet, dws As Worksheet
Dim unitRng As Range
Dim Unit As String
Dim c As Long, lr As Long
Application.ScreenUpdating = False
Set sws = Sheets("Data Entry")
Set dws = Sheets("Tables")
If Application.CountA(sws.Range("B7,D7,F7")) < 3 Then
MsgBox "The Form is incomplete. Fill the Form and then try again...", vbExclamation, "Unit Not Filled!"
Exit Sub
End If
Unit = sws.Range("B7").Value
Set unitRng = dws.Rows(1).Find(what:=Unit, lookat:=xlWhole)
If Not unitRng Is Nothing Then
c = unitRng.Column
lr = dws.Cells(Rows.Count, c).End(xlUp).Row + 1
sws.Range("D7").Copy dws.Cells(lr, c)
dws.Cells(lr, c + 1) = sws.Range("F7").Value
dws.Cells(lr, c + 2).FormulaR1C1 = "=IFERROR((R[1]C[-1]-RC[-1])/(DAYS(R[1]C[-2],RC[-2])),"""")"
dws.Cells(lr, c).Resize(, 3).Font.Size = 9
sws.Range("D7,F7").ClearContents
Else
c = dws.Cells(2, Columns.Count).End(xlToLeft).Column + 2
dws.Cells(1, c) = Unit
dws.Cells(1, c).Resize(, 3).Merge
dws.Cells(1, c).HorizontalAlignment = xlCenter
dws.Cells(2, c) = "Date"
dws.Cells(2, c + 1) = "SMU"
dws.Cells(2, c + 2) = "Util/Day"
With dws.Range(dws.Cells(1, c), dws.Cells(2, c + 2))
.Interior.Color = RGB(191, 191, 191)
.Font.Bold = True
End With
sws.Range("D7").Copy dws.Cells(3, c)
dws.Cells(3, c + 1) = sws.Range("F7").Value
dws.Cells(3, c + 2).FormulaR1C1 = "=IFERROR((R[1]C[-1]-RC[-1])/(DAYS(R[1]C[-2],RC[-2])),"""")"
dws.Cells(3, c).Resize(, 3).Font.Size = 9
dws.Cells(1, c).CurrentRegion.Borders.Color = vbBlack
dws.Cells(1, c).CurrentRegion.Columns.AutoFit
sws.Range("D7,F7").ClearContents
End If
Application.ScreenUpdating = True
MsgBox "The Form was submitted successfully", vbInformation, "Done!"
End Sub
In the attached, fill the form and hit the Submit button to submit the form data in the relevant table on Tables Sheet.
Bookmarks