Bear in mind that this is operating via a UserForm
A section of code within my "Add Enrty" code (below) on my UserForm establishes the First and last rows of a dynamic range.
It’s a dynamic range because every time a new entry is made, a new BLANK row is added.
What I’m trying to do is when that dynamic range is COMPLETELY BLANK and the first time data is added via the Userform that the 1st entry is made 3 rows BELOW the “FirstRow”. Then when subsequent entries are made that they are entered into the next available Empty/Blank row, so on & so on.
The section I can’t get right is the line; " ** This takes the values from the ComboBoxes/Text boxes on UserForm…."
A section of code within my "Add Enrty" code (below) on my UserForm establishes the First and last rows of a dynamic range.
It’s a dynamic range because every time a new entry is made, a new BLANK row is added.
What I’m trying to do is when that dynamic range is COMPLETELY BLANK and the first time data is added via the Userform that the 1st entry is made 3 rows BELOW the “FirstRow”. Then when subsequent entries are made that they are entered into the next available Empty/Blank row, so on & so on.
The section I can’t get right is the line; " ** This takes the values from the ComboBoxes/Text boxes on UserForm…."
VBA Code:
Private Sub cmdAddEntry_Click()
Dim FirstRow As Long
Dim LastRow As Long
Dim LR1 As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.ActiveSheet
Application.ScreenUpdating = False
'** Next section establishes; FirstRow, LastRow & LR1
With ws
FirstRow = .Columns("B:B").Find("Date", , xlFormulas, xlWhole, xlByRows, xlNext, False).Row + 3
LastRow = .Columns("D:D").Find("Total received", , xlFormulas, xlWhole, xlByRows, xlNext, False).Row
LR1 = .Range(.Cells(8, "A"), .Cells(LastRow - 1, "E")).Find("*", , xlValues, , xlByRows, xlPrevious).Row '*** Finds last USED row (col "A:E")
'** Next section adds new blank row every time new data is added
If LR1 = LastRow = 0 Then '(this needs to be 0 otherwise this doesn't work)
.Range("A" & LR1 + 1 & ":E" & LR1 + 1).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove ' *** Inserts new blank row into columns "A:E"
.Range("A" & LR1 + 1 & ":E" & LR1 + 1).FillUp '*** Fills the existing formatting into the newly added row
LastRow = LR1 + 1
End If
Debug.Print LastRow
Debug.Print FirstRow
Debug.Print LR1
'** This takes the values from the ComboBoxes/Text boxes on UserForm and places them in the respective columns
'?? With ws
If FirstRow.Cells = "" Then ???????????????????
.Cells(FirstRow, "B") = Format(Me.Calendar1.Value, "d/mmm/yy")
.Cells(FirstRow, "C") = Me.txtInvNo.Value
.Cells(FirstRow, "D") = Me.cmbPaymentFrom
.Cells(FirstRow, "E") = Me.txtItemValue.Value
'?? End If
Else
'**This works on its own,(but without "f FirstRow.Cells......" section above, BUT puts first value in row BELOW “FirstRow” (ei FirstRow +1)
.Cells(LastRow, "B") = Format(Me.Calendar1.Value, "d/mmm/yy")
.Cells(LastRow, "C") = Me.txtInvNo.Value
.Cells(LastRow, "D") = Me.cmbPaymentFrom
.Cells(LastRow, "E") = Me.txtItemValue.Value
End If
'?? End With
End With
'** This just resets everything on UserForm
Calendar1 = ""
txtDate = ""
txtItemValue = ""
txtInvNo = ""
cmbPaymentFrom = ""
Application.ScreenUpdating = True
End Sub