How to write an excellent report

advertisement
Computer Language Class Exercise
Exercise 5: Defining own functions in MS-Excel
Write the title of the document, your
Name: 福島 康裕
ID: xxxxxxxx
Submitted on 2005/04/11
name and student ID for any kind of
homework or report in university!!
In this exercise, we learned how to use Visual Basic Editor for 1) defining our own functions,
and to 2) modify Macro to automate iterative procedures as we wish. I found the following
from this exercise:
- definition of our own function is convenient
- the functions can be a part of other functions… functions are building materials
for other functions
- Macro is also a program called subroutine, it is just like functions. Therefore, we
can write and modify them using Visual Basic Editor
- Everything we do with Mouse and Keyboard (click, drag, copy, paste, type …)
can be written as a statement in the Macro
- Flowchart can help us think how to make programs
Show what you learned from the exercise. If there are questions or suggestions, write them in your report.
5-1) I think this exercise tries to let us get used to writing programs in Excel, and tries to help
us realize that function can be reused in other functions. I could use factorial and permutation
in the combination function. It helps us save lot of efforts!
Function factorial(n)
If n = 0 Then
n = 1
Else
n = n
End If
k = 1
Function permutation(n,r)
permutation = factorial(n) / factorial (n-r)
End Function
Function combinations(n,r)
combinations = permutation(n,r) / factorial(n)
End Function
For i = 1 to n
k = k * i
Next i
factorial = k
End Function
I found out that we cannot use some words for the name of
the functions. For example, “combination” cannot be used,
so I had to change the name of the function.
Excel already has “permut”, and “combin” functions !!
Describe all the troubles, how you tried to solve them, and
other things you found out on the way!!
5-2) The function will be as follows:
=Popgrowthrate (year_begin, population_begin, year_end, population_end)
and it will return the result calculated with the following equation:
Popgrowthrate = (population_end / population_begin)1/(year_end - year_begin) - 1
…(eq.1)
And the flowchart for this function will be as follows:
This function is easy and we don’t need any diamond
shapes for conditions, so this exercise is not interesting.
start
read
year_begin, population_begin,
year_end, and population_end
Maybe we can check if the user’s input is correct. For
example:
-
calculate population growthg
rate using (eq. 1)
return the result
population_begin should not be 0
year_end should be greater then year_begin
In that case, we will need diamond shapes for conditions.
end
5-3) Flowchart is as follows:
start
read n
the function will take an
argument n, and calculate
1 + 1/2 + … + 1/n
temp = 0, i = 1
i<n?
No
Yes
temp = temp + 1/i
return temp
end
The code is as follows:
Function invsum(n)
temp = 0
For i = 1 To n
temp = temp + 1 / i
Next i
invsum = temp
End Function
6
It is known that summation of “harmonic
series” would be infinite. The graph
might show as if it has an asymptotic
5
value, but actually, the answer of nÆ ∞
is ∞
4
3
2
1
0
0
20
40
60
80
100
Explanation:
n
1
1
<
∫1 x ∑
k =1 k
n 1
∫1 x = log n
lim log n = ∞
n
n→ ∞
n
∴ lim ∑
n→∞
k =1
1
=∞
k
5-4) I had struggled with this exercise for such a long time… because I did not know what
was wrong with my code until I found out that the error dialog gives me some clues.
First, I thought it should be easy. I thought it should work if I just apply For … Next loop as
we have learned in the previous exercise… I thought it should look as follows:
Sub readAllData()
For i = 1 To 10
With ActiveSheet.QueryTables.Add(Connection:="TEXT;C:¥Documents and Settings¥福
島康裕¥Desktop¥climate¥climate" + i + ".dat", Destination:=ActiveCell)
.Name = "climate2"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 932
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = True
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
ActiveCell.Offset(5, 0).Range("A1").Select
Next i
End Sub
However, if I try to run the macro, it will give me this error, telling me there is “type
mismatch” error.
By clicking “Help” button, I found out that there are different “types” in Excel.
The types are:
Next, I pressed “Debug” button in the dialog. Then, it looked as follows, indicating there is
an error in the yellow line.
From this, I guessed that this error happens because I am trying to add String and Variant,
which are different types. Apparently, we can use + ONLY for same types.
Now I need to change type of i from Variant to String. This could be done by adding the blue
lines in the code as shown in the next page. By substituting values in some type into
parameter with a different type, we can convert the type of parameter.
(i.e. stringnumber = i)
String
Å Variant
Sub readAllData()
Dim stringnumber As String
For i = 1 To 10
stringnumber = i
With ActiveSheet.QueryTables.Add(Connection:="TEXT;C:¥Documents and Settings¥福
島康裕¥Desktop¥climate¥climate" + stringnumber + ".dat", Destination:=ActiveCell)
.Name = "climate2"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 932
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = True
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
ActiveCell.Offset(5, 0).Range("A1").Select
Next i
End Sub
After this I could read all 10 files, but still have some problem. It does not show the correct
text. I could not solve this trouble…
--PS: The problem with corrupt character is due to bug of Excel… I am sorry if this caused any
trouble to you. Some students reported this problem in their report…
Download