Microsoft Excel VBA Programming for the Absolute Beginner Second Edition phần 7 pdf

50 543 0
Microsoft Excel VBA Programming for the Absolute Beginner Second Edition phần 7 pdf

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

The Open Statement The Open statement is used to read or write data to a file. Table 7.3 summarizes the type of access, and modes or functions available for reading and writing data to a file with VBA. There is also a Binary access type for reading and writing to any byte position in a file as might be done with an image; however, this technique is beyond the scope of this book. The Open statement requires several arguments, including a string that designates the path to a specified file. If the file does not exist, then one will be created. The Open statement also requires an access mode ( Append, Binary, Input, Output, or Random) and a file number. Optional parameters include an access parameter ( Read, Write, or Read Write), lock (used to restrict operations on the file from other programs), and record length (specifies the length of the buffer or record). Open “C:\Data\Test.txt” For Input As #1 The preceding line opens a file named Test.txt found at the designated path for input, and assigns the file to the file number 1. If the file is not found, then one will be created at the designated location with the name Test.txt. You can open multiple files in your VBA programs, but they must be assigned unique file numbers. HINT HINT 289 Chapter 7 • Error Handling, Debugging, and Basic File I/O Figure 7.14 The Open dialog box of the FileDialog object. Title property AllowMultiSelect property InitialFileName property FilterIndex property 290 Microsoft Excel VBA Programming for the Absolute Beginner, Second Edition Member Description ChDir Changes the current directory. ChDrive Changes the current drive. CurDir Returns the current directory path. Dir Returns the name of a file, directory, or folder that matches a pattern, file attribute, or the volume label of a drive. EOF End of file. FileAttr The mode used to open a file with the Open statement. FileCopy Copies a file from a source path to a destination path. FileDateTime Returns the date and time that a file was created or last modified. FileLen Returns the length of a file in bytes. FreeFile Returns an Integer representing the next file number available for use by the Open statement. GetAttr Returns an Integer representing the attributes of a file or directory. Kill Deletes a file or files. Loc Specifies the current read/write position within an open file. LOF Returns a Long integer specifying the length of an open file in bytes. MkDir Creates a new directory. Reset Closes all disk files opened using the Open statement. RmDir Deletes an empty directory. Seek Returns a Long integer specifying the current read/write position within an open file. SetAttr Sets attribute information for a file. TABLE 7. 2 M EMBERS OF THE F ILESYSTEM O BJECT These methods are primarily designed to be used with the Open statement, but you may also find them useful with the other objects and methods discussed in this chapter. 291 Chapter 7 • Error Handling, Debugging, and Basic File I/O Example Return Value ChDir “C:\Documents and Settings” or ChDir “ ” N/A ChDrive “D:” N/A MsgBox CurDir Outputs the current directory path in a message box. fileName = Dir(“C:\test.txt”, vbNormal) The file name if it exists. Otherwise an empty string. EOF(fileNum) A Boolean value indicating whether the end of an opened file (specified with a file number) has been reached. Mode = FileAttr(fileNum, 1) Returns a Long integer indicating the mode used to open a file (Input, Output, Random, and so on). FileCopy “C:\TestFile.txt”, “D:\TestFile.txt” N/A fileDate = FileDateTime(“C:\test.txt”) For example, 1/23/2004 10:25:14 AM fileSize = FileLen(“C:\test.txt”) For example, 4 FileNumber = FreeFile For example, 2 myAttr = GetAttr(CurDir) 0=Normal, 1=Read-Only, 2=Hidden, 4=System, 16=Directory, 32=Archive Kill “C:\test.txt” N/A MyLocation = Loc(1) A Long integer FileLength = LOF(1) For example, 4 MkDir “TestDir” N/A Reset N/A RmDir “TestDir” N/A Seek(1) If the file is opened in Random mode it returns the number of the next record, otherwise it returns the current byte position in the file. SetAttr “C:\test.txt”, vbReadOnly N/A 292 Sequential Access Files Writing information to a sequential access file is sort of like recording music to a cassette tape. The songs vary in length and are recorded one after the other. Because it is hard to know the location of each song on the tape, it is difficult to quickly access a particular song. When information is written to a sequential file, the individual pieces of data (usually stored in variables) vary in length and are written to the file one after the other. For example, a sequential file containing names and phone numbers may look something like what’s shown here: “John Smith”, “111-2222” “Joe James”, “123-4567” “Jane Johnson”, “456-7890” The names and phone numbers were all written to the file as strings so they are enclosed in quotes. Numerical values written to a sequential access file will not contain the quotes. The strings containing the names vary in length and will require different amounts of memory for storage. If access to a part of the sequential file is desired at a later time (say we want Jane Johnson’s phone number), the entire file must be read into memory because it is not possible to know the location of the desired component within the file. After loading the file, the content must be searched for the desired value. This makes sequential access inef- ficient with very large files, because it will take too long to access the desired information. With smaller files, however, that do not take long to read, sequential access will work well. The CreateSeqFile() sub procedure writes textual information from the first three rows in columns A and B of a worksheet to a sequential access file. Public Sub CreateSeqFile() Dim filePath As String Dim I As Integer filePath = ActiveWorkbook.Path & “\SeqPhone.txt” Open filePath For Output As #1 Microsoft Excel VBA Programming for the Absolute Beginner, Second Edition Access Type Writing Data Reading Data Sequential Print#, Write# Input#, Input Random Put Get TABLE 7.3 FILE A CCESS M ODES WITH VBA For I = 1 To 3 Write #1, Cells(I, “A”).Value, Cells(I, “B”).Value Next I Close #1 End Sub The procedure above uses a For/Next loop to write the contents of the first three cells of columns A and B to a file called SeqPhone.txt. The I/O operation is terminated with the Close statement. The resulting file as viewed from Notepad is shown in Figure 7.15. Using Write # places quotes around each value written to the file. The file contains three lines of data because Write # adds a new line character to the end of the last value written to the file; because the For/Next loop iterates three times, the Write # statement was executed three times, resulting in three lines of data. Because the structure of the file is known, it is a simple task to alter the CreateSeqFile() pro- cedure to create a new procedure that reads the data. Public Sub ReadSeqFile() Dim filePath As String Dim I As Integer Dim theName As String Dim theNumber As String I = 1 filePath = ActiveWorkbook.Path & “\SeqPhone.txt” Open filePath For Input As #1 Do While Not EOF(1) Input #1, theName, theNumber Cells(I, “A”).Value = theName Cells(I, “B”).Value = theNumber I = I + 1 293 Chapter 7 • Error Handling, Debugging, and Basic File I/O Figure 7.15 Using Notepad to view a sequential file created using VBA code. 294 Loop Close #1 End Sub I changed the Open statement in the ReadSeqFile() procedure to allow for data input, and I replaced Write # with Input #. I also replaced the For/Next loop with a Do-loop and used the EOF() function in the conditional to test for the end of the file. The EOF() function accepts the file number as an argument and returns true when the end of the file is reached. The loop, therefore, continues as long as the EOF() function returns false (Do While NOT False equates to Do While True). Variables must be used to hold the strings returned from the file. Two variables ( theName and theNumber) are used in order to match the structure of the pro- cedure that wrote the data to the file. Random Access Files Random access files allow the programmer to access specific values within the file without having to load the entire file into memory. This is accomplished by ensuring that the indi- vidual data elements are of the same length before writing to the file. Again, consider the example of a phone book. Instead of storing the information as variable-length strings, the name and phone number can be stored with fixed length strings. The combination of the two fixed length strings that follow require the same amount of memory for every line written to the file. This will make it easy to locate a particular line in the file when the data is input. Dim theName As String*20 Dim theNumber As String*8 If the name to be stored is less than 20 characters, then spaces are added to match the defined length. If the string exceeds 20 characters, only the first 20 characters of the string are stored; therefore, it is important to define the length of the string so that it will be long enough to contain any possible value, yet not so long that too much memory is wasted by saving lots of spaces. The resulting data file might then look something like this: “John Smith ”, “111-2222” “Joe James ”, “123-4567” “Jane Johnson ”, “456-7890” Each line in the file requires the same amount of memory to store and is referred to as a record. Records can be represented by one or more values of the same or different data type (string, integer, and so on). Because the length of each record is identical, finding a specific record in the file without loading the entire file into memory is relatively easy (as you will see shortly). Microsoft Excel VBA Programming for the Absolute Beginner, Second Edition Rather than declare the individual elements of a record as separate variables, it is useful to define a custom data type that can be used in a variable declaration. The variable of the newly defined type can include all the desired elements of the record. To define a phone record for the previous example, a custom data type that includes both string elements must be declared in the general declarations section of a module. With the new data type definition, any variable can now be declared in a procedure as type Phone as shown in the CreateRanAccessFile() sub procedure. Individual elements of the phoneRec variable are accessed using the dot operator. To take full advantage of the custom data type, I write the phoneRec variable to a file using random access. Private Type Phone theName As String*20 theNumber As String*8 End Type Public Sub CreateRanAccessFile() Dim phoneRec As Phone Dim filePath As String Dim I As Integer, recNum As Integer recNum = 1 filePath = ActiveWorkbook.Path & “\randomPhone.dat” Open filePath For Random As #1 Len = Len(phoneRec) For I = 1 To 3 phoneRec.theName = Cells(I, “A”).Value phoneRec.theNumber = Cells(I, “B”).Value Put #1, recNum, phoneRec recNum = recNum + 1 Next I Close #1 End Sub The length of the record is specified by passing the variable phoneRec to the Len() function. The data is written to the file using the Put statement. (You should read a random access file with the Get statement.) An integer variable indicating the record number (recNum) must also be included with the custom variable in the Put statement so VBA knows where to insert the value within the file. The record number (indicated by the variable recNum in the CreateRanAccessFile() procedure) must begin with the value 1. 295 Chapter 7 • Error Handling, Debugging, and Basic File I/O 296 Microsoft Excel VBA Programming for the Absolute Beginner, Second Edition Chapter Project: Word Find The Word Find project is an Excel VBA program that creates word search puzzles. Words for a puzzle are associated with a topic that the program uses to sort the data. The topics and words used in a puzzle are stored in a random access file. The file containing the words and topics is accessed and displayed by the program. New words and topics for puzzles can be added to the file by the user. A puzzle is created when the user selects individual words and places them within a fifteen by fifteen grid running in any direction. After placing the words, the empty spaces in the puzzle are randomly filled with letters before printing. The Word Find program is stored on the accompanying CD-ROM as Wordfind.xls. Requirements for Word Find The objectives for the Word Find project are to demonstrate some basic techniques for file I/O and error handling in a VBA program. To accomplish the task, I use an Excel worksheet as the grid for a word search puzzle and a VBA form for updating the data required by the pro- gram. The requirements for the program follow: 1. A VBA form ( UserForm object) shall be used as the interface for updating the program’s data (words and topics) stored in a random access file. 2. The form shall display all unique topics stored in the data file. 3. The form shall display all words stored in the data file that are associated with a user-selected topic. In the Real World Many applications save data to a type of random access file that is more commonly referred to as a database. Database files such as those created by MS Access (.mdb extension) offer a lot more power to the programmer relative to the random access file created by VBA’s Open state- ment. A single database file normally contains multiple tables of data that are linked together through related fields (columns). Furthermore, it is usually possible to use a database’s pro- gramming engine to link your VBA program to a database file such that you can quickly retrieve and update very specific data. With Excel, it is possible to link your VBA program to an Access database (and many others) even if the Access GUI has not been installed on your computer. Unfortunately, it would take at least an entire book to properly discuss database normalization, the Structured Query Language (SQL), and ActiveX Data Objects (ADO)—the topics required to understand and use database files in your VBA program. 4. The form shall allow the user to add new records to the data file. 5. The form shall allow the user to update (edit) previously stored words in the data file for an existing topic. Note that the program will not allow for existing topics to be updated. 6. The form shall display new and updated records as they are created. 7. An Excel worksheet shall be used to create the word search puzzle. 8. The puzzle worksheet shall isolate an area of cells (fifteen by fifteen cells in size) for displaying a puzzle. 9. The puzzle worksheet shall isolate an area of cells for displaying the list of words that have been added to a puzzle. 10. The puzzle worksheet shall isolate an area of cells for displaying help/error messages to the user when creating a puzzle. 11. The puzzle worksheet shall isolate an area of cells for displaying a puzzle’s title. 12. The puzzle worksheet shall display a list of unique topics from the data file for the user to choose from when creating a puzzle. 13. The puzzle worksheet shall display a list of words from the data file associated with the topic selected by the user. 14. The user shall be able to select a word from the displayed list of words on the puzzle worksheet and add it to the puzzle by indicating a starting position on the puzzle grid. 15. The user shall be able to select a direction for a word added to the puzzle from a series of buttons on the worksheet. 16. The program shall validate the user’s selection for the location of a word to ensure the entire word fits within the defined area of the puzzle grid. There will be no validation to prevent a word from overwriting another word(s). 17. The user shall be able to clear the contents of the puzzle, the list of words in the puzzle, the list of topics, and the list of words associated with the selected topic from a button on the worksheet. 18. The user shall be able to finish a puzzle by adding randomly selected uppercase letters to the empty cells in the puzzle grid from a button on the worksheet. 19. The user shall be able to print the puzzle and the list of words contained in the puzzle from a button on the worksheet. 20. The user shall be able to display the form used to update the data in the data file from a button on the worksheet. Note that the user will not be able to edit the data in the file directly from the worksheet. 297 Chapter 7 • Error Handling, Debugging, and Basic File I/O 298 21. The user shall be able to refresh the list of topics, and list of words associated with a topic from a button on the worksheet. 22. The data for the program shall be stored in a random access file containing three fields of data per record. The first field contains the numbers used to identify spe- cific records (rows) of data. The second field contains the topics, and the third field contains the words associated with the topics in the second field. 23. The data from the file shall be stored in a worksheet that is hidden from the user. The data from the file shall be written to the worksheet when the user elects to show the update form. 24. When the user chooses to edit an existing record or add a new record, the program shall write the new data to the text file and the hidden worksheet. As with every program you write, you will edit the requirement list after you have designed it; sometimes even after you started writing the program because it is nearly impossible to think of everything from the beginning. I added and removed requirements from the pre- vious list after careful consideration of the program’s design and objectives. Designing Word Find The Word Find program’s design features objects that are now familiar to you, including a VBA form and a worksheet. The form serves to allow the user to update the data file with new records as well as edit existing records. The worksheet serves as the interface for creating a word search puzzle. The program takes advantage of the grid-like nature of a worksheet to create the puzzle. Words are written to the puzzle using individual cells for holding the letters. After the puzzle is finished, it is a relatively simple task to print it for someone’s enjoyment. Designing the Form The program is divided into two parts: the first part contains the form used to update the data file and the second part contains the worksheet used to create a puzzle. Figure 7.16 shows the form’s design from the IDE. Whenever a program is required to display a list, you should immediately think List Box or Combo Box control. I have added a Combo Box control to the form for displaying the list of topics and a List Box control for displaying the list of words associated with a selected topic. That is, when the user changes the selection in the Combo Box control the list of words in the List Box control will also change to match the topic. Microsoft Excel VBA Programming for the Absolute Beginner, Second Edition [...]... the form I originally added the Label control to the form to test and help debug the program It serves no purpose to allow the user to see this value; however, the 308 Microsoft Excel VBA Programming for the Absolute Beginner, Second Edition Label control serves as a convenient location for storing the number of the record currently displayed on the form The record number is required for updating the. .. Sub The last procedure listed in the code module for the UserForm object is the QueryClose() event procedure that is simply used to hide the form Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) frmWordFind.Hide End Sub 314 Microsoft Excel VBA Programming for the Absolute Beginner, Second Edition Because the GetUniqueTopics() sub procedure is called from the code modules for the. .. in the ActiveX controls Furthermore, Microsoft Excel VBA Programming for the Absolute Beginner, Second Edition 306 when the topics are sorted alphabetically, it’s easier to pick out the unique values from the list Note that I passed the Sort() method of the Range object several arguments They are all optional, but at the very least, Key1 and Key2 must be included in order to specify the primary and secondary... displayed in the List Box 3 The user selects a word from the list in the List Box 4 The user selects a cell in the puzzle grid 5 The user clicks a button indicating the direction in which to write the word and the word is added to the puzzle 6 The user continues to add words to the puzzle until he or she is satisfied with the puzzle 7 The user clicks the Fill button and the program fills empty cells in the. .. worksheet that is hidden from the user works quite well, although further protections should probably be added (password protection of the worksheet) It is also important that the data in the hidden worksheet be updated as the file is updated by the user 300 Microsoft Excel VBA Programming for the Absolute Beginner, Second Edition I have left a couple of potential problems in the form’s design It does not... data file 302 Microsoft Excel VBA Programming for the Absolute Beginner, Second Edition Writing the Code for the Userform Module The program’s data is stored in a random access file The advantage to this type of file is that it can be quickly updated as long as your program correctly tracks the record number In order to do this, a custom data type is required to ensure each record uses the same amount... WordToList ‘Add word to the list below puzzle Range(“Output”).Value = “” Exit Sub ErrorHandler: Range(“Output”).Value = “Please select a word from the list!” End Sub 319 320 Microsoft Excel VBA Programming for the Absolute Beginner, Second Edition The SelectionValid() and CountCells() function procedures work together to validate the user’s selection on the puzzle grid for adding a word The selection is validated... possibilities for file I/O errors exist (for example, a corrupt file); therefore, the error handler is added to display a message box indicating the nature of the error and end the program The error handler is also called if the file path is invalid and the user chooses to cancel the file dialog used to find the file (this returns an empty string for the file path) I handle the error this way because of the. .. before the record is updated because the program requirements specified that no updates to the topics are allowed Private Sub cmdUpdate_Click() Dim I As Integer 312 Microsoft Excel VBA Programming for the Absolute Beginner, Second Edition Dim validTopic As Boolean For I = 0 To cmbTopics.ListCount - 1 If cmbTopics.List(I) = txtTopic.Text Then validTopic = True Exit For End If Next I If Not validTopic Then... grid 8 The user clicks the Print Puzzle button and the puzzle and word list is printed Writing the Code for Word Find As the interface for this program has two distinct parts, so will the code The form and worksheet components can be written and tested as independent programs, then added together to complete this project I will start with the code module for the UserForm object that updates the data . Refreshes the topic and word lists. This button is meant to be used after the user has updated the data using the form. Microsoft Excel VBA Programming for the Absolute Beginner, Second Edition Figure. I/O 296 Microsoft Excel VBA Programming for the Absolute Beginner, Second Edition Chapter Project: Word Find The Word Find project is an Excel VBA program that creates word search puzzles. Words for a. grid for a word search puzzle and a VBA form for updating the data required by the pro- gram. The requirements for the program follow: 1. A VBA form ( UserForm object) shall be used as the interface

Ngày đăng: 12/08/2014, 16:21

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan