manipulating data files in excel

Upload: nithin-santhosh

Post on 05-Apr-2018

229 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 Manipulating Data Files in Excel

    1/24

    Manipulating data files in ExcelIntroduction.

    The purpose of this document is to show how you can, with a few simple macros, manipulate data files which you havecreated or been given. The files may be in Excel already or may be Comma Separated Variable (CSV) files. Most computersdefault to opening CSV files as a single worksheet in Excel so, apart from remembering to save them as an Excel workbook

    they can be treated the same.The layout of the file may contain all or some of the items shown in the diagram. We will only be concerned with the Dataand its associated column headings.

    The data section will contain one or more columnsand one or more rows. In some cases all the rows willhave data, in some cases there will be blanks. Ourmacros will have to be able to deal with allpossibilities.

    There will be times when the problem to be solved isvery specific and times when there are several sets of data that need to be processed so it is important to beclear what your macro will do and what it wont. Thisdocument will contain several macros which you cancopy, paste and modify.

    Types of problems.

    Extracting a subset of data.

    Merging two sets of data into one

    Splitting a data set into two or more parts.

    Performing calculations on each record inthe data.

    General Information

    Column Headings

    Data

    General Information

  • 8/2/2019 Manipulating Data Files in Excel

    2/24

    A Generic view of data processing programs

    The following diagram shows the layout of a typical Data Processing Program. Each part is simple or more complicateddepending on the particular problem and the data given

    1: Start up operations include such things as findingwhere the data starts, copying over column headings if required and handling the first record(s) if these require adifferent procedure from the rest

    2: This type of process is repetitive so before we enter theloop we check whether we have a record to process.Although it may seem odd we check the first record aswell so the macro will work even with an empty list.Deciding if there are any more records can be simple if there are no blanks in the list or there is a known lastrecord. Otherwise some rule has to be devised.

    3: Depending on the job not all records may be required,so we ignore any we arent interested in and skip to step5

    4: Processing may include joining fields or doingarithmetic on values. If appropriate, we write out theresult of this processing remembering where we writeeach result as it may not be on the same line as theincoming data, or even the same sheet.

    5: Getting the next record in this type of process isusually looking at the line below the current one.

    6: When all the records have been processed then theremay still be some more information to be written. EgNumber of records written or column totals. In the caseof merging two lists when the end of one list is reachedall the remaining records in the other can be copied enmasse.

    3: Do I wantto process

    this record?

    4: Perform any processing requiredWrite the result in the next available

    space

    2: Do I haveany recordsto process

    Yes

    No

    Yes

    5: Look for next record and loopback to step 2

    Start

    No

    Sto

    6: Perform any finishing operations

    1: Perform any start up operations andlook for first record

  • 8/2/2019 Manipulating Data Files in Excel

    3/24

    Excel Basics

    Opening and saving files.

    If your data has been sent to you as a CSV (Comma Separated Variable) File the if you double click on it in WindowsExplorer it will automatically be opened up in Excel. To open it from within Excel, the in the File Open window, change theFiles of Type option at the bottom of the window to Text Files. Having opened the CSV file it is best to save it as an ExcelFile or to move the sheet created into an Excel File.

    Moving and/or copying worksheets.

    At the bottom of the Excel screen there are a row of tabs, one for each sheet. Right Clicking on this tab brings up a sub menuwhich enables you to rename the sheet and or move or copy it to a new location. Thus if you data comes as several CSV filesyou can open an Excel Work book, then open the CSV files in turn and move them into your main work book.

    Creating a place for your Macros

    On then Menu line click on Tools then Macro (click the do uble arrow if Macro doesnt appear) then Visual Basic Editor. (Or hold down the key and press the key)

    This opens a separate program linked to the workbook. From its menu line choose Insert and then Module. This brings up asheet on which you can write your macros.

    Important rule of macros

    There is never just one solution to creation a macro

    If it works its right if it doesnt its wrong

  • 8/2/2019 Manipulating Data Files in Excel

    4/24

    Writing a macro

    All Macros in this course will be based on the file data.xls.

    For the First Macro we will use the data on the names sheet

    Every macro has to have a single word name. Let us begin by writing a macro to count the number if items in a list. (Theremay be simpler ways of doing this but this is just an example of how macros work.) Well call the m acro count . So in your

    module typesub count

    and press

    Excel will respond by changing this to

    Sub count()

    End Sub

    Note that some words are in blue. These are special wordswhich have meaning to Excel and these words can only beused for that purpose.

    Following the plan to the left we begin with step one, startup operations and getting the first record. Let us assume thatthe data is in column C starting at row 5. We will start at thislocation and go down column C looking at rows 6,7,8,..until we find an empty cell counting as we go. To do this weneed to remember two things, the row we are on and howmany records we have seen so far. We give a name to eachof these pieces of information, rw for the row and nr for thenumber of records. When we start we have seen no recordsso nr = 0 and we are at row 5 so rw = 5. We enter thisinformation in our macro.

    Sub count()nr = 0 rw = 5 End Sub

    As we want to process (ie count) all records step 3 is omittedand step 4 will not need to write anything as we only writeout the answer when we have finished. When we findanother record we can increase the number of records ( nr )by one using the statement

    nr = nr + 1

    How do we know that we have found a record? A recordexists if the cell on the worksheet that we are looking at isnot empty. In this case if we do find an empty cell then wehave reached the end of our list.

    To look at a cell on the worksheet we refer to it as

    Cells(rownumber,columnnumber)

    Note that we use column numbers rather than letters so our first record in cell C5 would be

    Cells(5,3) or as we remember which row we are in by using rw we can refer to this first record as Cells(rw,3) The column wont change so we just use its number. There is a special phrase we use to find out if a cell hasa value or is empty: this is

    IsEmpty(Cells(row,column))

    This returns an answer True or False.

    The program runs through stages 2,3,4,5, ,2,3,4,5, as long as there are records. Ie steps 2,3,4 & 5 are repeated. This iscalled a loop and the statements we use are as follows

    Do While some condition is True

    3: Do I wantto process

    this record?

    4: Perform any processing requiredWrite the result in the next available

    space

    2: Do I haveany recordsto process

    Yes

    No

    Yes

    5: Look for next record and loopback to step 2

    Start

    No

    Sto

    6: Perform any finishing operations

    1: Perform any start up operations andlook for first record

  • 8/2/2019 Manipulating Data Files in Excel

    5/24

    Loop

    or

    Do Until some condition becomes True

    Loop

    Getting the next record is a matter of increasing the row number rw by one.

    Putting all this together we get.

    Sub count()nr = 0 rw = 5Do Until IsEmpty(Cells(rw,3))

    nr = nr+1rw = rw+1

    Loop

    End Sub

    We now need to perform the finishing operations which in this case is to write out the answer. In this example we write the

    words Number of Records in cell B3 and the Value in C3 giving the final macro asSub count()nr = 0 rw = 5Do While IsEmpty(Cells(rw,3))

    nr = nr+1rw = rw+1

    LoopCells(3,2).Value = Number of Records Cells(3,3).value = nrEnd Sub

    Question: What changes would you make to this macro if the data started in cell A1?

  • 8/2/2019 Manipulating Data Files in Excel

    6/24

    Problem 1 : You have some data starting in cells A1 and B1 consisting of the header line Name and Grade . Below that thereis a list of names (always present) and grades A to E or blank. You want to make the names of all people with an A gradeBold. Copy all the count macro and paste it into the module below where it is and rename it bolda as follows

    Sub bolda()nr = 0 rw = 5

    Do Until IsEmpty(Cells(rw,3))nr = nr+1rw = rw+1

    LoopCells(3,2).Value = Number of Records Cells(3,3).value = nrEnd Sub

    Delete the unwanted lines referring to the number of records and the answers thus

    Sub bolda()rw = 5Do Until IsEmpty(Cells(rw,3))

    rw = rw+1LoopEnd Sub

    We will be starting from row 2 (row one is the header) in column 1 (we cant use column 2 as it may contain blanks)

    Sub bolda()rw = 2Do Until IsEmpty(Cells(rw,1))

    rw = rw+1LoopEnd Sub

    We now need to introduce a Section 2 Do I need to process this record? This is done with an IF statement as follows

    Sub bolda()rw = 2Do Until IsEmpty(Cells(rw,1))

    If Cells(rw,2).Value = A Then

    End Ifrw = rw+1

    LoopEnd Sub

    Finally we need to make the name Bold which is done as follows.

    Sub bolda()rw = 2Do Until IsEmpty(Cells(rw,1))

    If Cells(rw,2).Value = A Then Cells(rw,1).Font.Bold = True

    End Ifrw = rw+1

    LoopEnd Sub

  • 8/2/2019 Manipulating Data Files in Excel

    7/24

    Problem 2: Using the same data as in problem 1 create a list of A grade students in columns E and F. Call this macroalist.

    Proceed as before by copying, pasting, renaming and removing unwanted lines to give

    Sub alist()rw = 2Do Until IsEmpty(Cells(rw,1))

    If Cells(rw,2).Value = A Then

    End Ifrw = rw+1

    LoopEnd Sub

    In this problem we have some start up jobs, ie copy the headers to a new location and we will need to keep track of whichrow we are going to use to write the current A student. We could call this information arw Given that the headers are in row1 then will be row 2 to begin with.

    Sub alist()rw = 2arw = 2Cells(1,5).Value = Cells(1,1).Value

    Cells(1,6).Value = Cells(1,2).ValueDo Until IsEmpty(Cells(rw,1))

    If Cells(rw,2).Value = A Then

    End Ifrw = rw+1

    LoopEnd Sub

    When we do find an A student we want to copy the value to our new location and increase the value of arw by one asfollows.

    Sub alist()rw = 2arw = 2

    Cells(1,5).Value = Cells(1,1).ValueCells(1,6).Value = Cells(1,2).ValueDo Until IsEmpty(Cells(rw,1))

    If Cells(rw,2).Value = A Then Cells(arw,5).Value = Cells(rw,1).ValueCells(arw,6).Value = Cells(rw,2).Valuearw =arw+1

    End Ifrw = rw+1

    LoopEnd Sub

  • 8/2/2019 Manipulating Data Files in Excel

    8/24

    How do you know how to make the font bold? What if you wanted to make it Red?

    When you want to achieve in your macro some effect which is fairly easy to do in Excel and you dont know how it is done,create a macro by recording your actions and adapting that. For example if we had wanted to make the As red rather thanbold we could proceed as follows.

    From the menu line choose [Tools] [Macro] [Record New Macro] Accept the defaults by clicking [OK] A little tool bar willappear on the sheet.

    Select a cell change its colour to red and click on the [Stop Recording] button on the small toolbar.

    Go to the Visual Basic Editor, This new macro will be in a module of its own. Find this and you should see something likethe code below.

    Sub Macro1()'' Macro1 Macro' Macro recorded 04/10/2007 by Jerry'

    'Range("A2").Select

    Selection.Font.ColorIndex = 3End Sub

    Lines which begin with an apostrophe () are merely comments or notes and can be ignored.

    The other two lines are the actions you took, first to select a cell and then to change its colour to red. In our macro we hadthe line

    Cells(rw,1).Font.Bold = True

    If we had wanted it to change the colour to red it would have read

    Cells(rw,1).Font.ColorIndex = 3

    To find out which colour has which value go to Visual Basic Help and search for PatternColorIndex

  • 8/2/2019 Manipulating Data Files in Excel

    9/24

    Problem 3 : We have a list of names, each accompanied by two marks. We wish to add the marks together and grade theresult according to the rule Below 80 E, 80 -89 D, 90 -99 C, 100 -110 B above 110 A. Using the work sheet Name -Marks we can see the Data starts on row 2, the marks are in columns E and F (5 and 6) and the results are to go in columns Gand H (7 and 8) on the same row as the data. So start by copying and pasting bolda renaming it grade and removing the linesabout grade A and the font change. (Note we use bolda again rather than alist as alist was writing out the answers ondifferent rows.

    Sub grade()rw = 2Do Until IsEmpty(Cells(rw,1))

    rw = rw+1LoopEnd Sub

    The Processing part of the operation is to add together the two marks and write the result to column 7. This gives us

    Sub grade()rw = 2Do Until IsEmpty(Cells(rw,1))

    Cells(rw,7).value = Cells(rw,5).value + cells(rw,6).valuerw = rw+1

    LoopEnd Sub

    Now we have to grade the result according to the rule above and to do this we use a new Visual Basic Statement Select asfollows.

    Sub grade()rw = 2Do Until IsEmpty(Cells(rw,1))

    Cells(rw,7).value = Cells(rw,5).value + cells(rw,6).valueSelect Case Cells(rw,7).value

    Case 0 to 79Cells(rw,8).value = E

    Case 80 to 89Cells(rw,8).value = D

    Case 90 to 99Cells(rw,8).value = C

    Case 100 to 109Cells(rw,8).value = B

    Case ElseCells(rw,8).value = A

    End Selectrw = rw+1

    LoopEnd Sub

  • 8/2/2019 Manipulating Data Files in Excel

    10/24

    Alternative solution to Problem 3.

    You will have noticed that adding the two cells together could have been don in Excel without writing a macro IE by eitherentering a formula =E2+F2 (on row 2) and extending down or by using the Function SUM as in =SUM(E2:F2). The Gradingpart can also be done by using a function but a function that you write yourself. In your module write Functiongrade(x)

    Excel will respond by adding the end line as follows

    Function grade(x)

    End Function

    Using the lines from the Sub above with minor alterations the Function becomes

    Function grade(x)Select Case x

    Case 0 to 79grade = E

    Case 80 to 89grade = D

    Case 90 to 99grade = C

    Case 100 to 109grade = B

    Case Elsegrade = A

    End SelectEnd Function

    Then in cell h2 enter =grade(g2) and extend down.

  • 8/2/2019 Manipulating Data Files in Excel

    11/24

    Problem 4 . Sheet Subjects has a list of names each accompanied by two subjects. The aim is to create a class list for eachsubject on a separate sheet. There is a blank sheet for each of the four possible subjects.

    Again start with the basic Macro copied and adapted from bolda and called split.

    Sub split()rw = 2Do Until IsEmpty(Cells(rw,1))

    rw = rw+1LoopEnd Sub

    We now look at the values in columns 3 and 4 in turn and write them out on the correct sheet in the correct place. As in thealist macro we need to remember where we can write the result but this time we need four counters one for each sheet. Forconvenience we name the counters after the courses.

    Sub split()rw = 2cabc50=1cabc65=1cpqr51=1cpqr73=1

    Do Until IsEmpty(Cells(rw,1))

    rw = rw+1LoopEnd Sub

    Now using the Select statement again we look at course 1 (column 3) then Course 2 (Column 4)

    Note that you can copy and paste parts of the macro and then edit the small changes necessary. If you are using an electronicversion of this document you can copy and paste the macro from Word to Excel)

  • 8/2/2019 Manipulating Data Files in Excel

    12/24

    Sub split()rw = 2cabc50=1cabc65=1cpqr51=1cpqr73=1Do Until IsEmpty(Cells(rw,1))

    Select Case Cells(rw,3).value

    Case ABC50 Sheets(ABC50).Cells(cabc50,1).value = Cells(rw,1).value Sheets(ABC50).Cells(cabc50,2).value = Cells(rw,2).value Cabc50=cabc50+1

    Case ABC65 Sheets(ABC65).Cells(cabc65,1).value = Cells(rw,1).value Sheets(ABC65).Cells(cabc65,2).value = Cells(rw,2).value Cabc65=cabc65+1

    Case PQR51 She ets(PQR51).Cells(cpqr51,1).value = Cells(rw,1).value Sheets(PQR51).Cells(cpqr51,2).value = Cells(rw,2).value cpqr51=cpqr51+1

    Case PQR73 Sheets(PQR73).Cells(cpqr73,1).value = Cells(rw,1).value Sheets(PQR73).Cells(cpqr73,2).value = Ce lls(rw,2).valuecpqr73=cpqr73+1

    End Select

    Select Case Cells(rw,4).value Case ABC50

    Sheets(ABC50).Cells(cabc50,1).value = Cells(rw,1).value Sheets(ABC50).Cells(cabc50,2).value = Cells(rw,2).value Cabc50=cabc50+1

    Case ABC65 Sheets(ABC65).Cells(cabc65,1).value = Cells(rw,1).value Sheets(ABC65).Cells(cabc65,2).value = Cells(rw,2).value Cabc65=cabc65+1

    Case PQR51 Sheets(PQR51).Cells(cpqr51,1).value = Cells(rw,1).value Sheets(PQR51).Cells(cpqr51,2).value = Cells(rw,2).valuecpqr51=cpqr51+1

    Case PQR73 Sheets(PQR73).Cells(cpqr73,1).value = Cells(rw,1).value Sheets(PQR73).Cells(cpqr73,2).value = Cells(rw,2).value cpqr73=cpqr73+1

    End Select

    rw = rw+1LoopEnd Sub

  • 8/2/2019 Manipulating Data Files in Excel

    13/24

    Problem 5 : Processing in two directions. Sheet Weather has some data showing the condition at four periods in a day for amonth. The aim is to create a list of the times it was raining.

    Once again this is a problem of going down a list, processing each line but the processing involves looking a list of values insuccessive columns.

    As it involves writing out a list of answers we can start with the alist macro deleting those lines which are specific to thatproblem.

    Sub raining()rw = 2arw = 2Do Until IsEmpty(Cells(rw,1))

    If Then

    arw =arw+1End If

    rw = rw+1LoopEnd Sub

    The processing involves looking at the value in column 2, if it is rain the write out the date (column 1) and Morning, Thenlook at column 3 if it is rain the write out the date (column 1) and Afternoon, and so on. If we were only looking at col 2 themacro could look like this

    Sub raining ()rw = 2arw = 2Do Until IsEmpty(Cells(rw,1))

    If Cells(rw,2).Value = Rain ThenCells(arw,7).Value = Cells(arw,1).ValueCells(arw,8).Value = Morning arw =arw+1

    End Ifrw = rw+1

    LoopEnd Sub

    We could then copy the lines from IF to End IF three times changing the column number and the output. But there is asimpler method. Remembering that Morning is in Cells(1,2), Afternoon in Cells (1,3) etc we write

    Sub raining ()rw = 2arw = 2Do Until IsEmpty(Cells(rw,1))

    For cl = 2 To 5 If Cells(rw,cl).Value = Rain Then

    Cells(arw,7).Value = Cells(rw,1).ValueCells(arw,8).Value = Cells(1,cl).Valuearw =arw+1

    End IfNextrw = rw+1

    LoopEnd Sub

    The For Loop works by doing everything inside with CL set to 2, Then 3 and so on until 5

    We could have used the For Loop in the previous example to give us looking at cols 3 then 4

  • 8/2/2019 Manipulating Data Files in Excel

    14/24

    Sub split2()rw = 2cabc50=1cabc65=1cpqr51=1cpqr73=1Do Until IsEmpty(Cells(rw,1))

    For cl = 3 to 4

    Select Case Cells(rw,cl).value Case ABC50

    Sheets(ABC50).Cells(cabc50,1).value = Cells(rw,1).value Sheets(ABC50).Cells(cabc50,2).value = Cells(rw,2).value cabc50=cabc50+1

    Case ABC65 Sheets(ABC65).Cells(cabc65,1).value = Cells(rw,1).value Sheets( ABC65).Cells(cabc65,2).value = Cells(rw,2).value cabc65=cabc65+1

    Case PQR51 Sheets(PQR51).Cells(cpqr51,1).value = Cells(rw,1).value Sheets(PQR51).Cells(cpqr51,2).value = Cells(rw,2).value cpqr51=cpqr51+1

    Case PQR73 Sheets( PQR73).Cells(cpqr73,1).value = Cells(rw,1).value Sheets(PQR73).Cells(cpqr73,2).value = Cells(rw,2).value

    cpqr73=cpqr73+1End SelectNextrw = rw+1

    LoopEnd Sub

  • 8/2/2019 Manipulating Data Files in Excel

    15/24

    Problem 6: You wish to create a list of files and the folders they are in, retaining information about file size and datecreated. Whilst Windows Explorer can show you this information you cant easily print out a list of files in a given folder and its sub folders.

    Using the Dir at a command prompt (See Appendix 2) prints out a list as a text file which can be imported into Excel (Seesheet doclist).

    We are going to write a macro which finds the files and the name of the folder they are in, combines the results and printsout a list with the required information.

    Rather than adapting an existing Macro I am going to go through the process from start to finish building up the macro stage by stage. Most of the code will be code that has been used before but where new commands are introduced Ill offer explanation. Note also that Directory and Folder are synonymous and Ill use both.

    The data looks some thing like this

    Volume in drive C has no label.

    Volume Serial Number is 88DC-005F

    Directory of C:\docs

    02/11/2004 09:18 .

    02/11/2004 09:18 ..

    18/12/2001 12:29 65,024 afm.doc

    20/02/2002 14:11 55,296 afm2.doc

    06/06/2000 07:01 2,949,120 Win98 modems - jan00.doc

    3 File(s) 3,657,216 bytes

    Directory of C:\docs\books\access

    15/10/2003 07:51 .

    15/10/2003 07:51 ..

    11/09/2001 07:55 3,096,064 access.doc

    11/11/1999 10:24 55,296 Accessdata.xls

    10/11/1999 13:46 311 Courses.txt

    3 File(s) 3,151,671 bytes

    And ends like this.

    Total Files Listed:

    267 File(s) 61,879,470 bytes

    71 Dir(s) 20,859,486,208 bytes free

    Things to note straight away is that there are blank lines in the data and some lines contain one sort of information and otherlines contain different sorts of information.

    For Example: Looking at the data we see that we have the directory name in column 1 preceded by the words Directory of

    Directory of C:\docs\books\access

    and then on subsequent rows we have date and size information in column 1 with file name in column 2.11/11/1999 10:24 55,296 Accessdata.xls

  • 8/2/2019 Manipulating Data Files in Excel

    16/24

    These are mixed in with some rows with the sub folder name in column2 and the word appearing in column 1.

    01/07/2004 11:01 fax

    When all files in a given folder have been listed there is a row of the form

    3 File(s) 3,151,671 bytes

    There are also some blank lines. Right at the end there is a line Total Files Listed

    Total Files Listed:

    What we have to do therefore is to

    Start at the top

    Look for a line beginning Directory of .

    Remember this name as until this type of line is repeated all subsequent lines with file names in them are in this folder.

    Search subsequent rows for files, when found combine directory name and file name, write the result somewhere and attachfile information.

    Continue changing directory name as appropriate until we reach a line with Total Files in column 1.

    We have to decide where to put the results, so let us choose column 4 for the filename and column 5 for the file information.

    We need variables to store which row we are searching, which row we are using for the answer and what the currentdirectory name is. We shall use rin, rout and direct for these

    To start with then we must set the two counters to 1 (the default is 0) and create a loop to look at all lines stopping when wereach on starting Total in column 1

    Note any thing in Bold Italic is not code, but just a note to remind us that we have to write code in that place.

    Sub filefind()rin = 1rout = 1Do Until (row starts with Total )

    Do something rin = rin + 1

    LoopEnd Sub

    There are many functions in Visual Basic (See Appendix 1 for some of these

    The function Left(string,number) will give the first number of characters of a string

    So the Macro would now read

    Sub filefind()rin = 1rout = 1Do Until Left (Cells(rin,1).Value,5) = Total

    Do something rin = rin + 1

    LoopEnd Sub

    The single quote turns the rest of that line into a comment so the macro above could be entered into Excel and wouldwork, albeit apparently doing nothing. Just to prove to ourselves that it is working we will add another line to tell us the rownumber where the process stops.

  • 8/2/2019 Manipulating Data Files in Excel

    17/24

    Sub filefind()rin = 1rout = 1Do Until Left (Cells(rin,1).Value,5) = Total

    Do something rin = rin + 1

    Loop

    Msgbox rin End Sub

    The Function Msgbox prints out the message, in this case the value of rin and pauses until you click OK Copy the Macrobelow and run it from the doclist worksheet. Make a note of the value and check that that row is the one we were looking for.

    Now for the do something

    1. If the line begins Directory of we want to remember the value ie change the value of direct and the go on to nextline

    2. If column 1 contains the word , File, Volume or is blank we wish to ignore that line

    3. Other wise we wish to copy the relevant info into columns 4 and 5

    First stage is to pick up the lines with Directory of in them

    Sub filefind()rin = 1rout = 1Do Until Left (Cells(rin,1).Value,5) = Total

    If Left(Cells(rin,1).Value,9) = Directory Then direct = Mid(Cells(rin,1).Value,14)Msgbox direct

    Else Do something

    End ifrin = rin + 1

    LoopMsgbox rin

    End Sub

    This Version contains two new things.

    The Function Mid(string,value) which returns all the characters of a string starting and the position given by value, in ourcase 14. Ie it ignores the 13 characters Directory of (spaces count as characters) and returns every thing else.

    We have added and Else clause into the If statement. This is what happens if the condition is not True

    Run this Macro and study the output of the Msgboxes. You should notice that it seems to repeat the folder nameC:\docs\misc\chris\nextye several times. This must be an error as each Folder name should be unique. So weneed to examine the data to see what is happening. If you look at row 234 you will notice that the directory name is too longfor column 1 and has spilt over into column 2. This is a result of the way the data was read in. We need therefore to add on tothe value of direct anything that is in column 2 of that row. As Follows

    Note we use & to join together two string values

  • 8/2/2019 Manipulating Data Files in Excel

    18/24

    Sub filefind()rin = 1rout = 1Do Until Left (Cells(rin,1).Value,5) = Total

    If Left(Cells(rin,1).Value,9) = Directory Then direct = Mid(Cells(rin,1).Value,14) & Cells(rin,2).ValueMsgbox direct

    Else

    Do something End ifrin = rin + 1

    LoopMsgbox rin

    End Sub

    We now need to identify the rows that we wish to process. In fact it is easier in this case to identify the rows we wish toignore. These are Lines containing in column 1 the words Volume, DIR or File or lines which are blank. Blank lines can befound using the IsEmpty function we have used before

    To see if a string contains another string we can use the Instr function which returns a value of 0 if the second string does ntexist of the value of the starting point if it does See Appendix 1

    So we start with a variable called process which we set to True, then make the four tests, and set the value of process to falseif any of the tests fail as follows.

    Process = TrueIf IsEmpty(Cells(rin,1)) Then process = falseIf Instr(Cells(rin,1).Value, Vol) > 0 Then process = falseIf Instr(Cells(rin,1).Value, DIR) > 0 Then process = false If Instr(Cells(rin,1).Value, File) > 0 Then process = false

    If after those lines process is still True we can write out the values

    Sub filefind()rin = 1rout = 1Do Until Left (Cells(rin,1).Value,5) = Total

    If Left(Cells(rin,1).Value,9) = Directory Then direct = Mid(Cells(rin,1).Value,14) & Cells(rin,2).Value

    ElseProcess = TrueIf IsEmpty(Cells(rin,1)) Then process = falseIf Instr(Cells(rin,1).Value, Vol) > 0 Then process = falseIf Instr(Cells(rin,1).Value, DIR) > 0 Then process = false If Instr(Cells(rin,1).Value, File) > 0 Then process = false If process = True then

    Cells(rout,4).value = direct & \ & Cells( rin,2).ValueRout =rout + 1

    End ifEnd ifrin = rin + 1

    LoopEnd Sub

  • 8/2/2019 Manipulating Data Files in Excel

    19/24

    APPENDIX

    Code Examples

    ArithmeticAddition a + b

    Subtraction a bMultiplication a * bDivision a / bTo the power of a ^ b

    Some Arithmetic functions.

    Function Action ExampleINT(A) Returns nearest whole number less than A INT(5.34) = 5

    INT(-5.34) = -6 A MOD B Returns the integer remainder when A is divided by B 7 MOD 2 = 1

    50 MOD 5 = 0ABS(A) Returns the absolute value of A ABS(5) = 5

    ABS(-5) = 5

    RND() Returns a random number between 0 and 1.Combined with the INT function you can generate randomintegers between 1 and N

    INT(N * RND() + 1)

  • 8/2/2019 Manipulating Data Files in Excel

    20/24

    Actions based on conditions

    If...Then...ElseUsed when you want the program to do different things depending oncertain conditions.Example

    if a = 5 thenform.print a is 5

    elseform.print a is not 5

    end if

    The If Statement can have 3 basic forms1 Single Line

    If c < 0 Then c = 02 Actions only if condition is true

    If c > 0 thenc=c-1x = True

    End If

    3 Full version as example above

    Select CaseRuns one of several series of instructions according to the value of Expression. Expression is compared with eachCaseExpression in turn. When a match is found, the instructions following that Case CaseExpression are run, and thencontrol passes to the instruction following End Select. If there is no match, the instructions following Case Else are run.

    ExampleSelect Case left(text,1)

    Case 0 To 9 form.print Digit

    Case a,e,i,o,u form.print Vowel

    Case Elseform.print Consonant

    End Select

    SyntaxIf Condition1 Then

    Series of instructions [Else

    Series of instructions ]End If

    Select Case Expression Case CaseExpression

    Series of instructions [Case Else

    Series of instructions ]End Select

  • 8/2/2019 Manipulating Data Files in Excel

    21/24

    Repeated actionsFor...NextRepeats the series of instructions between For and Next while increasing CounterVariable by 1 (default) or the value of

    Increment until CounterVariable is greater than End . If Start is greater than End , then Increment must be a negative value inwhich case CounterVariable decreases by Increment until it is less than End .

    For i = 1 to 10 step 2form.print i

    Next

    Do Loop

    x = 1Do Until x = 9

    form.print xx = x + 2

    Loop

    The condition can be either with the Do command in which

    case it is checked before entering the loop or with the Loopcommand in which case the loop is executed at least oncebefore reaching the condition.

    The optional Exit Do statement enables you to break out of theloop even if the condition has not been reached.

    A similar older version is below

    While...WendRepeats a series of instructions between While and Wend while the specified condition is true.

    x = 1While x < 11

    form.print xx = x + 2

    Wend

    For CounterVariable = Start To End [Step Increment ]Series of instructions

    Next [CounterVariable ]

    Do {While | Until } condition statements [Exit Do ]statements

    Loop

    Or

    Do statements [Exit Do ]statements

    Loop {While | Until } condition

    While Condition Series of instructions

    Wend

  • 8/2/2019 Manipulating Data Files in Excel

    22/24

    Strings and NumbersAsc() Asc( string )

    Returns the character code of the first character in string .Str () Str ( n)

    Returns the string representation of the value n. If n is a positive number, Str(n) returns a string with a leading space.To remove the leading space, use LTrim().

    Chr () Chr ( CharCode ) Returns the character whose ANSI character code is CharCode. Character codes in the range 0 (zero) to 31, inclusive,match the nonprinting characters of the standard ASCII code. For example, Chr(13) is a carriage return character andChr(9) is a tab character.The following table lists a few of the special characters you can produce using Chr().Value Character returnedChr(9) Tab characterChr(11) Newline character (SHIFT+ENTER)Chr(13) Carriage returnChr(32) Space characterChr(34) Quotation mark

    Val() Val( string ) Returns the numeric value of string . A common use of Val() is to convert strings containing digit characters to

    numbers so they may be used in mathematical formulas. If string does not begin with a digit character, Val() returns 0(zero).Len() Len( string )

    Returns the number of characters in string .InStr() InStr( [ Index ,] Source, Search )

    Returns the character position in Source at which Search begins, where 1 corresponds to the first character, 2 to thesecond character, and so on. If Source does not contain Search, InStr() returns 0 (zero).Argument ExplanationIndex The character position in Source at which to begin the search. Optional the default is 1Source The text to be searched.Search The text to search for.

    Left() Left( Source, Count ) Returns the leftmost Count characters of Source.

    Mid() Mid( Source, Start [, Count] )

    Returns a portion of Source starting at a given character position.Argument ExplanationSource The original string.Start The character position in Source where the string you want to return begins.Count The number of characters in the string you want to return. If you do not specify Count, the number of

    characters to the end of the string is assumed.Right() Right( Source, Count )

    Returns the rightmost Count characters of Source.Lcase() Lcase( Source )

    Returns a string in which all letters of Source have been converted to lowercase.Ucase() ditto uppercase

  • 8/2/2019 Manipulating Data Files in Excel

    23/24

    CommunicationInputBox () InputBox( Prompt [, Title] [, Default] )

    Displays a dialog box requesting a single piece of information and returns the text entered in the dialog box whenthe user chooses the OK button. If the user chooses the Cancel button, an error occurs. You can use the On Errorstatement to trap the error.Argument Explanation

    Prompt Text displayed in the dialog box indicating the kind of information requested.Title Text displayed in the title bar of the dialog box (if omitted, Word uses the title "Microsoft

    Word").Default Text that initially appears in the text box of the dialog box. This value is returned if the user types

    nothing before choosing OK.

    MsgBox, MsgBox() MsgBox Message[, Type] [, Title]MsgBox( Message [, Type] [, Title] ) The MsgBox statement displays a message in a message box. You can also display a message with theMsgBox() function, which returns a value according to the command button the user chooses in the messagebox. Use MsgBox() if you need your application to take action based on the user's response.Argument ExplanationMessage The message to be displayed in the message box. If Message is longer than 255 characters,

    an error occurs.Type A value representing the symbol and buttons displayed in the boxTitle The title of the message box. If omitted, "Microsoft Word" is the default title..Type is the sum of three values, one from each of the following groups.Group Value MeaningButton 0 (zero) OK button (default)

    1 OK and Cancel buttons2 Abort, Retry, and Ignore buttons3 Yes, No, and Cancel buttons4 Yes and No buttons5 Retry and Cancel buttons

    Symbol 0 (zero) No symbol (default)16 Stop symbol32 Question symbol48 Attention symbol64 Information symbol

    Button action 0 (zero) First button is the default256 Second button is the default512 Third button is the default

    Because the MsgBox statement does not return a value, the use of button values other than 0 (zero) is notrecommended. To make use of buttons other than the OK button, use the MsgBox() function. MsgBox()returns the following values.Return value Button chosen Button text

    -1 First (leftmost) button OKYesAbort

    0 (zero) Second button CancelNoRetry

    1 Third button CancelIgnore

  • 8/2/2019 Manipulating Data Files in Excel

    24/24

    Appendix 2 Creating a list of files

    Click on Start Run

    Type in cmd and press Enter, A command windowwill appear.

    Depending on the set up of the machine this may be pointing to the M: drive, so select the drive you require by typing acommand such as C: and press enter.

    Change to required folder by typing a command such as cd docs

    The type the command dir /s/n > c:\temp\filelist.txt (This creates a list of files in the given folder and all sub folders andwrites the output to a file called filelist.txt in the temp folder. Obviously you could change the name and location to whereveryou like.)

    Start up Excel in the normal way and open this file.You will have to tell Excel that you are looking fora text file.

    A window like the one to the Right should appear

    After selecting Fixed Width press Next

    Double click on all break lines to remove them,then scroll down the list to find a file name

    Click just to the left of the file name to add acolumn break

    Click Finish

    When the data has loaded, select the first twocolumns and change the font to Courier new andwiden the columns to see the data

    Save as an excel spreadsheet.