Casting, Associating Buttons with Macro

advertisement
計算機語言 (大一, 第二学期)
福島康裕 助理教授, 環境系統工程研究室
email: fuku@mail.ncku.edu.tw, ext. 65838
Requirements

Use buttons to start macros

“Read data” button : readAllData 巨集
 Ask
user how many data files there are
 Import the required number of data file

“Clear data” button : clearData 巨集
 Clear
all the data that are imported
Starting point:
Reading 10 data at once!
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)
.TextFileConsecutiveDelimiter = True
.Refresh BackgroundQuery:=False
End With
ActiveCell.Offset(5, 0).Range("A1").Select
Next i
End Sub
Type of parameters

Type of Parameters in VBA

Text


Number


Integer, Long Currency, Single, Double, Date
Logical type


String
Boolean
Casting (changing type of a parameter)

You need to have same type when you operate
some calculation

Ex: a = 1, b = 2.345, c = a + b

In this case, a will be Integer, b will be Double. VBA
will automatically change type of a into Double, and
add them together.
Type of parameters

Casting in our exercise

We can combine two texts with “+”


name = “Fuku”
greeting = “Hello, ” + name + “!!”
i = 1, 2, 3, 4, … so i is Integer type

we cannot add Integer with String directly
We need to combine …
“climate” and 1,2,3,…,9 and “.dat”

We cast the value in i into String

Dim stringnumber As String
stringnumber = i ’ casting happens in this line!!
Reading n data at once!
Sub readAllData()
Dim stringnumber As String
num = Application.InputBox("How many data files?")
Range("F2") = "number of data:“
Range("G3") = num
user
Range("A5").Select
‘ ask number to user
‘ write
‘ number of data input by
‘ always import to the same place
For i = 1 To num
stringnumber = i
With ActiveSheet.QueryTables.Add(Connection:="TEXT;C:\Documents and
Settings\福島康裕\Desktop\climate\climate" + stringnumber + ".dat",
Destination:=ActiveCell)
.TextFileSpaceDelimiter = True
.Refresh BackgroundQuery:=False
End With
ActiveCell.Offset(5, 0).Range("A1").Select
Next i
End Sub
Starting point:
Reading 10 data at once!
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)
.TextFileConsecutiveDelimiter = True
.Refresh BackgroundQuery:=False
End With
ActiveCell.Offset(5, 0).Range("A1").Select
Next i
End Sub
Using button control

[View][Toolbars][Forms]
Click button, and put
it on the sheet.
A dialog will pop up,
and let you relate the
button with a macro!
Clearing the data

First, just figure out how to clear a
range…
Sub clearData()
Range(A5:M8).Select
Selection.ClearContents
End Sub
Question:
This range A5:M8 is for only 1 data set
What is the range for n data sets?
Clearing the data

Now let’s clear the exact range…
Sub clearData()
' Macro recorded 2005/4/14 by Fukushima Yasuhiro
'
Dim num As String
Dim erasing_range As String
num = 5 * Range("g3").Value + 4 ‘ change type to String
erasing_range = "A5:M" + num
‘ connect Strings
Range(erasing_range).Select
Selection.ClearContents
Range("f2").ClearContents
Range("g3").ClearContents
End Sub
‘ erase number of data
‘ written in the sheet
Download