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

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061
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
 

PATSYS

Well-known Member
Joined
Mar 12, 2006
Messages
1,750
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.
 

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832
Hi,

You can unfilter if you put everything back afterwards...
see thread to retrieve current filtercriteria
http://www.mrexcel.com/board2/viewtopic.php?p=1296896

(putting back as before code started still to do)


Alternatively (perhaps better) you could copy the workbook, unfilter, run the code, copy the results, delete the copy, ....
kind regards,
Erik
 

Forum statistics

Threads
1,181,102
Messages
5,928,069
Members
436,586
Latest member
latintxn

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