Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-06-2016, 12:45 PM
OldColdDreamer OldColdDreamer is offline Copy data btw worksheets using public arrays Windows 10 Copy data btw worksheets using public arrays Office 2013
Novice
Copy data btw worksheets using public arrays
 
Join Date: Feb 2016
Location: Stockport UK
Posts: 3
OldColdDreamer is on a distinguished road
Angry Copy data btw worksheets using public arrays

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.
Attached Files
File Type: xlsm Cash analysis 3.00 wip+minimal data.xlsm (367.9 KB, 10 views)
Reply With Quote
  #2  
Old 02-09-2016, 03:43 AM
Debaser's Avatar
Debaser Debaser is offline Copy data btw worksheets using public arrays Windows 7 64bit Copy data btw worksheets using public arrays Office 2010 32bit
Competent Performer
 
Join Date: Oct 2015
Location: UK
Posts: 221
Debaser will become famous soon enough
Default

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.
Reply With Quote
  #3  
Old 02-09-2016, 05:00 PM
OldColdDreamer OldColdDreamer is offline Copy data btw worksheets using public arrays Windows 10 Copy data btw worksheets using public arrays Office 2013
Novice
Copy data btw worksheets using public arrays
 
Join Date: Feb 2016
Location: Stockport UK
Posts: 3
OldColdDreamer is on a distinguished road
Default

Thanks, Debaser. I think I am starting to get sorted out with my array and const declarations. Two quick follow-ons from this, though:

I want to call a sub (or a function?) when I try to leave a worksheet, which does some analysis of the data on that sheet, and leaves the results in various arrays.

What seems to work is the following coding...

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

I seem to need the double-layer call in order to get my "deactivate()" code invoked when (say) the user clicks on the tab for another worksheet, and then do the processing where the arrays etc. are in scope. But is there a better way of achieving this?
In the event that my analysis finds an error, can I (maybe by using functions instead of subs) force the user to stay on the outgoing worksheet and fix the error?
Reply With Quote
  #4  
Old 02-10-2016, 01:55 AM
Debaser's Avatar
Debaser Debaser is offline Copy data btw worksheets using public arrays Windows 7 64bit Copy data btw worksheets using public arrays Office 2010 32bit
Competent Performer
 
Join Date: Oct 2015
Location: UK
Posts: 221
Debaser will become famous soon enough
Default

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.
Reply With Quote
  #5  
Old 02-10-2016, 09:56 AM
OldColdDreamer OldColdDreamer is offline Copy data btw worksheets using public arrays Windows 10 Copy data btw worksheets using public arrays Office 2013
Novice
Copy data btw worksheets using public arrays
 
Join Date: Feb 2016
Location: Stockport UK
Posts: 3
OldColdDreamer is on a distinguished road
Default

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.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy data btw worksheets using public arrays Summing data from multiple worksheets in a workbook and placing into another worksheet 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
Copy data btw worksheets using public arrays Advanced Filter - Copy matching records in two worksheets 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

Other Forums: Access Forums

All times are GMT -7. The time now is 11:38 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft