- Tham gia
- 3/6/06
- Bài viết
- 1,611
- Được thích
- 14,001
- Nghề nghiệp
- ...thiết kế máy bay cho VOI tự lái...^.^
Phần I
HÃY CÙNG KHÁM PHÁ CÁC HÀM CỦA POP
HÃY CÙNG KHÁM PHÁ CÁC HÀM CỦA POP
Đa số các hàm này Tác giả đều có đề cập trong sách:Microsoft Excel 2000 Power Programming with VBA.
Đây là đọan mã của các hàm:
- Function CELLCOLORINDEX(Cell) As Long
' Returns a number that corresponds to a cell''s background color
' Trả về số đại diện cho màu nền của cell
CELLCOLORINDEX = Cell(1).Interior.ColorIndex
End Function - Function CELLFONTCOLOR(Cell) As Variant ' Tương tự trả về màu của font
CELLFONTCOLOR = Cell(1).Font.ColorIndex
End Function
[*]Function CELLHASFORMULA(Cell) As Boolean
' Returns TRUE if cell has a formula
' Trả về TRUE nếu cell có công thức
CELLHASFORMULA = Cell(1).HasFormula
End Function
[*] Function CELLTYPE(Cell)
' Returns the cell type of the upper left cell in a range
' Trả về chuổi mô tả kiểu dữ liệu của cell
Select Case True
Case Cell(1).NUMBERFORMAT = "@"
CELLTYPE = "Text"
Case IsEmpty(Cell(1))
CELLTYPE = "Blank"
Case WorksheetFunction.IsText(Cell(1))
CELLTYPE = "Text"
Case WorksheetFunction.IsLogical(Cell(1))
CELLTYPE = "Logical"
Case WorksheetFunction.IsErr(Cell(1))
CELLTYPE = "Error"
Case IsDate(Cell(1))
CELLTYPE = "Date"
Case InStr(1, Cell(1).text, ":") <> 0
CELLTYPE = "Time"
Case IsNumeric(Cell(1))
CELLTYPE = "Value"
End Select
End Function
[*] Function CONTAINS(text1 As String, text2 As String, Optional casesensitive) As Boolean
' Returns True if text2 is contained in text1
' Trả về True nếu text2 nằm trong text1
If text1 = "" Or text2 = "" Then
CONTAINS = Evaluate("NA()")
Exit Function
End If
If IsMissing(casesensitive) Then casesensitive = 0
Select Case casesensitive
Case 0 ''zero or missing
If text2 Like "*" & text1 & "*" Then CONTAINS = True Else CONTAINS = False
Case Else ''any non-zero 3rd argument
If UCase(text2) Like "*" & UCase(text1) & "*" Then CONTAINS = True Else CONTAINS = False
End Select
End Function
[*] Function COUNTAVISIBLE(ParamArray number())
' Returns the count of visible cells in a range
' Đếm số cell visible trong range
Dim Cell As Range, i As Long
Application.Volatile
COUNTAVISIBLE = 0
For i = 0 To UBound(number)
If Not IsError(number(i)) Then
If TypeName(number(i)) = "Range" Then
Set number(i) = Intersect(number(i).Parent.UsedRange, number(i))
For Each Cell In number(i)
If Not Cell.EntireRow.Hidden And Not Cell.EntireColumn.Hidden Then COUNTAVISIBLE = COUNTAVISIBLE + Evaluate(Application.CountA(Cell))
Next Cell
Else
COUNTAVISIBLE = COUNTAVISIBLE + Evaluate(Application.CountA(number(i)))
End If
End If
Next i
End Function
[*] Function COUNTBETWEEN(Rng, num1, num2) As Long
' Counts number of values between num1 and num2
' Đếm số giá trị giữa num1 và num2
Dim Cell As Range
Dim Low As Double, Hi As Double
Low = Application.Min(num1, num2)
Hi = Application.Max(num1, num2)
Set Rng = Intersect(Rng.Parent.UsedRange, Rng)
COUNTBETWEEN = 0
If Rng Is Nothing Then Exit Function
COUNTBETWEEN = Application.CountIf(Rng, ">=" & Low) - Application.CountIf(Rng, ">" & Hi)
End Function
[*] Function CREDITCARD(Num As String) As String
' Hàm này thường ít sử dụng
Dim i As Long, CheckSum As Long, Prod As Long
Dim CardType As String
Const MASK As String = "2121212121212121"
' Remove dashes and spaces
Num = Replace(Num, "-", "")
Num = Replace(Num, " ", "")
' Determine card type
CardType = "Unknown"
If Left(Num, 2) = "37" Then CardType = "AmEx"
If Left(Num, 1) = "4" Then CardType = "Visa"
If Left(Num, 1) = "5" Then CardType = "MasterCard"
If Left(Num, 1) = "6" Then CardType = "Discover"
' Make card length = 16
Select Case Len(Num)
Case 13: Num = "000" & Num
Case 14: Num = "00" & Num
Case 15: Num = "0" & Num
Case 16: Num = Num
Case Else
CREDITCARD = "Invalid"
Exit Function
End Select
' Calculate check sum
CheckSum = 0
For i = 1 To 16
Prod = Mid(Num, i, 1) * Mid(MASK, i, 1)
If Prod > 9 Then Prod = Prod - 9
CheckSum = CheckSum + Prod
Next i
CheckSum = CheckSum Mod 10
If CheckSum <> 0 Then CREDITCARD = "Invalid" Else CREDITCARD = CardType
End Function
[*] Function DAYSINMONTH(******_number As Date) As Long
' Returns the number of days in the month for a date
' Trả về số ngày trong một tháng
Dim m As Long, y As Long
m = Month(******_number)
y = Year(******_number)
If m = 12 Then
m = 1
y = y + 1
Else
m = m + 1
End If
DAYSINMONTH = Day(DateSerial(y, m, 1) - 1)
End Function
[*] Function DOLLARTEXT(number, Optional units) As String
'Chuyển số thành chữ
'Used in the Power Utility Pak with the kind permission of Steven Mark
'Copyright
'Author: Steven Mark, On-The-Mark Systems,
'Email: steve@otms.com
'Web: http://www.otms.com
Dim strEntireNumber As String
Dim cbEntireNumber As Long
Dim strOut As String
Dim iPos As Long
Dim nHundreds As Long
Dim nTens As Long
Dim nOnes As Long
Dim nTensOnes As Long
Dim astrUnits(17) As String
Dim strLast As String
''Ensure this is a number
If Not IsNumeric(number) Then
Exit Function
End If
''Format the number
strEntireNumber = Format(number, "###0.00")
cbEntireNumber = Len(strEntireNumber)
If cbEntireNumber > 18 Then
DOLLARTEXT = CVErr(xlErrNum)
Exit Function
End If
''Do it
strOut = ""
strLast = ""
astrUnits(17) = "Trillion"
astrUnits(14) = "Billion"
astrUnits(11) = "Million"
astrUnits(8) = "Thousand"
For iPos = 17 To 5 Step -3 ''0 is at the right, not the left
If cbEntireNumber > iPos - 2 Then
''Convert hundreds
If cbEntireNumber > iPos Then
nHundreds = Val(Mid$(strEntireNumber, cbEntireNumber - iPos, 1))
If nHundreds > 0 Then
strOut = strOut & " " & Application.Choose(nHundreds, "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine") & " Hundred"
End If
End If
''Convert tens and ones
If cbEntireNumber > iPos - 1 Then
nTens = Val(Mid$(strEntireNumber, cbEntireNumber - iPos + 1, 1))
Else
nTens = 0
End If
If cbEntireNumber > iPos - 2 Then
nOnes = Val(Mid$(strEntireNumber, cbEntireNumber - iPos + 2, 1))
Else
nOnes = 0
End If
nTensOnes = nTens * 10 + nOnes
If nTensOnes > 9 And nTensOnes < 20 Then
strOut = strOut & " " & Application.Choose(nTensOnes, , , , , , , , , , "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen")
Else
If nTens >= 2 Then
strOut = strOut & " " & Application.Choose(nTens, , "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety")
End If
If nOnes > 0 Then
If nTens >= 2 Then
strOut = strOut & "-"
Else
strOut = strOut & " "
End If
strOut = strOut & Application.Choose(nOnes, "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine")
End If
End If
''Add units if anything was converted for this block
If strOut <> strLast Then
strOut = strOut & " " & astrUnits(iPos)
strLast = strOut
End If
If strOut = "" Then strOut = "Zero"
End If
Next iPos
strOut = Trim(strOut) & " and " & Mid(strEntireNumber, cbEntireNumber - 1, 2) & "/100"
''Add units?
If IsMissing(units) Then strOut = strOut & " Dollars" Else strOut = strOut & " " & units
DOLLARTEXT = Trim(strOut)
End Function
(Trích bài của anh Lê Văn Duyệt)