VBA - Copy Formula down from cell above using a userform

djheater

New Member
Joined
Mar 7, 2015
Messages
5
Hi,
I'm slowly getting a little better but I'm stuck. My formula is not extending and I'd like simply have the userform copy the formula down from the row above whenever I enter new data using a userform.

I found some posts and tried to piece together a solution. The data entry part works but copying the formula is not working.

Any help appreciated:

'get next empty row
lRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1

'copy the data to the spreadsheet
With ws
.Cells(lRow, 1).Value = Me.txtStudent.Value
.Cells(lRow, 2).Value = Me.txtDate.Value
.Cells(lRow, 3).Value = (Format(Me.txtDate.Value, "dddd"))
.Cells(lRow, 4).Value = Me.txtPeriod.Value
.Cells(lRow, 5).Value = Me.cmbClass.Value
.Cells(lRow, 6).Value = Me.CkExcused.Value
.Cells(lRow, 7).Value = Me.txtEngaged.Value
.Cells(lRow, 9).Value = Me.txtFlwDir.Value



'update formula
.Cells(1Row -1, 8).Resize(2, 1).Formula = Cells(1Row -1, 8).Formula


End With
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Smitty

Legend
Joined
May 15, 2003
Messages
29,536
Welcome to the Board!

The first thing I see is that you're not referencing your IRow variable here:

.Cells(1Row -1, 8).Resize(2, 1).Formula = Cells(1Row -1, 8).Formula

But using 1Row.

If you go into VBA Options and check "Require Variable Declaration", the VBE would have caught that when it tried to compile the code.

HTH,
 

djheater

New Member
Joined
Mar 7, 2015
Messages
5
Thanks!

I corrected that typo and it still is not working. It seems to remove the function from the previous row now which is odd.
 

Smitty

Legend
Joined
May 15, 2003
Messages
29,536
If you set your data range up as a table and add the formula manually to that column, then it will automatically extend as you add new data from the form. That way you don't need to worry about adding it via code.
 

djheater

New Member
Joined
Mar 7, 2015
Messages
5
I've tried the following different ways I've found on the net and didn't have any luck

'update the formulas for percentages
1 .Cells(lRow, 8).Resize(2, 1).Formula = Cells(lRow - 1, 8).Formula
2 .Range(lRow - 1).EntireRow.FillDown
3 .Range("H8" & lRow - 1).FillDown

I just converted to a table and it's still not doing it. This is kind of driving me crazy. I'm trying to link to a picture. Any ideas what I'm doing wrong? I want the formula that is calculating the percentage to fill down.

I don't think the imgur will display so here's a link....
https://imgur.com/iFktLVV
iFktLVV
 

Watch MrExcel Video

Forum statistics

Threads
1,129,803
Messages
5,638,446
Members
417,025
Latest member
MusterDuster

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
Top