模塊化編程的意義在于可以批量生產(chǎn),插拔式開發(fā),往往可以事半功倍,這需要不斷總結(jié)梳理模塊庫(kù),細(xì)分場(chǎng)景,涵蓋的面越廣,越能夠提高開發(fā)效率,且使代碼的標(biāo)準(zhǔn)化程度更高,可移植性更強(qiáng),從而是產(chǎn)品更健壯。
筆者總結(jié)的這一系列常見方法可以極大的豐富插件庫(kù),拓寬思路,只須體會(huì)并領(lǐng)會(huì)其中的要義,就可以很方便靈活的實(shí)現(xiàn)現(xiàn)實(shí)中的大部分場(chǎng)景,甚至做到很少的幾行代碼就可以實(shí)現(xiàn)很復(fù)雜的功能……
這里有些方法是具體的示例,需要在實(shí)際需要中活學(xué)活用,切忌生搬硬套。
01
最后非空單元格的行號(hào)及列號(hào)
適用場(chǎng)景:動(dòng)態(tài)尋找行列范圍,多用于判斷循環(huán)邏輯的結(jié)束行和結(jié)束列。
ActiveSheet.Range("F300").End(xlUp).Row
ActiveSheet.Range("A1").End(xlRight).Column
02
取排名前/后27%的閾值
適用場(chǎng)景:獲取一批數(shù)據(jù)中前或后排在27%處的值,從而得到前或后27%的對(duì)象。
=LARGE(R2:R55,ROUND(COUNT(R2:R55)*0.27,0))
=SMALL(R2:R55,ROUND(COUNT(R2:R55)*0.27,0))
03
匹配行列交叉點(diǎn)
適用場(chǎng)景:先將配條件的行列號(hào)獲取到,并獲取其對(duì)應(yīng)的單元格的值。
=INDEX($A:$BG,MATCH($A$2,$A:$A,0),MATCH(A$3,1:1,0))
04
排序方法
適用場(chǎng)景:在對(duì)行數(shù)據(jù)進(jìn)行邏輯處理的時(shí)候,某列的值是否發(fā)生變化往往作為臨界條件,這需要先對(duì)行數(shù)據(jù)進(jìn)行排序使相同的數(shù)據(jù)挨在一起。
'wps
ws.Rows("2:" & ws.UsedRange.Rows.Count + 1).Select
With ws.Sort
With .SortFields
.Clear
.Add Key:=Range("P2:P" & wsLine), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:=""
.Add Key:=Range("C2:C" & wsLine), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:=""
End With
.Header = xlNo
.Orientation = xlSortColumns
.MatchCase = False
.SortMethod = xlPinYin
.SetRange Rng:=Selection
.Apply
End With
'excel
ws.Sort.SortFields.Clear
ws.Sort.SortFields.Add2 Key:=Range("C2:C" & wsLine) sortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ws.Sort.SortFields.Add2 Key:=Range("K2:K" & wsLine) SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ws.Sort
.SetRange Range("A1:N" & wsLine)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
05
查找數(shù)值位置
適用場(chǎng)景:當(dāng)想知道某值在工作表的什么位置(即所在行列)
Set c = ws1.Range("A3:A65535").Find(myValue) '在對(duì)應(yīng)列中查找
If Not c Is Nothing Then msgbox c.row & c.column
06
增加批注
適用場(chǎng)景:當(dāng)匯總數(shù)據(jù)后還想方便的看到關(guān)鍵明細(xì)數(shù)據(jù),那么批注或者注釋可以做到。
ws2.Range(ws1CLetter & r).AddComment (myCommentYellow)
07
開始編碼模板
適用場(chǎng)景:所有的實(shí)現(xiàn)邏輯都會(huì)包含這幾部分,開始編碼前可以快速的搭建起框架。
'容錯(cuò)處理
On Error Resume Next
'變量定義區(qū)域
dim i as integer,j as integer,k as integer
Dim wk1 As Workbook, wk2 As Workbook, wk3 As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet
'變量初始化區(qū)域
Set wk1 = ActiveWorkbook
Set ws1 = wk1.Sheets("匯總表")
Application.StatusBar = "開始處理"
Application.ScreenUpdating = False
'主邏輯處理區(qū)域
'邏輯處理結(jié)束區(qū)域
Application.ScreenUpdating = True
Range("A1").Select
Application.StatusBar = ""
MsgBox ("完事了!")
08
禁用常用功能
適用場(chǎng)景:當(dāng)工作表的內(nèi)容需要禁止拷貝復(fù)制的時(shí)候,可以通過禁止相應(yīng)的功能實(shí)現(xiàn)。
一、Call EnableMenuItem(21, Allow) '調(diào)用cut(21剪切)、19復(fù)制、22粘貼、755選擇性粘貼
Sub EnableMenuItem(ctlId As Integer, Enabled As Boolean) '激活/禁用向下拖動(dòng)填充
Dim cBar As CommandBar '聲明變量
Dim cBarCtrl As CommandBarControl
For Each cBar In Application.CommandBars
If cBar.Name <> "Clipboard" Then
Set cBarCtrl = cBar.FindControl(ID:=ctlId, recursive:=True)
If Not cBarCtrl Is Nothing Then cBarCtrl.Enabled = Enabled
End If
Next
End Sub
二、Application.CutCopyMode = False
09
禁止另存為
適用場(chǎng)景:當(dāng)工作表的內(nèi)容需要禁止被另存的時(shí)候,需要增加workbook_BeforeSave過程,這個(gè)過程應(yīng)添加到Thisworkbook模塊
With targetBook.VBProject.VBComponents(sheetName).CodeModule
If Not .Find("workbook_BeforeSave", 1, 1, -1, -1) Then '避免重復(fù)寫入產(chǎn)生二義性
.InsertLines startLine, "Private Sub workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)"
.InsertLines startLine + 1, " "
.InsertLines startLine + 2, " On Error Resume Next"
.InsertLines startLine + 3, " Dim response As Long"
.InsertLines startLine + 4, " If SaveAsUI = True Then"
.InsertLines startLine + 5, " response = MsgBox(" & Chr(34) & " 該工作簿不允許用 另存為 來保存,你要用原工作簿名稱來保存嗎?" & Chr(34) & ", vbQuestion + vbOKCancel)"
.InsertLines startLine + 6, " Cancel = (response = vbCancel)"
.InsertLines startLine + 7, " If Cancel = False Then Me.Save" '首行顏色受影響
.InsertLines startLine + 8, " Cancel = True"
.InsertLines startLine + 9, " End If"
.InsertLines startLine + 10, " End sub"
End If
End With
10
禁用快捷鍵
適用場(chǎng)景:某個(gè)功能往往有很多種操作方法,如果要做的全面禁止,快捷鍵不應(yīng)被忽略。
With Application
Select Case Allow '允許選擇
Case Is = False '為假
.OnKey "^c", "" '"Ctrl+c"復(fù)制快捷鍵無效,并通知用戶
.OnKey "^v", "" '"Ctrl+v"粘貼快捷鍵無效,并通知用戶
.OnKey "^x", "" '"Ctrl+x"剪切快捷鍵無效,并通知用戶
.OnKey "+{DEL}", "" '"+{DEL}"刪除快捷鍵無效,并通知用戶
.OnKey "^{INSERT}", "" '"Ctrl+{INSERT}"插入快捷鍵無效,并通知用戶
Case Is = True '為真
.OnKey "^c" '"Ctrl+c"復(fù)制快捷鍵有效
.OnKey "^v" '"Ctrl+v"粘貼快捷鍵有效
.OnKey "^x" '"Ctrl+x"剪切快捷鍵有效
.OnKey "+{DEL}" '"+{DEL}"刪除快捷鍵有效
.OnKey "^{INSERT}" '"Ctrl+{INSERT}"插入快捷鍵有效
End Select '結(jié)束選擇
End With
聯(lián)系客服