Workbook before save event help

PATSYS

Well-known Member
Joined
Mar 12, 2006
Messages
1,750
I have a file with about 10 sheets, 6 of those are data input sheets with exactly the same format. Users are supposed to input data here.

Several columns in those sheets are calculated fields (with formula). Specifically columns D, H, M, N and W. When users are inputting new row of data, they're supposed to simply copy the formula down. However they usually forget to do this. Furthermore, sometimes formula is deleted and overwritten be a value.

I chose not to protect the column because if I do then the users will not be able to delete rows, etc. I also did not choose to copy the formula down to 65536th row to avoid increasing the file size.

I believe the right solution would be that when the users saves, a set of code should run to copy the formula in all those calculated field and extend them down to the last row. Last row here is the last row in column A.

Can somedody show me how this is done?

Thanks
 

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
This should work. Haven't tested it though.
the If Sheet.name = line can be used if there are any sheets you don't want to run this on..

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
For Each Sheet In Sheets

    'use this line to EXCLUDE sheets from the code..
    If Sheet.name = "sheet1" OR Sheet.name = "sheet2" Then Exit Sub
    
    With Sheets(Sheet.Name)
         LR = .Cells(Rows.Count, "A").End(xlup).Row
         .Range("D2:D" & LR).Filldown
         .Range("H2:H" & LR).Filldown
         .Range("M2:M" & LR).Filldown
         .Range("N2:N" & LR).Filldown
         .Range("W2:W" & LR).Filldown
    End With

Next Sheet
End Sub
 
Upvote 0
This should work. Haven't tested it though.
the If Sheet.name = line can be used if there are any sheets you don't want to run this on..

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
For Each Sheet In Sheets

    'use this line to EXCLUDE sheets from the code..
    If Sheet.name = "sheet1" OR Sheet.name = "sheet2" Then Exit Sub
    
    With Sheets(Sheet.Name)
         LR = .Cells(Rows.Count, "A").End(xlup).Row
         .Range("D2:D" & LR).Filldown
         .Range("H2:H" & LR).Filldown
         .Range("M2:M" & LR).Filldown
         .Range("N2:N" & LR).Filldown
         .Range("W2:W" & LR).Filldown
    End With

Next Sheet
End Sub

Hi Jonmo,

Have encountered slight problem - some of the sheets are sometimes on filtered mode. When that is the case, the "hidden" rows get skipped by the filldown.

Do you know of a way to handle this?

Thanks

PS. I can not unflter (show data all) the sheets because I am not the "owner" of the sheets. The users will really be pissed of if they find out they have to re-filter eveytime.
 
Upvote 0

Forum statistics

Threads
1,213,494
Messages
6,113,988
Members
448,538
Latest member
alex78

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