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

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
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,
 
Upvote 0
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.
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,861
Messages
6,121,969
Members
449,059
Latest member
oculus

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