VBA to add new row

coliervile

Well-known Member
Joined
May 19, 2006
Messages
724
The following worksheet (Funds) keep track of 5 funds and each day I go in and manually add a new row to at the bottom and the new daily totals. I would like a macro (with a command button) that will add a new row and copy the formulas and formats from the row above and leave the following columns blank (B, E, H, K, and N the fund closing prices).

On the second worksheet (Daily Averages) is a running totals for each day of the week averages. Thats why I don't just run the formulas down on worksheet one for all of the columns.

If someone has an easier way I'm all ears!

Here the worksheets...letsjust say that row 6 is the last filled row in the worksheet.

Excel Workbook
ABCDEFGHIJKLMNOPQR
1DateG$ Change% ChangeF$ Change% ChangeC$ Change% ChangeS$ Change% ChangeI$ Change% ChangeBestWorst
230-Dec-19946.10810.00000.00004.95390.00000.00004.16200.00000.0000******GG
33-Jan-19956.11000.00190.00034.95850.00460.00094.18060.01860.0045******CG
44-Jan-19956.11200.00200.00034.96310.00460.00094.19920.01860.0044******CG
55-Jan-19956.11390.00190.00034.96780.00470.00094.1977(0.0015)(0.0004)******FC
66-Jan-19956.11590.00200.00034.97240.00460.00094.20070.00300.0007******FG
Funds


Worksheet 2- Daily Averages

Excel Workbook
ABCD
1CSI
2Monday0.00389(0.00638)0.00215
3Tuesday0.005170.005040.00324
4Wednesday0.003330.012530.00849
5Thursday0.002360.004520.00382
6Friday0.000420.00839(0.00028)
Daily Averages
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
I have this code from a userform (see below). How can I can this part of the coding
""copy the data to the database
ws.Cells(iRow, 1).Value = Me.txtDate.Value
ws.Cells(iRow, 2).Value = Me.txtGFund.Value
ws.Cells(iRow, 5).Value = Me.txtFFund.Value
ws.Cells(iRow, 8).Value = Me.txtCFund.Value
ws.Cells(iRow, 11).Value = Me.txtSFund.Value
ws.Cells(iRow, 14).Value = Me.txtIFund.Value""

to input the new data into the newly added row and not the fixed rows that is currently be used.


Private Sub cmdAdd_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Funds")

'find first empty row in database
iRow = ws.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row

'check for a date
If Trim(Me.txtDate.Value) = "" Then
Me.txtDate.SetFocus
MsgBox "Please enter date"
Exit Sub
End If

'copy the data to the database
ws.Cells(iRow, 1).Value = Me.txtDate.Value
ws.Cells(iRow, 2).Value = Me.txtGFund.Value
ws.Cells(iRow, 5).Value = Me.txtFFund.Value
ws.Cells(iRow, 8).Value = Me.txtCFund.Value
ws.Cells(iRow, 11).Value = Me.txtSFund.Value
ws.Cells(iRow, 14).Value = Me.txtIFund.Value

'clear the data
Me.txtDate.Value = ""
Me.txtGFund.Value = ""
Me.txtFFund.Value = ""
Me.txtCFund.Value = ""
Me.txtSFund.Value = ""
Me.txtIFund.Value = ""
Me.txtDate.SetFocus

End Sub
Private Sub cmdClose_Click()
Unload Me
End Sub

Private Sub txtDate_Change()

End Sub
 
Upvote 0
Here is all the VBA code that adds data to a database but not in a new row below the last filled row. I have two command button on the front of this worksheet named "Add Row" and "Click here to add new fund data".

Under Forms VBA
frmFunds:

Option Explicit

Private Sub cmdAdd_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Funds")

'find first empty row in database
iRow = ws.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row

'check for a date
If Trim(Me.txtDate.Value) = "" Then
Me.txtDate.SetFocus
MsgBox "Please enter date"
Exit Sub
End If

'copy the data to the database
ws.Cells(iRow, 1).Value = Me.txtDate.Value
ws.Cells(iRow, 2).Value = Me.txtGFund.Value
ws.Cells(iRow, 5).Value = Me.txtFFund.Value
ws.Cells(iRow, 8).Value = Me.txtCFund.Value
ws.Cells(iRow, 11).Value = Me.txtSFund.Value
ws.Cells(iRow, 14).Value = Me.txtIFund.Value

'clear the data
Me.txtDate.Value = ""
Me.txtGFund.Value = ""
Me.txtFFund.Value = ""
Me.txtCFund.Value = ""
Me.txtSFund.Value = ""
Me.txtIFund.Value = ""
Me.txtDate.SetFocus

End Sub
Private Sub cmdClose_Click()
Unload Me
End Sub

Private Sub txtDate_Change()

End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, _
CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
Cancel = True
MsgBox "Please use the button!"
End If
End Sub

module 1:

Sub FindEmptyCell()
If ActiveSheet.UsedRange.Count < 2 Then
MsgBox 1
Else
MsgBox Columns("A:A").Find(What:="", LookAt:=xlWhole).Row
End If
End Sub

module 2:

Sub Rectangle1_Click()
frmFunds.Show
End Sub

module 3:

Sub InsertRow()
ActiveCell.Offset(1, 0).EntireRow.Insert
End Sub

module 4:

Sub InsertRowsAndFillFormulas(Optional vRows As Long = 0)


' row selection based on active cell
Dim x As Long
ActiveCell.EntireRow.Select 'So you do not have to preselect entire row
If vRows = 0 Then
vRows = Application.InputBox(prompt:= _
"How many rows do you want to add?", Title:="Add Rows", _
Default:=1, Type:=1) 'Default for 1 row, type 1 is number
If vRows = False Then Exit Sub
End If

'if you just want to add cells and not entire rows
'then delete ".EntireRow" in the following line

Dim sht As Worksheet, shts() As String, i As Long
ReDim shts(1 To Worksheets.Application.ActiveWorkbook. _
Windows(1).SelectedSheets.Count)
i = 0
For Each sht In _
Application.ActiveWorkbook.Windows(1).SelectedSheets
Sheets(sht.Name).Select
i = i + 1
shts(i) = sht.Name

x = Sheets(sht.Name).UsedRange.Rows.Count 'lastcell fixup

Selection.Resize(rowsize:=2).Rows(2).EntireRow. _
Resize(rowsize:=vRows).Insert Shift:=xlDown

Selection.AutoFill Selection.Resize( _
rowsize:=vRows + 1), xlFillDefault

On Error Resume Next 'to handle no constants in range
' to remove the non-formulas
Selection.Offset(1).Resize(vRows).EntireRow. _
SpecialCells(xlConstants).ClearContents
Next sht
Worksheets(shts).Select
End Sub

module 5:

Sub InsertRowsAndFillFormulas_caller()
'-- this macro shows on Tools, Macro..., Macros (Alt+F8) dialog
Call InsertRowsAndFillFormulas
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

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