Professional Documents
Culture Documents
VBA
VBA
Sub test()
Range("a1") = 100
End Sub
Function shcount()
shcount = Sheets.Count
End Function
Sub test2()
Call test
End Sub
Sub test3()
End Sub
VBA
VBA excel
Excel
Workbooks ,Workbooks(N) N
Workbooks ("")----- Excel Excel
ActiveWorkbook
ThisWorkBook
Sub Excel ()
MsgBox " " & Workbooks.Count & " Excel "
End Sub
2
Sheets("")
Sheet1 ,Sheet2 sheet
Sheets(n) n sheet
ActiveSheet
worksheet sheet
3
cells
Range ("")
Range ("A1") Range ("B1D10") Range ("A1B1D10E2F5")
Sub ()
Range("A1B1D10E2F5").Select
A = ActiveWorkbook.Colors(1)
Application.Dialogs(xlDialogEditColor).Show (1)
b = ActiveWorkbook.Colors(1)
ActiveWorkbook.Colors(1) = A
Selection.Interior.Color = b
End Sub
VBA
VBA VBA
.=
Selection.Interior.Color = b Selection
Interior.Color b
Sub ttt()
Range("a1").Value = 100
End Sub
Sub ttt1()
Sheets(1).Name = ""
End Sub
Sub ttt2()
Sheets("Sheet2").Range("a1").Value = "abcd"
End Sub
Sub ttt3()
Range("A2").Interior.ColorIndex = 3
End Sub
VBA
VBA VBA
VBA
. :=
Sub ttt4()
. :=
Range("A1").Copy Destination=Range("A2")
Range("A1").Copy Range("A2")Destination=
End Sub
Sub ttt5()
Sheet1.Move before:=Sheets("Sheet3")
End Sub
------
--------
VBA80 03
If
Sub 1() '
If Range("a1").Value > 0 Then
Range("b1") = ""
Else
Range("b1") = " 0"
End If
End Sub
Sub 2()
If Range("a1") <> "" And Range("a2") <> "" Then
Range("a3") = Range("a1") * Range("a2")
End If
End Sub
Select
Sub 1() '
Select Case Range("a1").Value
Case Is > 0
Range("b1") = ""
Case Else
Range("b1") = " 0"
End Select
End Sub
IFF
Sub 4()
Range("a3") = IIf(Range("a1") <= 0, "", "")
End Sub
Sub if ()
If Range("a2") <= 1000 Then
Range("b2") = 0.01
ElseIf Range("a2") <= 3000 Then
Range("b2") = 0.03
ElseIf Range("a2") > 3000 Then
Range("b2") = 0.05
End If
End Sub
Sub select ()
Select Case Range("a2").Value
Case 0 To 1000
Range("b2") = 0.01
Case 1001 To 3000
Range("b2") = 0.03
Case Is > 3000
Range("b2") = 0.05
End Select
End Sub
VBA80 04
Sub t1()
Range("d2") = Range("b2") * Range("c2")
Range("d3") = Range("b3") * Range("c3")
Range("d4") = Range("b4") * Range("c4")
Range("d5") = Range("b5") * Range("c5")
Range("d6") = Range("b6") * Range("c6")
End Sub
Sub t2()
Dim x As Integer
For x = 2 To 6 For x = 10000 To 2 Step -3
Range("d" & x) = Range("b" & x) * Range("c" & x)
Next x
End Sub
Sub t3()
Dim rg As Range
--- rg
For Each rg In Range("d2:d18")
----rg Range("d2:d18") Range("d2:d18") rg
Sub t4()
Dim x As Integer
x = 1
Do
x = x + 1
Cells(x, 4) = Cells(x, 2) * Cells(x, 3)
Loop Until x = 18 x=18
End Sub
Sub t5()
x = 1
Do While x < 18
x = x + 1
Cells(x, 4) = Cells(x, 2) * Cells(x, 3)
Loop
End Sub
Sub s1() 0
Dim rg As Range
For Each rg In Range("a1:b7,d5:e9")
If rg = "" Then
rg = 0
End If
Next rg
End Sub
Sub s2()
Dim x As Integer
Do
x = x + 1
If Cells(x + 1, 1) <> Cells(x, 1) + 1 Then
Cells(x, 2) = ""
Exit Do---- Do
End If
Loop Until x = 14
End Sub
Sub s2222()
Dim x As Integer
Do
x = x + 1
If Cells(x + 1, 1) <> Cells(x, 1) + 1 Then
Cells(x, 2) = ""
' Exit Do '---- Do
End If
Loop Until x = 129
End Sub
VBA80 05 VBA
Dim m As Integer
'
'
'
Sub t1()
Dim X As Integer 'x
For X = 1 To 10
Cells(X, 1) = X
Next X
End Sub
'
'1
' t1
'2
Sub t2()
Dim st As String
Dim X As Integer
For X = 1 To 10
st = st & "Excel "
Next X
MsgBox st
End Sub
'3
Sub t3()
Dim rg As Range
Set rg = Range("a1") set
rg = 100---- rg.Value =100
End Sub
'4
Sub t4()
End Sub
'
'1
'
'2
'3
'dim public
'
'1 :
' t1
'2 :
' 5
Sub t6()
m = 1
End Sub
Sub t5()
MsgBox m
m = 7
End Sub
'3 : EXCEL
' public
Sub t7()
MsgBox qq
End Sub
'
'
set =nothing
VBA80 06
Option Explicit
'
'1 VBA
Sub t1()
Range("d2") = "=b2*c2" ------
End Sub
Sub t2()
Dim x As Integer
For x = 2 To 6
Cells(x, 4) = "=b" & x & "*c" & x --------------Cells(x,y) x y
&
Next x
End Sub
Sub t22()
Dim x As Integer
For x = 2 To 6
Range("d" & x) = "=b" & x & "*c" & x ---
Next x
End Sub
'2 VBA
Sub t3()
End Sub
'3 VBA
Sub t4()
Range("c9").FormulaArray = "=SUM(B2:B6*C2:C6)"
End Sub
'------ Evaluate
Sub t5()
Range("d16") = Evaluate("=SUMIF(A2:A6,""b"",B2:B6)")
Range("d9") = Evaluate("=SUM(B2:B6*C2:C6)")
End Sub
' Application.WorksheeFunction excel
Sub t6()
Range("d8") = Application.WorksheeFunction.Sumf(Range("d2:d6"))
-----WorksheeFunction.
----- VBA
End Sub
Sub VBA ()
Dim X As Integer
X = Application.WorksheeFunction.CountA(Range("A:A"))=== WorksheeFunction
MsgBox X
End Sub
Sub t66()
End Sub
' VBA
Sub t7()
End Sub
'
Function wn()
wn = Application.Caller.Parent.Name
End Function
VBA80 07 VBE
VBE
'1
'A
'B
'C
'D
'range("a1")=10
'
'3
'A -----
'B
'C ------
'D
'
'---
'E
'4
'
Sub d()
Dim x As Integer, st As String
For x = 1 To 10
st = st & Cells(x, 1)
Debug.Print "" & x & ":" & st
Next x
End Sub
'5
'
Sub d1()
Dim x As Integer, k As Integer
For x = 1 To 10
k = k + Cells(x, 1)
Next x
End Sub
Eurotool.xla
Funcres.xlaEXCEL
VBA80 08 VBA End
'END
'
Private Sub CommandButton1_Click()
End
End Sub
'Exit
'
'1Exit Sub
Sub e1()
Dim x As Integer
For x = 1 To 100
Cells(1, 1) = x
If x = 5 Then
Exit Sub
End If
Next x
Range("b1") = 100
End Sub
'2Exit function
Function ff()
Dim x As Integer
For x = 1 To 100
If x = 5 Then
Exit Function
End If
Next x
ff = 100
End Function
'3Exit for
Sub e2()
Dim x As Integer
For x = 1 To 100
Cells(1, 1) = x
If x = 5 Then
Exit For
End If
Next x
Range("b1") = 100
End Sub
'4Exit do
Sub e3()
Dim x As Integer
Do
x = x + 1
Cells(1, 1) = x
If x = 5 Then
Exit Do
End If
Loop Until x = 100
Range("b1") = 100
End Sub
'Goto ,
Sub t1()
Dim x As Integer
Dim sr
100:
sr = Application.InputBox("", "")
If Len(sr) = 0 Or Len(sr) = 5 Then GoTo 100
End Sub
'gosub..return ,,
Sub t2()
Dim x As Integer
For x = 1 To 10
If Cells(x, 1) Mod 2 = 0 Then GoSub 100
Next x
Exit Sub
100:
Cells(x, 1) = ""
Return ' gosub 100
End Sub
Sub t3()
On Error Resume Next
Dim x As Integer
For x = 1 To 10
Cells(x, 3) = Cells(x, 2) * Cells(x, 1)
Next x
End Sub
Sub t4()
On Error GoTo 100
Dim x As Integer
For x = 1 To 10
Cells(x, 3) = Cells(x, 2) * Cells(x, 1)
Next x
Exit Sub
100:
MsgBox "" & x & ""
End Sub
Sub t5()
On Error Resume Next
Dim x As Integer
For x = 1 To 10
If x > 5 Then On Error GoTo 0
Cells(x, 3) = Cells(x, 2) * Cells(x, 1)
Next x
Exit Sub
End Sub
VBA80 09 excel
excel
'excel
'excel excel excel excel
'Workbooks excel
'Workbooks("A.xls") A excel
Sub t1()
Workbooks("A.xls").Sheets(1).Range("a1") = 100
End Sub
'workbooks(2)
Sub t2()
Workbooks(2).Sheets(2).Range("a1") = 200
End Sub
'ActiveWorkbook excel ActiveWorkbook
'ThisworkbookVBA ()
,thisworkbook
'
'Windows("A.xls"),A windows
Sub t3()
Windows("A.xls").Visible = False
End Sub
Sub t4()
Windows(2).Visible = True
End Sub
Excel ()
Option Explicit
Public a As String
Public b As String
Public x As Integer
Public wb As Workbook
Sub Excel ()
'Dim x As Integer
x = 1
'Dim wb As Workbook
For Each wb In Workbooks
a = Workbooks(x).Name
b = ActiveWorkbook.Name
'Debug.Print a
'Debug.Print b
If b = Workbooks(x).Name Then
MsgBox "" & x & " Excel " & Workbooks.Count & " Excel
" & " Excel " & b
End If
x = x + 1
Next wb
End Sub
excel
'1 A.Xls
Sub W1()
If Len(Dir("d:/A.xls")) = 0 Then
MsgBox "A "
Else
MsgBox "A "
End If
End Sub
'2 A.Xls
Sub W2()
Dim x As Integer
For x = 1 To Windows.Count
If Windows(x).Caption = "A.XLS" Then
MsgBox "A "
Exit Sub
End If
Next
End Sub
'3 excel
Sub W3()
Dim wb As Workbook
Set wb = Workbooks.Add
set ,,,,,workbooks add workbook
wb.Sheets("sheet1").Range("a1") = "abcd"
wb.SaveAs "D:/B.xls"
End Sub
'4 excel
Sub w4()
Dim wb As Workbook
Set wb = Workbooks.Open("D:/B.xls")
-----
-----
------ Workbooks.Open "D:/B.xls"
MsgBox wb.Sheets("sheet1").Range("a1").Value
wb.Close False---true
End Sub
'5 excel
Sub w5()
Dim wb As Workbook
Set wb = ThisWorkbook
wb.Save
wb.SaveCopyAs "D:/ABC.xls"
End Sub
'6 excel
Sub W6()
FileCopy "D:/ABC.XLS", "E:/ABCd.XLS"
Kill "D:/ABC.XLS"
End Sub
VBA80 10 excel
excel
Option Explicit
'excel
'excel (worksheet)
sheets
Workbook
'sheets excel
'Sheets("A") A excel
Sub t1()
Sheets("A").Range("a1") = 100
End Sub
excel
'1 A
Sub s1()
Dim X As Integer
For X = 1 To Sheets.Count---- Sheets.Count
If Sheets(X).Name = "A" Then
MsgBox "A "
Exit Sub
End If
Next
MsgBox "A "
End Sub
'2 excel
Sub s2()
Dim sh As Worksheet
Set sh = Sheets.Add--- sh set
sh.Name = ""
sh.Range("a1") = 100
End Sub
Sub s5552()
Dim sh As Worksheet
Dim x As Integer
Dim y As Integer
For x = 1 To 5
Set sh = Sheets.Add
sh.Name = x * 100
sh.Range("a2") = x * 1000
sh.Move after:=Sheets(Sheets.Count)---
Next x
End Sub
'3 excel
Sub s3()
Sheets(2).Visible = True------Visible
End Sub
'4 excel
Sub s4()
Sheets("Sheet2").Move before:=Sheets("sheet1") 'sheet2 sheet1
Sheets("Sheet1").Move after:=Sheets(Sheets.Count) 'sheet1
End Sub
'6 excel
Sub s5() '
Dim sh As Worksheet
Sheets("").Copy before:=Sheets(1) Sheets("") Copy before:=Sheets(1)
before copy true FALSE =====
Set sh = ActiveSheet
sh.Name = "1 "
sh.Range("a1") = ""
End Sub
Sub s5rtyr552()
Sheets(1).Copy=========== excel sheet Excel
End Sub
Sub s6() '
Dim wb As Workbook
Sheets("").Copy=========== excel sheet Excel
Set wb = ActiveWorkbook
wb.SaveAs ThisWorkbook.Path & "/1 .xls"=======
wb.Sheets(1).Range("b1") = ""
wb.Close True
End Sub
'7
Sub s7()
Sheets("sheet2").Protect "123"
End Sub
Sub s8() '
If Sheets("sheet2").ProtectContents = True Then
MsgBox ""
Else
MsgBox ""
End If
End Sub
'8
Sub s9()
Application.DisplayAlerts = False excel
Sheets("").Delete
Application.DisplayAlerts = True excel
End Sub
'9
Sub s10()
Sheets("sheet2").Select
End Sub
VBA80 11
'1 (a1)
Sub s()
Range("a1").Select
Cells(1, 1).Select
Range("A" & 1).Select
Cells(1, "A").Select
Cells(1).Select
[a1].Select
End Sub
'2
Sub d() ' a1:c5
' Range("a1:c5").Select
' Range("A1", "C5").Select
' Range(Cells(1, 1), Cells(5, 3)).Select
'Range("a1:a10").Offset(0, 1).Select
Range("a1").Resize(5, 3).Select
End Sub
'3
Sub d1()
Range("a1,c1:f4,a7").Select
'Union(Range("a1"), Range("c1:f4"), Range("a7")).Select
End Sub
Sub dd() 'union
Dim rg As Range, x As Integer
For x = 2 To 10 Step 2
If x = 2 Then Set rg = Cells(x, 1)
Set rg = Union(rg, Cells(x, 1))
Next x
rg.Select
End Sub
'4
Sub h()
'Rows(1).Select
'Rows("3:7").Select
'Range("1:2,4:5").Select
Range("c4:f5").EntireRow.Select
End Sub
'5
Sub L()
' Columns(1).Select
' Columns("A:B").Select
' Range("A:B,D:E").Select
Range("c4:f5").EntireColumn.Select ' c4:f5
End Sub
'6
Sub cc()
Range("b2").Range("a1") = 100
End Sub
'7
Sub d2()
Selection.Value = 100
End Sub
VBA80 12
'1
Sub d1()
Sheets("sheet2").UsedRange.Select
'wb.Sheets(1).Range("a1:a10").Copy Range("i1")
End Sub
'2 ()
Sub d2()
Range("b8").CurrentRegion.Select
End Sub
()
'3
Sub d3()
Application.Intersect(Columns("b:c"), Rows("3:5")).Select----- Application
End Sub
'4
Sub d4()
Range("A1:A6").SpecialCells(xlCellTypeBlanks).Select-----
End Sub
'5
Sub d5()
Range("a65536").End(xlUp).Offset(1, 0) = 1000----- Excel
End Sub
Sub d6()
Range(Range("b6"), Range("b6").End(xlToRight)).Select
End Sub
Sub t()
Dim x As Integer
For x = 2 To 6
If Cells(x, 2) > 0 Then
Cells(x, "N") = "1 "
Else
Cells(x, "N") = Range("b" & x).End(xlToRight).Column - 1 & ""
End If
Next x
End Sub
VBA80 13
'1
Sub x1()
Range("b10") = Range("c2").Value===
Range("b11") = Range("c2").Text===
Range("c10") = "'" & Range("I3").Formula===
End Sub
'2
Sub x2()
With Range("b2").CurrentRegion b2 B2I6
[b12] = .Address=======$B$2$I$6
[c12] = .Address(0, 0) === B2I6
[d12] = .Address(1, 0) === B$2I$6
[e12] = .Address(0, 1) ===$B2$I6
[f12] = .Address(1, 1) ===$B$2$I$6 Address
End With=== with
End Sub
'3
Sub x3()
With Range("b2").CurrentRegion
[b13] = .Row
[b14] = .Rows.Count
[b15] = .Column
[b16] = .Columns.Count
[b17] = .Range("a1").Address
End With
End Sub
'4
Sub x4()
With Range("b2")
[b19] = .Font.Size
[b20] = .Font.ColorIndex
[b21] = .Interior.ColorIndex
[b22] = .Borders.LineStyle
End With
End Sub
'5
Sub x5()
[B24] = Range("I2").Comment.Text
End Sub
'6
Sub x6()
With Range("b3")
[b26] = .Top
[b27] = .Left
[b28] = .Height
[b29] = .Width==== Top Left Height Width
End With
End Sub
'7
Sub x7()
With Range("b3")
[b31] = .Parent.Name====
[b32] = .Parent.Parent.Name====
End With
End Sub
'8
Sub x8()
With Range("i3")
[b34] = .HasFormula====
[b35] = .Hyperlinks.Count====
End With
End Sub
'9
VBA80 14
vba --- 3
'Excel
'
'1
Sub d1()
[b1] = ""===
'If Range("a1") = "" Then===
'If Len([a1]) = 0 Then===
If VBA.IsEmpty([a1]) Then===
[b1] = ""
End If
End Sub
'2
Sub d2()
[b2] = ""
'If VBA.IsNumeric([a2]) And [a2] <> "" Then=== IsNumeric
'1
Sub t1()
Range("a1") = "a" & "b"
Range("b1") = "a" & Chr(10) & "b" '===, Chr(10)
End Sub
'2
Sub t2()
Range("a1:a10").Copy Range("c1") '====A1A10 C1
End Sub
Sub t3()
Range("a1:a10").Copy
ActiveSheet.Paste Range("d1") ===' D1
End Sub
Sub t4()
Range("a1:a10").Copy
Range("e1").PasteSpecial (xlPasteValues) '
End Sub
Sub t5()
Range("a1:a10").Cut
ActiveSheet.Paste Range("f1") '====A1A10 f1
End Sub
Sub t6()
Range("c1:c10").Copy
Range("a1:a10").PasteSpecial Operation:=xlAdd '-
End Sub
Sub T7()
Range("G1:G10") = Range("A1:A10").Value
End Sub
'3
Sub T8()
Range("b1") = "=a1*10"
Range("b1:b10").FillDown '===
End Sub
Sub c1()
Rows(4).Insert
End Sub
Sub c2() '
Rows(4).Insert
Range("3:4").FillDown
Range("4:4").SpecialCells(xlCellTypeConstants) = ""=====
End Sub
Sub c3()
Dim x As Integer
For x = 2 To 20
If Cells(x, 3) <> Cells(x + 1, 3) Then
Rows(x + 1).Insert
x = x + 1
End If
Next x
End Sub
Sub c4()
Dim x As Integer, m1 As Integer, m2 As Integer
Dim k As Integer
m1 = 2
For x = 2 To 1000
If Cells(x, 1) = "" Then Exit Sub
If Cells(x, 3) <> Cells(x + 1, 3) Then
m2 = x
Rows(x + 1).Insert
Cells(x + 1, "c") = Cells(x, "c") & " "
Cells(x + 1, "h") = "=sum(h" & m1 & ":h" & m2 & ")"
Cells(x + 1, "h").Resize(1, 4).FillRight
Cells(x + 1, "i") = ""
x = x + 1
m1 = m2 + 2
End If
Next x
End Sub
Sub c44()
'
Dim x As Integer
Dim t As Integer
t = Range("c65536").End(xlUp).Row
For x = t To 2 Step -1
If Cells(x, 3) <> Cells(x - 1, 3) Then
Rows(x).Insert
Cells(Cells(x, "C").Offset(1, 0).End(xlDown).Row + 1, "C") = Cells(Cells(x, "C").Offset(1,
0).End(xlDown).Row, "C") & " "
Cells(Cells(x, "H").Offset(1, 0).End(xlDown).Row + 1, "H") = _
Application.Sum(Range(Cells(x, "h").Offset(1, 0), Cells(x, "H").Offset(1, 0).End(xlDown)))
End If
Next x
End Sub
Option Explicit
'1 ()
'2
Sub c1() ',
Dim hao As Integer
Dim icount As Integer
icount = Application.WorksheetFunction.CountIf(Sheets("").[b:b], [g3])
If icount > 0 Then
MsgBox ""
MsgBox Application.WorksheetFunction.Match([g3], Sheets("").[b:b], 0)
End If
End Sub
'3 Find
Sub c2()
Dim r As Integer, r1 As Integer
Dim icount As Integer
icount = Application.WorksheetFunction.CountIf(Sheets("").[b:b], [g3])
If icount > 0 Then
r = Sheets("").[b:b].Find(Range("G3"), Lookat:=xlWhole).Row '===
r1 = Sheets("").[b:b].Find([g3], , , , , xlPrevious).Row
MsgBox r & ":" & r1
End If
End Sub
Sub c3() '
MsgBox Sheets("").Cells.Find("*", , , , , xlPrevious).Row
End Sub
Sub ()
Dim c As Integer '
Dim r As Integer '
Dim cr As Integer '
With Sheets("")
c = Application.CountIf(.[b:b], Range("g3"))
If c > 0 Then
MsgBox ","
Exit Sub
Else
r = Application.CountIf(Range("b6:b10"), "<>")
cr = .[b65536].End(xlUp).Row + 1
.Cells(cr, 1).Resize(r, 1) = Range("e3")
.Cells(cr, 2).Resize(r, 1) = Range("g3")
.Cells(cr, 3).Resize(r, 1) = Range("c3")
.Cells(cr, 4).Resize(r, 6) = Cells(6, 2).Resize(r, 6).Value
MsgBox ""
End If
End With
End Sub
Sub ()
Dim c As Integer '
Dim r As Integer '
With Sheets("")
c = Application.CountIf(.[b:b], Range("g3"))
If c = 0 Then
MsgBox ""
Exit Sub
Else
r = .[b:b].Find(Range("g3"), , , , , xlNext).Row
Range("c3") = .Cells(r, 3)
Range("e3") = .Cells(r, 1)
Cells(6, 2).Resize(c, 5) = .Cells(r, 4).Resize(c, 5).Value
MsgBox ""
End If
End With
End Sub
Sub ()
Dim c As Integer '
Dim r As Integer '
With Sheets("")
c = Application.CountIf(.[b:b], Range("g3"))
If c = 0 Then
MsgBox ""
Exit Sub
Else
r = .[b:b].Find(Range("g3"), , , , , xlNext).Row
.Range(r & ":" & c + r - 1).Delete
MsgBox ""
End If
End With
End Sub
Sub ()
Call
Call
End Sub
VBA80 17 excel ()
excel excel excel
excel
End Sub
End Sub
End Sub
End Sub
End Sub
End Sub
End Sub
MsgBox ""
End Sub
VBA80 19 excel
excel
Public WithEvents app As Excel.Application=== excel
End Sub
End Sub
Option Explicit
End Sub
2_ VBA
'1 VBA
'2 VBA
' VBA ,
'1)
'array(1,2)
'array(array(1,2,4),array("a","b","c"))
'2)
'x(4) ' 5 0~4
'arr(1 to 10) ' 10 1~10
'arr(1 to 10,1 to 2) '10 2 20
'arr(1 to 10,1 to 2,1 to 3) ' 10*2*3=60
'3)
'arr() '
3_VBA
' VBA
'1()
'2
Sub t3()
Dim arr()
Dim row
row = Sheets("sheet2").Range("a65536").End(xlUp).row - 1
ReDim arr(1 To row)
For x = 1 To row
arr(x) = Cells(x, 1)
Next x
Stop
End Sub
'3
'
'(5)
'(3,2)
':
Sub d1()
Dim arr, arr1()
Dim x As Integer, k As Integer, m As Integer
arr = Range("a1:a10") '== arr 10 1
Next x
Stop
End Sub
'2
'Lbound() ()
'Ubound() ()
'Ubound(,1) ( 1 )
'Ubound(,2) ( 2 )
Sub d6()
Dim arr
Dim k, m
arr = Range("a2:d5")
For x = 1 To UBound(arr, 1)
Next x
End Sub
'2
'
'
'ReDim Preserve arr()
'
1 sheet1
Private Sub ComboBox1_GotFocus()
Dim arr(), x, arr1, k
arr1 = Range("a1:a10")
For x = 1 To UBound(arr1)
If arr1(x, 1) > 10 Then
k = k + 1
ReDim Preserve arr(1 To k)
arr(k) = arr1(x, 1)
End If
Next x
ComboBox1.List = arr
End Sub
Sub d7()===
Dim arr, arr1()
arr = Range("a1:d6")
Dim x, k
For x = 1 To UBound(arr)
If arr(x, 1) = "B" Then
k = k + 1
ReDim Preserve arr1(1 To 4, 1 To k)
arr1(1, k) = arr(x, 1)
arr1(2, k) = arr(x, 2)
arr1(3, k) = arr(x, 3)
arr1(4, k) = arr(x, 4)
End If
Next x
Range("a8").Resize(k, 4) = Application.Transpose(arr1)
End Sub
Sub d8()
Dim arr, arr1(1 To 100000, 1 To 4)
arr = Range("a1:d6")
Dim x, k
For x = 1 To UBound(arr)
If arr(x, 1) = "B" Then
k = k + 1
arr1(k, 1) = arr(x, 1)
arr1(k, 2) = arr(x, 2)
arr1(k, 3) = arr(x, 3)
arr1(k, 4) = arr(x, 4)
End If
Next x
Range("a15").Resize(k, 4) = arr1
End Sub
'3
' earse
Sub d9()===
Dim arr, arr1(1 To 1000, 1 To 1)
Dim x, m, k
arr = Range("a1:a16")
For x = 1 To UBound(arr)
If arr(x, 1) <> "" Then
k = k + 1
arr1(k, 1) = arr(x, 1)
Else
m = m + 1
Range("c1").Offset(0, m).Resize(k) = arr1
Erase arr1
k = 0
End If
Next x
End Sub
VBA80_ 23 .VBA -4
VBA
'1
Sub s()
Dim arr1()
End Sub
'2
' application.Sum ()
'3
Sub s1()
End Sub
'3
Sub s2()
Dim arr
On Error Resume Next
arr = Array("a", "c", "b", "f", "d")
MsgBox Application.Match("f", arr, 0)
If Err.Number = 13 Then
MsgBox ""
End If
End Sub
' 1split
' VBA , 0
'split(,)
Sub t1()
Dim sr, arr
sr = "A-BC-FGR-H"
arr = VBA.Split(sr, "-")===Split
MsgBox sr(2)
MsgBox Join(arr, ",")===Join
End Sub
' 2Filter
'
'Filter(,,/)
'true
Sub t2()
Dim arr, arr1, arr2
arr = Application.Transpose(Range("A2:A10"))===.Transpose
'3index
'
' Application.Index(,0,))
' Application.Index(,,0))
Sub t3()
Dim arr, arr1, arr2
arr = Range("a2:d6")
arr1 = Application.Index(arr,0, 1)
arr2 = Application.Index(arr, 4, 0)
Stop
End Sub
'4vlookup
'Vlookup VBA VBA
Sub t4()
Dim arr, arr1
arr = Range("a2:d6")
arr1 = Application.VLookup(Array("B", "C"), arr, 4, 0)
End Sub
Sub t55()
Dim T
T = Timer
Dim arr, arr1(1 To 4, 1 To 2), x
arr1(1, 1) = "B"
arr1(2, 1) = "C"
arr1(3, 1) = "G"
arr1(4, 1) = "R"
' arr = Range("a1:d10000")
For x = 2 To 10000
Select Case Cells(x, 1)
Case "B"
arr1(1, 2) = arr1(1, 2) + Cells(x, 2)
Case "C"
arr1(2, 2) = arr1(2, 2) + Cells(x, 2)
Case "G"
arr1(3, 2) = arr1(3, 2) + Cells(x, 2)
Case "R"
arr1(4, 2) = arr1(4, 2) + Cells(x, 2)
End Select
Next x
MsgBox Timer - T
End Sub
VBA80_ 24 .VBA -5
'
' range
'
'>255
Sub ()
Range("a2:d2,a7:d7,a10:d10").Interior.ColorIndex = 3
End Sub
Sub ()
Dim x As Integer
Dim t
t = Timer
For x = 2 To Range("a65536").End(xlUp).Row
If Range("d" & x) > 500 Then
Range(Cells(x, 1), Cells(x, 4)).Interior.ColorIndex = 3
End If
Next x
MsgBox Timer - t
End Sub
Sub ()
Range("a:d").Interior.ColorIndex = xlNone
End Sub
Sub ()
Dim arr, t
Dim x As Integer
Dim sr As String, sr1 As String
t = Timer
arr = Range("d2:d" & Range("a65536").End(xlUp).Row)
For x = 1 To UBound(arr)
If x = UBound(arr) And sr <> "" Then Range(Left(sr, Len(sr) - 1)).Interior.ColorIndex = 3
If arr(x, 1) > 500 Then
sr1 = sr
sr = sr & "A" & x + 1 & ":D" & x + 1 & ","
If Len(sr) > 255 Then
sr = sr1
Range(Left(sr, Len(sr) - 1)).Interior.ColorIndex = 3
sr = ""
End If
End If
Next x
MsgBox Timer - t
End Sub
Sub 2()
Dim arr, t
Dim x As Integer, x1 As Integer
Dim sr As String, sr1 As String
===
t = Timer
arr = Range("d2:d" & Range("a65536").End(xlUp).Row)
For x = 1 To UBound(arr)
If x = UBound(arr) Then Range(Left(sr, Len(sr) - 1)).Interior.ColorIndex = 3
If arr(x, 1) > 500 Then
sr1 = sr
x1 = x + 1
Do
x = x + 1
Loop Until arr(x, 1) <= 500
End Sub
Sub 3()
Dim arr, t
Dim x As Integer, x1 As Integer
Dim sr As String, sr1 As String
t = Timer
arr = Range("d2:d" & Range("a65536").End(xlUp).Row)
For x = 1 To UBound(arr)
If x = UBound(arr) Then Application.Intersect(Range("a:d"), Range(Left(sr, Len(sr) -
1))).Interior.ColorIndex = 3
If arr(x, 1) > 500 Then
sr1 = sr
x1 = x + 1
Do
x = x + 1
Loop Until arr(x, 1) <= 500
Sub ()
Dim arr, temp, x, y, t, k
t = Timer
arr = Range("a1:a10")
For x = 1 To UBound(arr) - 1
For y = x + 1 To UBound(arr) '
If arr(x, 1) > arr(y, 1) Then '
temp = arr(x, 1)
arr(x, 1) = arr(y, 1)
arr(y, 1) = temp
End If
Next y
Next x
Range("b3").Resize(x) = ""
Range("b3").Resize(x) = arr
'Range("b2") = Timer - t
MsgBox k
End Sub
Sub ()
Dim arr, temp, x, y, t, k
For x = 1 To 9
Range("a" & x).Interior.ColorIndex = 3
For y = x + 1 To 10 '
Range("a" & y).Interior.ColorIndex = 4
If Cells(x, 1) > Cells(y, 1) Then '
temp = Cells(x, 1)
Cells(x, 1) = Cells(y, 1)
Cells(y, 1) = temp
2 End If
Range("a" & y).Interior.ColorIndex = xlNone
Next y
Range("a" & x).Interior.ColorIndex = xlNone
Next x
End Sub
Sub ()
Dim arr, temp, x, y, t, iMax, k, k1, k2
t = Timer
arr = Range("a1:a10")
For x = UBound(arr) To 1 + 1 Step -1
iMax = 1 '
For y = 1 To x
If arr(y, 1) > arr(iMax, 1) Then iMax = y
Next y
temp = arr(iMax, 1)
arr(iMax, 1) = arr(x, 1)
arr(x, 1) = temp
Next x
'Range("c3").Resize(UBound(arr)) = ""
'Range("c3").Resize(UBound(arr)) = arr
'Range("c2") = Timer - t
'MsgBox k1
End Sub
Sub ()
Dim arr, temp, x, y, t, iMax, k, k1, k2
For x = 10 To 2 Step -1
iMax = 1
Range("a" & x).Interior.ColorIndex = 3
For y = 1 To x
Range("a" & y).Interior.ColorIndex = 4
If Cells(y, 1) > Cells(iMax, 1) Then
Range("a" & iMax).Interior.ColorIndex = xlNone
iMax = y
End If
Range("a" & y).Interior.ColorIndex = xlNone
Range("a" & iMax).Interior.ColorIndex = 6
Next y
temp = Cells(iMax, 1)
Cells(iMax, 1) = Cells(x, 1)
Cells(x, 1) = temp
Range("a" & x).Interior.ColorIndex = xlNone
Range("a" & iMax).Interior.ColorIndex = xlNone
Next x
End Sub
VBA80_ 26 .VBA -7VBA
Sub ()
Dim arr, temp, x, y, t, iMax, k, k1, k2
t = Timer
arr = Range("a1:a10")
For x = 2 To UBound(arr)
For y = x - 1 To 1 Step -1
If arr(y, 1) <= temp Then Exit For
arr(y + 1, 1) = arr(y, 1)
'k1 = k1 + 1
Next y
arr(y + 1, 1) = temp
'k2 = k2 + 1
Next
' Range("d3").Resize(UBound(arr)) = ""
' Range("d3").Resize(UBound(arr)) = arr
'Range("d2") = Timer - t
MsgBox k1
End Sub
Sub ()
On Error Resume Next
Dim arr, temp, x, y, t, iMax, k
For x = 2 To 10
End Sub
Sub ()
Dim arr
Dim , , x, y, temp, t
t = Timer
arr = Range("a1:a30")
= UBound(arr) - LBound(arr) + 1
= 1
If > 13 Then
Do While <
= * 3 + 1
Loop
= \ 9
End If
' Stop
Do While
For x = LBound(arr) + To UBound(arr)
temp = arr(x, 1)
For y = x - To LBound(arr) Step -
If arr(y, 1) <= temp Then Exit For
arr(y + , 1) = arr(y, 1)
' k1 = k1 + 1
Next y
arr(y + , 1) = temp
Next x
= \ 3
Loop
' MsgBox k1
'Range("e3").Resize(5000) = ""
Range("d1").Resize(UBound(arr)) = arr
'Range("e2") = Timer - t
End Sub
Sub ()
Dim arr, temp, x
arr = Range("a1:a" & Range("a65536").End(xlUp).Row)
For x = 1 To UBound(arr)
num = Int(Rnd() * UBound(arr) + 1)
temp = arr(num, 1)
arr(num, 1) = arr(x, 1)
arr(x, 1) = temp
Next x
Range("a1").Resize(x - 1) = arr
End Sub
Sub ()
Dim arr
Dim , , x, y, temp, t
t = Timer
arr = Range("a1:a" & Range("a65536").End(xlUp).Row)
= UBound(arr) - LBound(arr) + 1
= 1
If > 13 Then
Do While <
= * 3 + 1
Loop
= \ 9
End If
' Stop
Do While
For x = LBound(arr) + To UBound(arr)
temp = Cells(x, 1)
Range("a" & x).Interior.ColorIndex = 3
For y = x - To LBound(arr) Step -
Range("a" & y).Interior.ColorIndex = 6
If Cells(y, 1) <= temp Then Exit For
Cells(y + , 1) = Cells(y, 1)
' k1 = k1 + 1
Next y
Cells(y + , 1) = temp
Range("a1:a30").Interior.ColorIndex = xlNone
Next x
= \ 3
Loop
' MsgBox k1
'Range("e3").Resize(5000) = ""
' Range("d1").Resize(UBound(arr)) = arr
'Range("e2") = Timer - t
End Sub
VBA80_ 27 .VBA -1
'1 VBA
'dictionary
' key ,
' item, key item
'Key item
'A 10
'B 20
'C 30
'Z 10
'2
':
'1) A
'2) key item key item
'3
'
'
'4
' scrrun.dll
'
'Set d = CreateObject("scripting.dictionary")
'
'--- scrrun.dll-
'1
Sub t1()
Dim d As New Dictionary
Dim x As Integer
For x = 2 To 4
d.Add Cells(x, 1).Value, Cells(x, 2).Value
Next x
MsgBox d.Keys(1)
'Stop
End Sub
'2
Sub t2()
Dim d
Dim arr
Dim x As Integer
Set d = CreateObject("scripting.dictionary")
For x = 2 To 4
d.Add Cells(x, 1).Value, Cells(x, 2).Value
Next x
'MsgBox d("")
'MsgBox d.Keys(2)
Range("d1").Resize(d.Count) = Application.Transpose(d.Keys)
Range("e1").Resize(d.Count) = Application.Transpose(d.Items)
arr = d.Items
End Sub
'3
Sub t3()
Dim d As New Dictionary
Dim x As Integer
For x = 2 To 4
d.Add Cells(x, 1).Value, Cells(x, 2).Value
Next x
d("") = 78
MsgBox d("")
d("") = 100
MsgBox d("")
End Sub
'4
Sub t4()
Dim d As New Dictionary
Dim x As Integer
For x = 2 To 4
d(Cells(x, 1).Value) = Cells(x, 2).Value
Next x
d.Remove ""
' MsgBox d.Exists("")
d.RemoveAll
MsgBox d.Count
End Sub
'
Sub t5()
Dim d As New Dictionary
Dim x
d.CompareMode = BinaryCompare====
For x = 1 To 5
d(Cells(x, 1).Value) = ""
Next x
Stop
End Sub
VBA80_ 28 .VBA -2
Sub ()===
Dim d As New Dictionary
Dim arr, x
arr = Range("a2:a12")
For x = 1 To UBound(arr)
d(arr(x, 1)) = ""
Next x
Range("c2").Resize(d.Count) = Application.Transpose(d.Keys)
End Sub
Sub ()===
Dim d As New Dictionary
Dim arr, x
arr = Range("a2:b10")
For x = 1 To UBound(arr)
d(arr(x, 1)) = d(arr(x, 1)) + arr(x, 2) 'key item
Next x
Range("d2").Resize(d.Count) = Application.Transpose(d.Keys)
Range("e2").Resize(d.Count) = Application.Transpose(d.Items)
End Sub
Sub ()
Dim d As New Dictionary
Dim x, y
Dim arr
For x = 3 To 5
arr = Sheets(x).Range("a2").Resize(Sheets(x).Range("a65536").End(xlUp).Row - 1, 2)
For y = 1 To UBound(arr)
d(arr(y, 1)) = arr(y, 2)
d(arr(y, 2)) = arr(y, 1)
Next y
Next x
MsgBox d("C1")
MsgBox d("")
End Sub
VBA80_ 29 .VBA
Sub ()
Dim (1 To 10000, 1 To 3)
Dim
Dim arr, x, k
Dim d As New Dictionary
arr = Range("a2:c" & Range("a65536").End(xlUp).Row)
For x = 1 To UBound(arr)
If d.Exists(arr(x, 1)) Then
= d(arr(x, 1))
(, 2) = (, 2) + arr(x, 2)
(, 3) = (, 3) + arr(x, 3)
Else
k = k + 1
d(arr(x, 1)) = k
(k, 1) = arr(x, 1)
(k, 2) = arr(x, 2)
(k, 3) = arr(x, 3)
End If
Next x
Range("f2").Resize(k, 3) =
End Sub
Sub ()
Dim (1 To 10000, 1 To 4)
Dim
Dim arr, x As Integer, sr As String, k As Integer
Dim d As New Dictionary
arr = Range("a2:d" & Range("a65536").End(xlUp).Row)
For x = 1 To UBound(arr)
sr = arr(x, 1) & "-" & arr(x, 2)
If d.Exists(sr) Then
= d(sr)
(, 3) = (, 3) + arr(x, 3)
(, 4) = (, 4) + arr(x, 4)
Else
k = k + 1
d(sr) = k
(k, 1) = arr(x, 1)
(k, 2) = arr(x, 2)
(k, 3) = arr(x, 3)
(k, 4) = arr(x, 4)
End If
Next x
Range("g2").Resize(k, 4) =
End Sub
Sub ()
Dim d As New Dictionary
Dim (1 To 10000, 1 To 7)
Dim ,
Dim arr, x, k
For x = 1 To UBound(arr)
= (InStr("1 2 3 4 5 6 ", arr(x, 2)) + 1) / 2 + 1
If d.Exists(arr(x, 1)) Then
= d(arr(x, 1))
(, ) = (, ) + arr(x, 3)
Else
k = k + 1
d(arr(x, 1)) = k
(k, 1) = arr(x, 1)
(k, ) = arr(x, 3)
End If
Next x
Range("f2").Resize(k, 7) =
End Sub