Excel VBA使用总结
生活随笔
收集整理的這篇文章主要介紹了
Excel VBA使用总结
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Excel VBA
- 1.入門
- 1.1開啟VBA之旅
- 2.語法
- 2.1清除單元格內容
- 2.2vba設密碼
- 2.3以前的代碼
- 2.4使用正則
- 2.5使用stack
都說世界上最好的語言就是PPT,工作報告,年度總結。。。。
到處都有它的身影,更是被高手設計的惟妙惟肖。
今天,我們不爭第一,勇奪第二。
還在為設計報表而發愁嗎,還在為統計數據而苦惱嗎,今天,它來了,它跳著芭蕾走來了(一片稀稀落落的掌聲),有個大神說,Excel除了不能給你生孩子,就沒有它搞不定的,這????
1.入門
Excel VBA官方文檔: Office Visual Basic For Application Document.
Visual Basic for Applications(VBA)是Visual Basic的一種宏語言,是微軟開發出來在其桌面應用程序中執行通用的自動化(OLE)任務的編程語言。主要能用來擴展Windows的應用程式功能,也可說是一種應用程式視覺化的Basic腳本。 vba的好處:1.規范用戶操作,控制用戶的操作行為2.操作界面人性化,方便用戶操作3.自動化辦公,可以將多個步驟的手工操作通過一步來實現4.Word和ppt里面也可以用vba來操作 5.實現一些vb無法實現的功能6.用vba制作excel登錄系統7.利用vba可以excel內輕松開發功能強大的自動化程序1.1開啟VBA之旅
首先,普及一下基礎知識
每層關系都是有上限的
2.語法
語法沒有太多要講的,不懂自己就錄制宏,然后再修修改改。
Option Explicit種竡琌祘Α家艙い眏–祘Α家艙跑計常ゲ斗絋Ч俱㎝﹚竡ㄤ嘿,絛瞅籔. 還真的亂碼了,留著做個紀念Option Explicit 的意義1.在程式模組中強迫每個在程式模組里的變數必須明確完整的宣告和定義其名稱,范圍和類型2.并且可以避免打錯變數或在有效范圍內設定兩個相同的變數名稱2.1清除單元格內容
Sub clearContent(xlWorkbook As Workbook)Dim tableCount As IntegerDim columnCount As IntegerDim xlWorksheet1 As WorksheetDim xlWorksheet2 As WorksheetDim i As IntegerDim j As Integer//TableInfo 就是某一頁的名字Set xlWorksheet1 = xlWorkbook.Sheets("TableInfo")Set xlWorksheet2 = xlWorkbook.Sheets("ColumnInfo")tableCount = xlWorksheet1.[B65535].End(xlUp).RowcolumnCount = xlWorksheet2.[B65535].End(xlUp).RowWith xlWorksheet1For i = 4 To tableCountFor j = 2 To 30.Cells(i, j).Value = ""Next jNextEnd WithWith xlWorksheet2For i = 4 To columnCountFor j = 2 To 50.Cells(i, j).Value = ""Next jNextEnd With End Sub2.2vba設密碼
亂碼問題很惡心,我也沒解決,最后就是都用英文
如果打開excel,就要啟動的方法,都放在這里
2.3以前的代碼
很多都不記得了,這是留給我自己看的,謝謝
Option Explicit'TableInfo max count Const MAX_ITTABLE_LEN = 5000 'ColumnInfo max count Const MAX_TABLECOL_LEN = 5000 Const MAX_COL_NUMBER = 1000 Const MAX_SOURCE_COLUMN_NUM = 100 Const PGM_PASSWORD = "zaq12wsx" Dim columnInfoCollection As Collection Dim BlankCollection As Collection Dim ISACollection As Collection Dim TableInfoRowErrCollection As Collection Dim ColumnInfoRowErrCollection As CollectionType SYSTEM_TBLSystem As StringTable As StringType As StringCondition As StringColumnCheck As Boolean End TypePublic Type StackStructSize As IntegerPointer As IntegerMaxElementCount As IntegerElement() As Integer End Type 'for method 23,get table properties,pk-pk or pks-pk Sub CHECK_SRC_TYPE_RULE_PUBLIC()Dim xlWorksheet2 As WorksheetDim columnCount As IntegerDim PKCount As IntegerDim FKCount As IntegerDim i As IntegerDim BeginRow As Integer, firstRow As Integer, EndRow As IntegerPKCount = 33FKCount = 34BeginRow = 4Set xlWorksheet2 = ThisWorkbook.Sheets("ColumnInfo")columnCount = xlWorksheet2.[A65535].End(xlUp).RowSet columnInfoCollection = New CollectionWith xlWorksheet2'Construct the ColumnInfoCollection to keep track of every combination of :' 1. SYS_ID+DB_NAME+SCHEMA_NAME+TABLENAME+'PKCount'' 2. SYS_ID+DB_NAME+SCHEMA_NAME+TABLENAME+'fKCount'For i = 4 To columnCountDim fullQualifiedName As StringDim fullQualifiedNameNext As StringfullQualifiedName = .Range("B" & i) & "-" & .Range("C" & i) & "-" & .Range("D" & i) & "-" & .Range("E" & i)fullQualifiedNameNext = .Range("B" & i + 1) & "-" & .Range("C" & i + 1) & "-" & .Range("D" & i + 1) & "-" & .Range("E" & i + 1)If .Range("A" & i) <> "" ThenIf fullQualifiedName <> fullQualifiedNameNext ThenfirstRow = BeginRowEndRow = iBeginRow = i + 1PKCount = Application.WorksheetFunction.CountIf(.Range("M" & firstRow & ":M" & EndRow), "P")FKCount = Application.WorksheetFunction.CountIf(.Range("O" & firstRow & ":O" & EndRow), "<>")columnInfoCollection.Add PKCount, fullQualifiedName & "PKCount"columnInfoCollection.Add FKCount, fullQualifiedName & "FKCount"End IfEnd IfNext iFor i = 4 To columnCountfullQualifiedName = .Range("B" & i) & "-" & .Range("C" & i) & "-" & .Range("D" & i) & "-" & .Range("E" & i)'Check If the collection contains the Key, get value from it iff key existIf Contains(columnInfoCollection, fullQualifiedName & "PKCount") Then.Cells(i, 33) = columnInfoCollection.Item(fullQualifiedName & "PKCount")End If'Check If the collection contains the Key, get value from it iff key existIf Contains(columnInfoCollection, fullQualifiedName & "FKCount") Then.Cells(i, 34) = columnInfoCollection.Item(fullQualifiedName & "FKCount")End IfNext iEnd WithEnd Sub Sub Check_CDM()Dim Status_Base As BooleanDim Status_Detail As Boolean' Dim SystemTables(MAX_ITTABLE_LEN) As SYSTEM_TBLDim xlErrSheet As WorksheetDim ctrErrBase As IntegerDim ctrErrDetail As IntegerDim xlWorksheet1 As WorksheetDim xlWorksheet2 As WorksheetApplication.ScreenUpdating = FalseApplication.StatusBar = ""Set xlErrSheet = ThisWorkbook.Sheets("CheckReport")Set xlWorksheet1 = ThisWorkbook.Sheets("TableInfo")Set xlWorksheet2 = ThisWorkbook.Sheets("ColumnInfo")'Call xlErrSheet.Unprotect(PGM_PASSWORD)'Call xlWorksheet1.Unprotect(PGM_PASSWORD)'Call xlWorksheet2.Unprotect(PGM_PASSWORD)調用下面的方法Call Check_CDM_FILE_Initial(ThisWorkbook)Call CHECK_SRC_TYPE_RULE_PUBLIC End Sub Sub Check_CDM_FILE_Initial(xlWorkbook As Workbook)Dim xlErrSheet As WorksheetDim xlWorksheet1 As WorksheetDim xlWorksheet2 As WorksheetDim i As IntegerDim j As IntegerDim tableCount As IntegerDim columnCount As IntegerDim errCountTab As IntegerDim errCountCol As IntegerSet xlErrSheet = xlWorkbook.Sheets("CheckReport")Set xlWorksheet1 = xlWorkbook.Sheets("TableInfo")Set xlWorksheet2 = xlWorkbook.Sheets("ColumnInfo")tableCount = xlWorksheet1.[B65535].End(xlUp).RowcolumnCount = xlWorksheet2.[B65535].End(xlUp).RowerrCountTab = xlErrSheet.[B65535].End(xlUp).RowerrCountCol = xlErrSheet.[E65535].End(xlUp).RowWith xlErrSheet' Clean Up the Check Report Header.Cells(2, 3).Value = "No Error".Cells(3, 3).Value = "".Cells(4, 3).Value = ""' Clean Up the Check Report 郎膀セ戈癟(TableInfo)If errCountTab > errCountCol ThenFor i = 8 To errCountTabFor j = 2 To 6.Cells(i, j).Value = "".Cells(i, j).Value = ""Next jNextElseFor i = 8 To errCountColFor j = 2 To 6.Cells(i, j).Value = "".Cells(i, j).Value = ""Next jNextEnd IfEnd WithWith xlWorksheet1'https://access-excel.tips/excel-vba-color-code-list/'color index values for referenceWith .Range(.Cells(4, 2), .Cells(tableCount, 5))' 20.Interior.ColorIndex = 33End WithWith .Range(.Cells(4, 6), .Cells(tableCount, 26))' 20.Interior.ColorIndex = 44End WithWith .Range(.Cells(tableCount + 1, 2), .Cells(tableCount + 5, 26)).Interior.ColorIndex = 44End WithEnd WithWith xlWorksheet2'https://access-excel.tips/excel-vba-color-code-list/'color index values for referenceWith .Range(.Cells(4, 2), .Cells(columnCount, 6))' -4142.Interior.ColorIndex = 33End WithWith .Range(.Cells(4, 7), .Cells(columnCount, 30))' 20.Interior.ColorIndex = 44End WithWith .Range(.Cells(columnCount + 1, 2), .Cells(columnCount + 5, 30)).Interior.ColorIndex = 44End WithEnd WithEnd Sub2.4使用正則
在VBA的界面要開啟RegExp
2.5使用stack
來判斷括號對稱的問題
Option Explicit Public Type StackStructSize As IntegerPointer As IntegerMaxElementCount As IntegerElement() As Integer End Type ---------------------------------------------------- ' create a stack棧 Public Function CreateStack(StackSize As Integer) As StackStructDim h As StackStructh.Pointer = -1h.Size = 0h.MaxElementCount = StackSizeReDim h.Element(StackSize - 1) As IntegerCreateStack = h End Function ---------------------------------------------------- ' Determines if the stack is empty Public Function StackEmpty(h As StackStruct) As BooleanStackEmpty = (h.Pointer = -1) End Function ---------------------------------------------------- ' Determines if the stack is full Public Function StackFull(h As StackStruct) As BooleanStackFull = (h.Size = h.MaxElementCount) End Function ---------------------------------------------------- 'Push the element onto the stack Public Function Push(ByRef h As StackStruct, Ikey As Integer) As Boolean'if stack full,return FalseIf StackFull(h) ThenPush = FalseExit FunctionElseh.Pointer = h.Pointer + 1h.Element(h.Pointer) = Ikeyh.Size = h.Size + 1End If End Function --------------------------------------------------- '將Pops the element off the stack Public Function Pop(ByRef h As StackStruct) As VariantIf StackEmpty(h) ThenPop = FalseElsePop = h.Element(h.Pointer)h.Pointer = h.Pointer - 1h.Size = h.Size - 1End If End Function ---------------------------------------------------- Function symmetrical(content As String) As BooleanDim abcd As StringDim arr As VariantDim i As IntegerDim result As StringDim contentLong As IntegerDim stack As StackStructstack = CreateStack(20)contentLong = Len(content)For i = 1 To contentLongresult = Mid(content, i, 1)If (result = "[") ThenIf StackFull(stack) ThenElsestack.Pointer = stack.Pointer + 1stack.Size = stack.Size + 1End IfElseIf (result = "]") ThenIf StackEmpty(stack) Then'Debug.Print "no pair"symmetrical = FalseExit FunctionElsestack.Pointer = stack.Pointer - 1stack.Size = stack.Size - 1End IfElseEnd IfNext'final resultIf StackEmpty(stack) Then'Debug.Print "pair"symmetrical = TrueElse'Debug.Print "no pair"symmetrical = FalseEnd If End Function --------------------------------------------------- Sub text9()Dim name As Stringname = "abc][][]"Call symmetrical(name) End Sub總結
以上是生活随笔為你收集整理的Excel VBA使用总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: chrome官网下载离线安装包
- 下一篇: RayData大数据可视化教程(1)——