![]() |
#1
|
|||
|
|||
![]() A long spiel from a septuagenarian novice struggling with Excel VBA... I have a workbook with several data-entry worksheets (each with a different layout, to facilitate input of possibly repetitive financial data, and variable amounts of data). For each of these sheets, other sheets transcribe the data into tabular blocks laid out for printing, adding things like Brought Forward, Carried Forward, subtotals etc. Some of these latter sheets contain the full input data; some remove personal details and only show total amounts. Finally a "Print" sheet merges the full data onto several pages of paper, deciding (inter alia) where to put page breaks. A "Digest" sheet does the same for the de-personalised data. The user fills in all the data-entry sheets, then prints the "Print" and "Digest" sheets. The problem is that this is achieved using some extremely complicated cell formulae, which (although they work) are not easily maintainable - including conditional formatting to put horizontal or vertical lines, or boxes, around various fields on the printout. I would like to use VBA macros to collect the data in public arrays, and use code with copious comments to do the reorganisation of the data. Typically this should occur in a routine called on exiting from each sheet to the next one. [Also, I would like to declare some preset arrays, e.g. the value of each coin/banknote denomination.] But I haven't discovered how and where to declare these arrays. If I try doing it at the head of the Workbook code, I can't "do the presetting outside a sub"; if I try it within a sub, it doesn't like the public declaration. ![]() |
#2
|
||||
|
||||
![]()
Public variables have to be declared outside all routines, but set within one. You can either use public Functions that simply return the array you need, or use public Constants that are delimite strings, and then Split them whenever you need an array.
|
#3
|
|||
|
|||
![]()
Thanks, Debaser. I think I am starting to get sorted out with my array and const declarations. Two quick follow-ons from this, though:
![]() ![]() In worksheet "Income" (aka "Sheet2"): Private Sub worksheet_deactivate() Call ThisWorkbook.Income_Deactivate End Sub In ThisWorkbook: <various array and constant declarations (I'm happy with these now)> Public Sub Income_Deactivate() <sort the data into the arrays, maybe generate error messages> End Sub ![]() ![]() |
#4
|
||||
|
||||
![]()
The public declarations don't really belong in the ThisWorkbook module - they should be in a regular module. If you have them in ThisWorkbook, you need to access them as members of that object from other modules - i.e. use ThisWorkbook.variable_name and not just variable_name.
You could turn your routine into a function that returns True if everything is ok and False otherwise. Then the deactivate event can check the return value and simply reactivate the sheet if need be. |
#5
|
|||
|
|||
![]()
Thanks, Debaser. I haven't started to use modules yet, but I think I am now able to work out for myself how to do it.
You may consider this thread solved, and once again many thanks. |
![]() |
Thread Tools | |
Display Modes | |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
safrac | Excel | 17 | 10-04-2022 12:21 PM |
Can you copy & paste cells across worksheets and preserve reference to worksheet? | New Daddy | Excel | 2 | 11-27-2013 07:19 AM |
![]() |
morgantobin | Excel | 1 | 03-29-2013 03:58 AM |
Finding sub-sets of data in worksheets | Rudi | Excel | 3 | 01-28-2013 06:13 AM |