behavioral data management using visual basic for applications to automate data capture and analysis
Post on 02-Jul-2016
212 Views
Preview:
TRANSCRIPT
Behavioral data management using VISUAL BASIC FOR APPLICATIONS
to automate data capture and analysis
Maurice L. Sipos a,*, Richard E. Sweeney b
a Advanced Assessment Branch, Drug Assessment Division, United States Army Medical Research Institute of Chemical Defense, Aberdeen Proving
Ground, MD 21010, USAb RESECO Research Engineering Consultants, PO Box 2311, Upper Darby, PA 19082, USA
Received 23 September 2002; received in revised form 14 May 2003; accepted 14 May 2003
Journal of Neuroscience Methods 128 (2003) 53�/65
www.elsevier.com/locate/jneumeth
Abstract
Many researchers are familiar with the spreadsheet capabilities of Microsoft Excel†, but have never explored using customized
VISUAL BASIC FOR APPLICATIONS (VBA) macros embedded in the program. At the United States Army Medical Research Institute
of Chemical Defense (USAMRICD), the implementation of VBA program code to carry out repetitive operations has resulted in a
tremendous savings in both the time and manpower required to reliably capture, analyze, and plot data from research protocols. A
set of ‘‘template’’ workbooks was developed and is used to organize data from different types of studies. At the heart of the concept
is a ‘‘setup information’’ worksheet onto which the user enters information about the study (i.e. the number of subjects, groups,
graphs). Clicking a control button on this worksheet launches the VBA code that creates worksheets for each subject, group and
chart specified. A ‘‘pairmatch’’ function allows the user to create groups either randomly or by pair matching based on user-
specified variables. Controls are provided that run VBA program code to parse subject data files into the proper worksheets and
perform group averages. Charts are updated automatically whenever group averages change so that the researcher always has an up-
to-date plot available. The ability to reformat the captured data onto a ‘‘statistical output’’ worksheet allows data to be imported
easily to statistical software packages. The concept is being used for several different types of studies at USAMRICD and has
significantly reduced not only amount of time spent on data management, but also the number of data entry errors. Readers
interested in acquiring an electronic copy of the startle workbook example, which contains the complete VBA code, should send the
request to the authors at reseco@erols.com or maurice.sipos@us.army.mil.
# 2003 Elsevier B.V. All rights reserved.
Keywords: Data entry; Data management; Automation; VISUAL BASIC FOR APPLICATIONS; Macros; Microsoft Excel
1. Introduction
Studies examining the effects of repeated exposures to
low doses of chemical warfare nerve agents at the
United States Army Medical Research Institute of
Chemical Defense (USAMRICD) typically involve the
daily collection of several physiological or behavioral
measurements from subjects over several weeks or
months (Fig. 1 illustrates the time course of a typical
study). These studies generate copious amounts of data
that must be collected, organized, stored, compiled and
analyzed. Prior to the inception of this automated
system, skilled technicians working on the study were
forced to process the data at least partially by hand,
which often required them to devote a significant
portion of their time to routine tasks such as data entry,
computations, or file manipulations. Manual data
management typically involved reorganizing the data
files produced by laboratory software packages and
importing them into a spreadsheet. Subsequently, in-
dividual subject data could be imported into a statistical
package for data analysis and graphical presentation or
could be graphed directly using the spreadsheet pro-
gram. In addition to consuming an inordinate amount
* Corresponding author. Present address: Army Physical Fitness
Research Institute, US Army War College, 122 Forbes Avenue,
Carlisle, PA 17013, USA. Tel.: �/1-717-245-4580; fax: �/1-717-245-
4558.
E-mail address: maurice.sipos@us.army.mil (M.L. Sipos).
0165-0270/03/$ - see front matter # 2003 Elsevier B.V. All rights reserved.
doi:10.1016/S0165-0270(03)00150-X
of time, processing data manually was error prone and
required additional time to ensure the accuracy of data
entry.
In the present paper, the benefit of using VISUAL
BASIC FOR APPLICATIONS (VBA) macros to facilitate
data management and analysis is discussed. Microsoft
Excel† was chosen as the platform around which the
automated processing system would be centered.
Although many individuals are already familiar with
the EXCEL program, most have never explored the
possibility of using VBA macros embedded within the
application to perform routine tasks. The present manu-
script targets individuals with an intermediate level of
expertise: individuals who know what VBA macros can
do, but have never considered applying them to his/her
own laboratory. More inexperienced users are directed
to review more general references to gain a basic
understanding of what VBA is and how macros can be
used to automate repetitive functions (i.e. Jacobson,
1999; Wells and Harshbarger, 1997, or the VBA help
files).
Using VBA macros to perform repetitive data manip-
ulations increased productivity and reduced errors in
data entry. The macros were embedded into the work-
sheets of an EXCEL workbook that also served to
organize, analyze, plot and store the data. A workbook
was created as a template that could be copied andrenamed for use during studies examining the effects of
chemical warfare nerve agents on the acoustic startle
response in guinea pigs. Although this paper is based on
using VBA macros to process acoustic startle response
data, VBA macros have also been used at USAMRICD
to process data collected from active avoidance studies,
radial arm maze studies, and operant procedures, as well
as to process data collected using electronic laboratorymonitoring systems that collect body weight data and
temperature. A copy of the startle workbook template,
which contains the complete VBA code, may be
obtained from reseco@erols.com or maurice.sipos@
us.army.mil.
2. Methods and results
2.1. Building the workbook
Since many parameters can change from study to
study, a ‘‘Setup Information’’ worksheet was included in
Fig. 1. Generalized time line for a study compatible with this software. Note that the baseline, challenge, and recovery periods can be of arbitrary
duration.
M.L. Sipos, R.E. Sweeney / Journal of Neuroscience Methods 128 (2003) 53�/6554
Fig. 2. The macros are contained in a ‘‘Template’’ workbook that contains worksheets from which Subject, Group, Statistics, and Charts are copied.
In this particular example, 60 subjects were assigned to one of ten groups based on treatment (saline or 0.4�/LD50 VX) and food availability (Ad
libitum, 100% of the recommended daily requirement (RDR), 85% of the RDR, 75% of the RDR, or 65% of the RDR).
Fig. 3. The subjects are randomly assigned into treatment groups and listed into columns ‘‘A’’ through ‘‘G.’’ See Fig. 2 legend for the details of the
study included in this example.
M.L. Sipos, R.E. Sweeney / Journal of Neuroscience Methods 128 (2003) 53�/65 55
the template workbook that would allow users to
customize the program to meet their individual needs
(see Fig. 2). On this worksheet, the user must enter the
total number of subjects in the study, the identification
number for the first subject, the number of study groups
and treatment groups included in the study, group labels
and their corresponding graph colors, the total number
of charts to be created, the names, titles, and specific
information for each chart, the directory from which
unprocessed data files are retrieved, and the directory
into which processed data files should be deposited.
Once the required information has been entered, the
user clicks on the ‘‘Create Subject Sheets’’ button to
initiate a VBA macro that directs the analysis workbook
to ‘‘build’’ itself based upon the setup information.
Although it has not been incorporated into the startle
template workbook, the VBA code could be used to
check the user’s entries for validity (based on rules set up
in advance). Annotated excerpts of the VBA code that
performs the ‘‘self-build’’ task are provided in section 1
of Appendix A. The self-build feature saves an extra-
ordinary amount of time when compared with manually
interfacing with the EXCEL program to create the charts
and groups. During building, many worksheets are
created in the workbook as described below.
1) A subject data worksheet is created from the
‘‘Subject X’’ template worksheet for each subject
in the study and is named according to the subject’s
identification number.
2) A group data worksheet is created from the ‘‘Group
X’’ template worksheet for each group defined in
the study and is named appropriately.3) A chart type worksheet is created from the ‘‘Chart
X’’ template sheet for each requested chart and is
named appropriately.
The user must create these template sheets manually
depending on the information that is collected during
the study. The organization used for the subject sheet in
a sample acoustic startle response study is shown in Fig.
5. It was useful to devote one row for each startle session
and single columns for date, group, and session identi-
fiers and for each variable measured during the session.
The organization of the group template can be similar to
the subject sheet, but would contain averages of results
across the subjects within the group. The chart template
sheet contains a single chart that plots one data series
for each group in the study. The y-source data for each
series is taken from a measurement column on a group
worksheet. The x-source data is taken from the corre-
sponding date column on the group worksheets. As a
result, each chart is devoted to a single variable
measured in the study and facilitates the comparison
of daily group means by dependent variable during the
course of the study. To create the chart, the ‘‘Group X’’
template was filled with temporary data to provide a
target for the chart’s series objects. During the work-
book build operation, the source data references for all
data series in each chart are automatically calculatedand pointed to appropriate cells on the group sheets.
Means, standard errors of the mean, and colors of the
lines for each group on each chart are entered auto-
matically under program control. After creation, the
charts always display the values (means and standard
errors of the mean) that are present in the group
worksheets.
2.2. The ‘‘Pairmatch’’ worksheet
As the workbook is built, information about the study
is entered automatically onto the ‘‘Pairmatch’’ work-
sheet. Fig. 3 shows a sample ‘‘Pairmatch’’ worksheet for
a new workbook before data are processed. Subject
identifiers are listed in columns ‘‘A’’ and ‘‘F.’’ Althoughnot used in the acoustic startle response sample study,
two columns are reserved for subject identifiers for cases
in which subjects can be identified using both a name
and a number. For example, nonhuman primates are
often given a number and a name (i.e. 89�/154 and
‘‘Alpha,’’ respectively). Columns ‘‘B’’ and ‘‘C’’ in Fig. 3
display empty cells for each animal, but these are
replaced by the average startle response values to 100and 120 dB stimuli collected over the most recent 5 days
of data collection prior to pair matching. Column ‘‘G’’
lists the treatment group to which each animal has been
randomly assigned by the macro. Columns ‘‘I’’ through
‘‘O’’ display group averages of the measurements used
for pair matching. This information is not valid until
pair matching, which will be discussed shortly, has been
performed. The information in cells ‘‘V2’’ through‘‘X16’’ is used for creation of an output worksheet
that presents data in a format that can be easily
exported to the SigmaStat† statistical package.
After the workbook has been built, the VBA macro
automatically renames the template to prevent the user
from inadvertently overwriting the original. The ‘‘Pair-
match’’ worksheet contains several control buttons that
run VBA macros that enable users to perform routinedaily tasks. Although data from multiple days can be
processed together, the power of the macros is that they
enable the user to examine the data immediately to
determine what effects are occurring on a daily basis. To
process data, a technician executes the steps outlined
below:
1) Move the raw data files (produced by the laboratory
equipment) for the day into the ‘‘input’’ directory
area specified in cell ‘‘B3’’ of the ‘‘Setup Informa-tion’’ worksheet.
2) Click the ‘‘Crunch New Data’’ button to launch a
macro that automatically reads each raw data file,
M.L. Sipos, R.E. Sweeney / Journal of Neuroscience Methods 128 (2003) 53�/6556
distributes its data to the appropriate cells on the
subject worksheets, and moves each file to the
‘‘archive’’ directory specified in cell ‘‘B6’’ of the
‘‘Setup Information’’ worksheet. Annotated ex-cerpts of the VBA code that performs these tasks
for the data files produced by the startle system are
provided in section 3 of Appendix A.
3) After 5 days of baseline data have been collected,
the user must click the ‘‘Do Pair Matching’’ button
to pair match the subjects in the study into equal
groups based on the variables specified in columns
‘‘B’’ and ‘‘C’’.4) The user can also update the graphs daily by
clicking the ‘‘Do Group Averages’’ button to launch
the macro that computes daily averages across
subjects for each group and updates the ‘‘Group’’
worksheets. This function is only enabled after pair
matching has been completed. Whenever the group
worksheets are updated, the charts linked to those
sheets are updated automatically.
2.3. Pair matching the subjects into groups
After the baseline period, information about each
animal’s baseline responses is available in the subject’s
worksheet. Worksheet formulas provide each subject’s
average response (averaged over 5 days) for each of the
two measures displayed in columns ‘‘B’’ and ‘‘C’’ of the‘‘Pairmatch’’ worksheet. Column ‘‘D’’ gives the average
of the two values for a subject and is used in the pair
matching process. Since variability exists in the strength
of individual responses to startle eliciting stimuli,
animals are pair matched to create individual treatment
groups that start out with similar group means and
variances. Pair matching is performed by clicking the
‘‘Do Pair Matching’’ button. In addition, this processsystematically assigns animals to behavioral test cham-
bers to assure that all treatment conditions are repre-
sented as equally as possible across test chambers.
Programmatically, pair matching was accomplished by
rank ordering the subjects based upon the magnitude of
their acoustic startle responses to 100 and 120 dB
stimuli. Subjects were then pulled by rank order and
assigned to study groups in a rotating fashion. Eachgroup is thereby assigned similar numbers of strong and
weak responders, balancing both group means and
variances.
Fig. 4 shows the ‘‘Pairmatch’’ worksheet after the pair
matching operation has been completed and the subjects
have been assigned into study groups. The information
in column ‘‘M’’ can be used to verify that the two study
groups within each treatment group in this examplehave similar group means. Pair matching can be done
only once, and the button is disabled after the procedure
has been performed.
2.4. Group averaging and statistical output
Any time after pair matching has been completed, the
‘‘Do Group Averages’’ button can be clicked to launch amacro that performs averages across all subjects within
a group for each day of the study. The ‘‘Group’’
worksheets are updated by this operation. Since the
charts are linked to the group worksheets, they are also
updated automatically. The format of the group work-
sheets is similar to the subject worksheet shown in Fig.
5. A sample chart worksheet is shown in Fig. 6.
At the end of the study, the user can click the ‘‘CreateStats Pages’’ button to launch a macro that copies the
‘‘STATS X’’ worksheet to a new worksheet named
according to the title given in ‘‘column ‘X’ in the
Pairmatch sheet’’. The information on the ‘‘Pairmatch’’
worksheet in cell ‘‘W4’’ is used to define the number of
‘‘STATS’’ worksheets to be created. There are 12
statistics titles listed in ‘‘X5 to X16’’ in this example,
but the figure in cell ‘‘W4’’ is eight. Thus, only the firsteight statistics sheets are created. The appropriate
dependent variable column on ‘‘Subject’’ worksheets
to be used in generating each statistics summary work-
sheet is defined by the list of numbers in cells ‘‘W5 to
W16’’. Each ‘‘STATS’’ output worksheet is named
based on the list of labels in cells ‘‘X5 to X16’’. Each
statistical output worksheet contains all the data in the
study for a single dependent variable (defined by thedata column number entered into the list starting at
‘‘W5’’). The subject, group and treatment identifiers as
well as the date values are also included in the statistical
output worksheets. A sample statistical output work-
sheet is shown in Fig. 7.
3. Discussion
The Microsoft Excel† component of the Office Suite
must be installed on the processing computer to use
VBA macros to organize and process data in the manner
described in this paper. This approach is most useful
when there are numerous data files generated by
laboratory equipment (in a standardized fixed format)
and when the data processing requires repetitive, time
consuming operations to retrieve individual subject datafrom these files, organize the data, perform descriptive
statistics and graph the data, export the data to a
statistics package, and store the original data files in an
organized fashion. Because the approach uses specia-
lized code to read the data file format output by
laboratory software, the addition of new file formats
to be read will require programming skills in the VBA
language to write the EXCEL macro programs to processdata files. We have successfully written templates
containing the proper code to manipulate data files for
the Hamilton�/Kinder Acoustic Startle System; the San
M.L. Sipos, R.E. Sweeney / Journal of Neuroscience Methods 128 (2003) 53�/65 57
Fig. 4. As the study progresses through the baseline period, the daily raw data files are manually loaded into the ‘‘Unprocessed Data’’ directory and
the ‘‘Crunch New Data’’ button is pressed to launch a VBA macro that reads each raw data file; processes the file and distributes the results into the
subject worksheets; and optionally moves the data file to the ‘‘Processed Data’’ directory. A running average is kept for each subject’s ‘‘response’’
measured by a selected variable. At the end of the baseline period, the ‘‘Do Pair Matching’’ button is clicked to automatically assign the subjects into
the study groups (based upon each animal’s response strength) so that group averages and variances are similar prior to treatment. This figure
presents the worksheet, as it would appear after pair matching has been done. See Fig. 2 legend for the details of the study included in this example.
Fig. 5. The daily values of multiple variables measured for each animal are stored in the individual subject worksheets shown here. These worksheets
are filled automatically when the ‘‘Crunch New Data’’ button is clicked. The sheet is organized to contain the results of a single session in each row.
Columns are dedicated for date, group, and session identifiers as well as for each of the measured variables. See Fig. 2 legend for the details of the
study included in this example.
M.L. Sipos, R.E. Sweeney / Journal of Neuroscience Methods 128 (2003) 53�/6558
Fig. 6. Whenever the ‘‘Do Group Averages’’ macro updates a group worksheet, each chart in the workbook is automatically updated with the new
values. This allows the researcher to continuously track the course of the experiment as new data points are collected. In this particular example, 24
subjects were injected with either saline or 0.4�/LD50 VX. This graph shows the peak startle amplitude (Newtons) 30 min post-injection for the
duration of the study.
Fig. 7. The statistical output worksheets are formatted for easy import into SigmaStat† or other statistical package. See Fig. 2 legend for the details
of the study included in this example.
M.L. Sipos, R.E. Sweeney / Journal of Neuroscience Methods 128 (2003) 53�/65 59
Diego Instruments, Inc. Active Avoidance System; the
BioMedic Data Systems’ ELAMS chip system; and the
Med Associates Inc. Operant Conditioning System.
Using VBA macros in conjunction with MicrosoftExcel has allowed us to visualize daily changes in
behavioral data easily, has significantly reduced the
number of data entry errors, has greatly increased the
speed at which the data can be processed (from hours to
seconds), and has freed skilled technicians for more
important tasks. In addition, data files are easily
organized and retrieved, subjects are easily pair matched
and assigned to treatment conditions or test chambers,and data are rapidly transformed for direct import into
statistical packages for rapid evaluation of experimental
outcomes.
Acknowledgements
The United States Army Medical Research and
Materiel Command supported this work under Depart-ment of Defense contract DAAH04-96-0086, Task 685,
administered by Battelle.
Appendix A
Appendix section 1: VBA code to build a workbook
from setup information.A template workbook containing all of the code is
available upon request. In this section, we will present
some of the more interesting code fragments. So that the
code may be discussed in the text, we include a numeric
index with each statement. Although the code fragments
presented in the following sections are numbered con-
secutively, the fragments are not necessarily pulled from
sections of code that are contiguous. Examples oftransferring data to or from the setup worksheet of
the active workbook are given below:
001 NumberOfTreatments�/Worksheets(‘‘Setup Infor-
mation’’).Cells(11,2).value
002 SubjectsPerGroup�/Worksheets(‘‘Setup Informa-
tion’’).Cells(5,2).value
003 FirstSubject�/Worksheets(‘‘Setup Informa-tion’’).Cells(7,2).value
004 NumberOfGroupsPerTreatment�/Work-
sheets(‘‘Setup Information’’).Cells(4,2).value
If the code must specify a cell in a workbook that is
not currently active, then a fully formed cell reference is
required in the format ‘‘Workbooks(wn).Work-
sheets(sn).Cells(r,c)’’ where ‘‘wn’’, ‘‘sn’’, ‘‘r’’, and ‘‘c’’
are variables that contain the workbook and worksheetnames and the desired row and column of the cell. A
reference of this type can be used on the right-hand side
of the equal sign in a VBA assignment statement cause
the value of the cell to be read. Use of the reference on
the left hand side causes a new value to be written to the
cell.
Creating group sheets: The total number of studygroups was defined as the number of groups per
treatment times the number of treatments. The group
sheets were created by copying the ‘‘Group X’’ sheet to a
new worksheet (line 8) and renaming the new worksheet
appropriately (line 9) as shown below:
005 NumberOfGroups�/NumberOfTreatments *
NumberOfGroupsPerTreatment006 For t�/NumberOfTreatments To 1 Step-1
007 For i�/NumberOfGroupsPerTreatment To 1 Step-
1
008 Worksheets(‘‘Group X’’).Copy after:�/Work-
sheets(‘‘Group X’’)
009 Worksheets(‘‘Group X (2)’’).Name�/‘‘Group’’_
& (t�/1) * NumberOfGroupsPerTreatment�/i
010 Worksheets(‘‘Setup Information’’).Cells((t�/1) *NumberOfGroupsPerTreatment_
�/i�/23,7)�/Worksheets(‘‘Setup In-
formation’’).Cells(1�/t,7) & ‘‘�/’’_
& Worksheets(‘‘Setup Information’’).Cells(12�/i,7)
011 Next i
012 Next t
Notice the use of the underscore character on line 10
to indicate that the statement is continued on subse-
quent lines. Since new group sheets were insertedimmediately to the right of the template sheet, the
stepping increment was set to minus one (instead of the
conventional positive one) so that the created sheets
were ordered numerically from left to right in increasing
order. The following fragment was used to clean up the
portions of the groups’ list on the ‘‘Pairmatch’’ work-
sheet and to place a thick border around the groups that
remained:
013 Worksheets(‘‘Pairmatch’’).Range(‘‘I’’ & 2�/Num-
berOfGroupsPerTreatment * _
NumberOfTreatments & ‘‘:O41’’).Delete (xlShift-
Up)
014 Worksheets(‘‘Pairmatch’’).Range(‘‘I2:O’’ & 1�/
NumberOfGroupsPerTreatment * _
NumberOfTreatments).BorderAround weight:�/
xlThick
Creating subject sheets: The number of subjects
required in the study was computed (line 15), and it
was assumed that they were numbered sequentially
starting with the first subject’s number (line 17). The
subject worksheets were then created (lines 18, 19) and
identifying entries were placed into a list on the
‘‘Pairmatch’’ worksheet (lines 23, 24):
015 NumberOfSubjects�/NumberOfGroupsPerTreat-
ment_
M.L. Sipos, R.E. Sweeney / Journal of Neuroscience Methods 128 (2003) 53�/6560
* NumberOfTreatments * SubjectsPerGroup
016 For i�/NumberOfSubjects To 1 Step-1
017 subject�/FirstSubject�/i�/1
018 Worksheets(‘‘Subject X’’).Copy after:�/Work-sheets(‘‘Subject X’’)
019 Worksheets(‘‘Subject X (2)’’).Name�/‘‘Subject’’ &
subject
020 Next i
021 For i�/1 To NumberOfSubjects
022 subject�/FirstSubject�/i�/1
023 Worksheets(‘‘Pairmatch’’).Cells(i�/1,1)�/subject
024 Worksheets(‘‘Pairmatch’’).Cells(i�/1,6)�/subject025 Next I
The subject numbers are placed in both columns ‘‘A’’
and ‘‘F’’ of the worksheet because some studies refer to
subjects both numerically and by a name.
Cleaning the ‘‘Pairmatch’’ sheet: The subject list on
the ‘‘Pairmatch’’ worksheet of the template workbook
contained 200 lines of references to subject sheets. The
unused cells from the subject list were deleted (line 27).
Borders were placed around the columns ‘‘A’’ to ‘‘G’’and around column ‘‘H’’ of the remaining section (lines
31, 32):
026 Dim highestitem, lowrow, highrow As Integer
027 Worksheets(‘‘Pairmatch’’).Range(‘‘A’’ & 2�/Num-
berOfSubjects & ‘‘:G200’’).Delete_
(xlShiftUp)
028 For i�/1 To NumberOfGroupsPerTreatment *NumberOfTreatments
029 lowrow�/2�/SubjectsPerGroup * (i�/1)
030 highrow�/1�/SubjectsPerGroup * (i�/1)�/Sub-
jectsPerGroup
031 Worksheets(‘‘Pairmatch’’).Range(‘‘A’’ & lowrow &
‘‘:G’’ & highrow).BorderAround_
weight:�/xlThick
032 Worksheets(‘‘Pairmatch’’).Range(‘‘H’’ & lowrow& ‘‘:H’’ & highrow).BorderAround_
weight:�/xlThick
033 Worksheets(‘‘Pairmatch’’).Cells(i�/1,14)�/lowrow
034 Worksheets(‘‘Pairmatch’’).Cells(i�/1,15)�/high-
row
035 Next i
Assigning subjects to treatment groups: Activation of
the ‘‘Pairmatch’’ worksheet caused it to be displayed,providing feedback for the user during the execution of
the macro. Treatments were assigned to the subject in a
balanced fashion by building a list of sequential integers
into an array called ‘‘treat’’ such that the list contained
equal amounts of each treatment number.
036 Worksheets(‘‘Pairmatch’’).Activate
037 Worksheets(‘‘Pairmatch’’).Cells(1,7)�/‘‘Treat-ment Group’’
038 Dim rndary(3000),treat(3000) As Integer
039 Dim trt, grp, subj, c As Integer
040 c�/0 ‘SET A COUNTER TO ZERO
041 For trt�/1 To NumberOfTreatments
042 For grp�/1 To NumberOfGroupsPerTreatment
043 For subj�/1 To SubjectsPerGroup044 c�/c�/1 ‘INCREMENT THE COUNTER
045 rndary(c)�/c ‘SET AN ARRAY OF IN-
CREASING NUMBERS
046 treat(c)�/trt ‘SET AN ARRAY OF TREAT-
MENT NUMBERS
047 Next subj ‘STEP THROUGH SUBJECTS IN
A GROUP
048 Next grp ‘STEP THROUGH THE GROUPS049 Next trt ‘STEP THROUGH THE TREAT-
MENTS
050 num�/c ‘INITIALIZE A DOWN-COUNTER
051 numsave�/c ‘SAVE THE NUMBER OF AR-
RAY ELEMENTS
The built-in random number generator was initialized
(line 52), and random numbers were generated using the
built-in variable ‘‘Rnd’’ (line 56), which returns a
random value between 0 and 1 each time it is accessed.
Random integers between 0 and the current value of
‘‘num’’ were generated and were used to randomly pick
(line 57) without replacement (lines 58�/61) elements
from the list. The picked value was used to assign a
treatment to the subjects on the ‘‘Pairmatch’’ worksheet:
052 Randomize ‘INITIALIZE
THE RAN-
DOM GEN-
ERATOR
053 c�/0 ‘RESET ‘‘C’’
FOR USE AS
A COUNTER054 For i�/numsave To 1 Step-1 ‘STEP FROM
END TO
START OF
ARRAYS
055 c�/c�/1 ‘BUMP THE
OUTPUT
ROW COUN-
TER056 rval�/Int((num * Rnd)�/1) ‘GET A RAN-
DOM BE-
TWEEN 1
AND ‘‘NUM’’
057 Worksheets(‘‘Pairmatch’’).Cells(1�/
c,7)�/treat(rval)
‘OUTPUT
THAT ELE-
MENT
058 For j�/rval To numsave ‘SLIDE THEREST OF THE
ARRAY FOR-
WARD
M.L. Sipos, R.E. Sweeney / Journal of Neuroscience Methods 128 (2003) 53�/65 61
059 rndary(j)�/rndary(j�/1) ‘TO PRO-
DUCE A
SHORTER
ARRAYMISSING
060 treat(j)�/treat(j�/1) ‘THE ELE-
MENT THAT
WAS USED
061 Next j
062 num�/num�/1 ‘SHORTEN
THE
LENGTH OFTHE ARRAY
063 Next I ‘AND RE-
PEAT UNTIL
ALL ELE-
MENTS
HAVE BEEN
USED
Creating and customizing the chart sheets: To alter
settings for the charts, a string specifying the data rangein the ‘‘A1’’ format had to be created from column
numbers specified by integers. The following code
fragment provides one way to accomplish the task for
column numbers less than 53. The ‘‘If, Then, Else’’
construct can easily be nested to allow for higher
column numbers. The fragment provides the proper
source string, ‘‘rm’’, that refers to the data from row
‘‘lowitem’’ to row ‘‘highestitem’’ in the column indicatedby the integer ‘‘col’’.
064 Dim lowitem, highitem As Integer
065 Dim sourcespecifier, colstr As String
066 lowitem�/11
067 highestitem�/70
068 If col�/26 Then
069 colstr�/Chr(64�/Int((col�/1)/26)�/(Int((Int((col�/
1)/26)�/1)/26) * 26))_
& Chr(64�/col�/(Int((col�/1)/26) * 26))
070 Else
071 colstr�/Chr(64�/col�/(Int((col�/1)/26) * 26))
072 End If
073 rm�/colstr & lowitem & ‘‘:’’ & colstr & highestitem
The charts were created from a template chart using
copy and rename operations:
074 Charts(‘‘Chart X’’).Copy after:�/Charts(‘‘Chart
X’’)
075 chartname�/‘‘AM 100dB Chart’’
076 Charts(‘‘Chart X (2)’’).Name�/chartname
The chart was accessed using a chart object (line 77)
that was set to a member of the charts collection (line
78) and the chart titles were changed (lines 79�/81):
077 Dim ch As Chart
078 Set ch�/Charts(chartname)
079 ch.ChartTitle.Text�/‘‘title across the top of the
chart window’’
080 ch.Axes(xlValue).AxisTitle.Text�/‘‘y axis title’’
081 ch.Axes(xlCategory).AxisTitle.Text�/‘‘x axis title’’
The individual data series in the chart were accessed
using a series object that was set to a member of the
series collection for the chart (line 84):
082 Dim s As Series
083 g�/4
084 Set s�/ch.SeriesCollection(g) ‘GET THE
4th DATA
SERIES
(GROUP 4)
085 s.Values�/Worksheets(‘‘Group’’ &
g).Range(rm)
‘SET THE
Y-AXISSOURCE
DATA
086 s.XValues�/Worksheets(‘‘Group’’ &
g).Range(rx)
‘SET THE
X-AXIS
SOURCE
DATA
087 s.Name�/Work-
sheets(‘‘Pairmatch’’).Cells(1�/
g,9).value
‘SET THE
SERIESNAME
‘SET THE
ERROR
BAR
SOURCE
DATA
088 s.ErrorBar Direction:�/xlY, In-
clude:�/xlErrorBarIncludeBoth, _Type:�/xlErrorBarTypeCustom,
Amount:�/Worksheets(‘‘Group’’ &
g).Range(re), _
MinusValues:�/Work-
sheets(‘‘Group’’ & g).Range(re)
089 Dim e As ErrorBars
090 Set e�/s.ErrorBars
091 e.EndStyle�/xlCap ‘AND SETTHE ER-
ROR BAR
STYLE
The data range (lines 85, 86), name (line 87), and error
bar values (line 88) were set to values on the ‘‘Group’’
and ‘‘Pairmatch’’ worksheets. The error bar style was set
to the ‘‘cap’’ style (line 90).
Deleting unused groups from the charts: Each of the
ten data series in the template chart points to a similar
plotted variable on the group sheets. If the experiment
did not use all ten groups, some data series needed to be
deleted from the chart. This was done using the‘‘Delete’’ method of the ‘‘SeriesCollection’’ object for
the chart to delete a data series. The following line
deletes the 5th data series:
M.L. Sipos, R.E. Sweeney / Journal of Neuroscience Methods 128 (2003) 53�/6562
092 ch.SeriesCollection(5).Delete ‘DELETE AN UN-
USED SERIES IN
THE CHART
Note that after the delete operation the seriesnumbers are recalculated so that what was the sixth
series before line 92 above was executed becomes the
fifth series after execution of the line.
Hiding the template worksheets: To avoid confusion,
the template worksheets were hidden after they were no
longer needed (lines 94�/96). The ‘‘Display Alerts’’
feature of the EXCEL application was toggled disabled
(lines 93, 97) during the operation to avoid a messagebox requesting user confirmation of the actions:
‘HIDE THE CHART, GROUP, AND SUBJECT
TEMPLATE WORKSHEETS
093 Application.DisplayAlerts�/False
094 Charts(‘‘Chart X’’).Visible�/False
095 Worksheets(‘‘Group X’’).Visible�/False
096 Worksheets(‘‘Subject X’’).Visible�/False097 Application.DisplayAlerts�/True
Appendix section 2: VBA program code to find and
read instrument data files.
Only the more interesting aspects of the code are
presented in the interest of space. The complete code
listing is available from the authors.
Improving performance by suspending auto calculation
mode: The EXCEL workbook will evaluate the formula
assigned to a cell and place the new value in the cell
whenever one of the inputs to that formula changes. The
user may not notice the delay when values are entered by
hand. However, when automated entry of data into cells
used as inputs to functions is done, the delay caused by
the repetitive evaluations can accumulate to produce
prolonged and unnecessary delays. The ‘‘autocalcula-tion’’ feature was disabled and the ‘‘calculate on save’’
feature was enabled to avoid the repetitive recalculations
and to ensure that at least one evaluation takes place
before the user saves the workbook.
‘SET EXCEL FOR MANUAL CALCULATION
TO INCREASE SPEED
098 Application.Calculation�/xlCalculationManual099 Application.CalculateBeforeSave�/True
Before subroutine exited, the features were restored.
‘FORCE A WORKBOOK CALCULATION
AND RESET CALCULATION MODE
100 Application.Calculate
101 Application.Calculation�/xlCalculationAutomatic
Finding files to be processed: The method usedassumed that the user had manually moved any raw
data files to be processed into an ‘‘input file’’ directory
specified on the ‘‘Setup Information’’ worksheet. A file
search object (line 102) was used to look for all raw data
files in the input directory (line 103). In this example, the
input files had ‘‘.CSV’’ filename extensions (line 104).
After the file search has been executed (line 105), thenumber of files found (line 106) and the found file names
(line 108) were returned from the object. Any file loaded
by EXCEL becomes the active worksheet so the ‘‘Pair-
match’’ sheet was activated (line 110�/114) after file
processing operations were completed.
‘START LOOP THROUGH SUBJECT’S FILES
FOR USER ENTERED DATE102 Set fs�/Application.FileSearch
103 fs.LookIn�/Worksheets(‘‘Setup Informa-
tion’’).Cells(3,2)
104 fs.Filename�/ ‘‘*.csv’’
‘EXECUTE A FILE SEARCH USING THE
FILESEARCH OBJECT
105 If fs.Execute�/0 Then
‘FOR AS MANY FILES AS WERE FOUND106 filecount�/fs.FoundFiles.count
107 For FileNum�/1 To filecount
108 fname�/fs.FoundFiles(FileNum)
(Process the data file, pulling information from it
and loading the information into the appropriate
rows and columns of the subject sheets. Move the
data file after it has been processed if the user
desires.)109 Next FileNum
‘ACTIVATE THE PAIRMATCH SHEET AND
CONFIRM UPDATES
110 Worksheets(‘‘Pairmatch’’).Activate
111 MsgBox (filecount & ‘‘updated sessions’’)
112 Else
‘ACTIVATE THE PAIRMATCH SHEET AND
CONFIRM NO-FILES113 Worksheets(‘‘Pairmatch’’).Activate
114 MsgBox ‘‘There were no files found.’’
115 End If
Moving raw data files after they are processed: The
example presented in this paper was designed for files
produced by the Hamilton�/Kinder Acoustic Startle
System, which produces comma separated value files
with the raw data. The code to conditionally move
processed data follows:
‘DOES USER WISH TO MOVE INPUT FILES
AFTER THEY ARE PROCESSED?
115 Dim DataMove As Boolean
116 DataMove�/False
117 If Worksheets(‘‘Pairmatch’’).Cells(26,28).value�/
‘‘Yes’’ Then DataMove�/True
After the file was processed:
‘IF USER REQUESTED IT, RENAME THE
CSV FILE TO A NEW LOCATION
M.L. Sipos, R.E. Sweeney / Journal of Neuroscience Methods 128 (2003) 53�/65 63
118 Dim OldDirectory, NewDirectory
119 OldDirectory�/Worksheets(‘‘Setup Informa-
tion’’).Cells(3,2)
120 NewDirectory�/Worksheets(‘‘Setup Informa-tion’’).Cells(6,2)
121 Dim OldName, NewName
122 OldName�/OldDirectory & ‘‘\’’ & Name
123 NewName�/NewDirectory & ‘‘\’’ & Name
124 If DataMove�/True Then Name OldName As
NewName
Processing each raw data file: The manner in which a
raw data file is processed will depend on many different
constraints. Different types of instrumentation produceraw data files in different formats. Additionally, users
may choose to name data files according to various
criteria or to enter different information into instrument
data fields. Only the most basic processing steps are
described here for the Hamilton�/Kinder (.CSV) data
format.
At our laboratory, the data collection date is encoded
into the file name. The EXCEL program will open commaseparated value files as a workbook with a single
worksheet with the file’s name.
‘OPEN THE CSV FILE (EXCEL
WILL PULL IT INTO A WORK-
BOOK
125 Workbooks.Open (fname)
‘GET THE FILE DATE EN-CODED INTO THE NAME
126 Name�/Application.ActiveWork-
book.Name
127 fileday�/Mid(Name,1,2) ‘DAY
128 fileMonth�/Mid(Name,3,3) ‘MONTH
129 session�/Mid(Name,6,2) ‘RUN NUM-
BER
130 sheetname�/fileday & fileMonth &session
131 indate�/fileday & ‘‘’’ & fileMonth &
‘‘2003’’
‘NOTE THE
HARDCODED
YEAR
‘SORT THE CSV FILE’S WORK-
BOOK BY ENCLOSURE THEN
TRIAL TYPE
132 Selection.Sort Key1:�/Range(‘‘I2’’),Order1:�/xlAscending, Key2:�/
Range(‘‘M2’’)_
, Order2:�/xlAscending, Header:�/
xlGuess, OrderCustom:�/1_
, MatchCase:�/False,
Orientation:�/xlTopToBottom
At this point, the rows in the raw data worksheet were
examined, and the average values for peak response,peak latency and average response were computed. We
ignored the first trial of a particular type and based the
averages on the remaining trials. The averaged values
were saved in arrays and were distributed to the proper
subject sheets after the raw data file was closed:
‘CLOSE THE CURRENT CSV WORKBOOK
WITHOUT SAVING CHANGES133 Workbooks(Name).Close SaveChanges:�/False
Distributing data to the subject sheets: This relatively
straightforward process requires no example code frag-
ments. The only point of interest is how multiple
sessions in a single day were handled. Each animal in
the example study (see Fig. 2) was tested twice a day
(once in the morning and once in the afternoon). The
startle system used during the study had eight separate
enclosures so that eight subjects could be tested in asingle session. Running 16 sessions per day allowed a
maximum of 64 animals to be tested twice a day (eight
morning sessions and eight afternoon sessions). The 16
raw data files produced each day were assigned session
numbers encoded into the file name. Data from the first
eight sessions were stored in the ‘‘AM’’ data columns on
the subject sheets while the data from the second eight
sessions were stored in the ‘‘PM’’ data columns. The
column set to be used was computed from the session
number. Each row on a subject sheet corresponded todata collected on a particular date, which was stored in
the first column of the row. Given the date that the data
was collected (from the raw data file name), the program
simply scanned down the first column looking for a date
that matched the test date in order to locate the row into
which the new data should be placed. If no matching
row was found, the next available empty row was started
for the test date.
Processing data files that EXCEL cannot open directly:
Some instruments produce data files that cannot be
opened conveniently by EXCEL. We have included a
fragment of VBA code that can be used to open text
files.
‘TO OPEN A FILE
NAMED fname
134 Open (fname) For Input
As #1‘TO READ THE NEXT
LINE FROM FILE (TO
LINE FEED) INTO A
STRING
135 Dim InputLine as String
136 Line Input #1, InputLine ‘THIS STATEMENT
WOULD BE RE-
PEATED‘TO GET DATE FROM
SOME PART OF THE
LINE JUST READ
137 Dim part as String
M.L. Sipos, R.E. Sweeney / Journal of Neuroscience Methods 128 (2003) 53�/6564
138 part�/Mid(Input-
Line,10,8)
‘GET 8 CHARAC-
TER STARTING AT
POSITION 10
‘TO CLOSE THE FILEAFTER IT HAS BEEN
READ
139 Close #1
Appendix section 3: VBA program to perform group
averages.
This straightforward procedure averages the dataitems from corresponding dates and columns for the
subjects that are members of a group and places the
average, count, and standard deviation of those values
into the group sheet for that group. The process is
repeated for each date of each group. No code fragment
is given. It is critical to disable the auto calculation
feature beforehand and to restore it afterwards. Other-
wise, a complete set of chart updates (based on new data
in the group sheet) will be done for each entry onto the
group sheets. The only interesting point is that the user
must ensure that appropriate subject rows are included
in the averages. In the case described in this paper, all
subjects were treated on the same daily schedule. If there
is more than one daily schedule in a study, we have
found it useful to define a ‘‘study day’’ column that can
be used with a listing of subject starting dates to preserve
the study’s time information. In such a case, the group
average should include same study day across the
group’s subjects.
References
Jacobson R. EXCEL 2000 VISUAL BASIC FOR APPLICATIONS: Funda-
mentals. Redmond, WA: Microsoft Press, 1999.
Wells E, Harshbarger S. Microsoft† Excel 97 Developer’s Handbook.
Redmond, WA: Microsoft Press, 1997.
M.L. Sipos, R.E. Sweeney / Journal of Neuroscience Methods 128 (2003) 53�/65 65
top related