fill blank rows or insert blank rows before lastrow by userform

Abdo

Board Regular
Joined
May 16, 2022
Messages
220
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Hi guys,
I want copy data from all of textboxes on userform to sheet
column A should populate date , column B=textbox1,column C =textbox2, column D=textbox3
column E should calculate as I put the formulas( formula in second row is not after until before TOTAL row ).
TOTAL row should sum columns C,D
textbox2,3 will add numeric values
should copy and fill in blank rows , if there is no blank rows then will add new blank row before total row with the same formatting .
I don't need showing any formula.

here is my data on sheet & code
fill (2).xlsm
ABCDE
1DATEBRANDIMPORTEXPORTNET
20.00
30.00
40.00
5TOTAL0.000.000.00
ATSS
Cell Formulas
RangeFormula
E2,E5E2=C2-D2
E3:E4E3=E2+C3-D3
C5:D5C5=SUM(C2:C4)


VBA Code:
Private Sub CommandButton1_Click()
Dim LR As Long
Dim ws As Worksheet

Set ws = Sheets("ATSS")
With ws
LR = .Range("A" & Rows.Count).End(xlUp).Row
.Range("A" & LR + 1) = Date
.Range("B" & LR + 1) = TextBox1.Text
.Range("C" & LR + 1) = TextBox2.Text
.Range("D" & LR + 1) = TextBox3.Text

End With
TextBox1.Value = ""
TextBox2.Value = ""
TextBox3.Value = ""


End Sub
also posted here
fill blank rows or insert blank rows before lastrow by userform
thanks
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Your sheet should retain at least the formulas in cells E2 and E3.
From then on the macro can insert the rows and continue creating the formulas for the total row and column E.

Try:
VBA Code:
Private Sub CommandButton1_Click()
  Dim i As Long
  Dim ws As Worksheet
  Dim f As Range
  
  Application.ScreenUpdating = False
  Set ws = Sheets("ATSS")
  i = 2
  Do While Range("A" & i).Value <> ""
    i = i + 1
    If ws.Range("A" & i).Value = "TOTAL" Then
      ws.Range("A" & i & ":E" & i).Insert Shift:=xlDown
    End If
  Loop
  
  ws.Range("A" & i) = Date
  ws.Range("B" & i) = TextBox1.Text
  ws.Range("C" & i) = TextBox2.Text
  ws.Range("D" & i) = TextBox3.Text
  If i - 1 > 2 Then ws.Range("E" & i - 1).Copy ws.Range("E" & i)
  Set f = ws.Range("A:A").Find("TOTAL", , xlValues, xlWhole, , , False)
  With ws.Range("C" & f.Row & ":D" & f.Row)
    .Formula = "=SUM(C2:C" & f.Row - 1 & ")"
  End With
  
  TextBox1.Value = ""
  TextBox2.Value = ""
  TextBox3.Value = ""
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi Dante,
Your sheet should retain at least the formulas in cells E2 and E3.
if there is way to add formula by code as in value . I don't want showing formulas
thanks for your time .
 
Upvote 0
I don't need showing any formula.
Then try:

VBA Code:
Private Sub CommandButton1_Click()
  Dim i As Long
  Dim ws As Worksheet
  Dim f As Range
  
  Application.ScreenUpdating = False
  Set ws = Sheets("ATSS")
  i = 2
  Do While Range("A" & i).Value <> ""
    i = i + 1
    If ws.Range("A" & i).Value = "TOTAL" Then
      ws.Range("A" & i & ":E" & i).Insert Shift:=xlDown
    End If
  Loop
  
  ws.Range("A" & i) = Date
  ws.Range("B" & i) = TextBox1.Text
  ws.Range("C" & i) = TextBox2.Text
  ws.Range("D" & i) = TextBox3.Text
  If i = 2 Then
    ws.Range("E2").Value = ws.Range("C2").Value - ws.Range("D2").Value
  Else
    ws.Range("E" & i).Value = ws.Range("E" & i - 1).Value + ws.Range("C" & i).Value - ws.Range("D" & i).Value
  End If
  
  Set f = ws.Range("A:A").Find("TOTAL", , xlValues, xlWhole, , , False)
  With ws.Range("C" & f.Row & ":D" & f.Row)
    .Formula = "=SUM(C2:C" & f.Row - 1 & ")"
    .Value = .Value
  End With
  ws.Range("E" & f.Row).Value = ws.Range("C" & f.Row).Value - ws.Range("D" & f.Row).Value
  
  TextBox1.Value = ""
  TextBox2.Value = ""
  TextBox3.Value = ""
  Application.ScreenUpdating = True
End Sub
 
Upvote 1
Solution

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top