之前就有了解過WPS Office的宏功能,并且mark了一下,昨天偶然百度到了門路,這里把一些過程簡單總結(jié)一下,分享給大家。
1.vba簡介
Visual Basic for Applications(VBA)是Visual Basic的一種宏語言,是微軟開發(fā)出來在其桌面應(yīng)用程序中執(zhí)行通用的自動化(OLE)任務(wù)的編程語言。主要能用來擴(kuò)展Windows的應(yīng)用程式功能,特別是Microsoft Office軟件。也可說是一種應(yīng)用程式視覺化的Basic腳本。該語言于1993年由微軟公司開發(fā)的的應(yīng)用程序共享一種通用的自動化語言——–Visual Basic For Application(VBA),實(shí)際上VBA是寄生于VB應(yīng)用程序的版本。
2.所需軟件:
WPS Office (10.1.0.7224)
也就是目前2018年4月14日安裝的最新版本,由此可以看出,和wps版本無關(guān)~
vba提取自WPS2012專業(yè)增強(qiáng)版
下載地址網(wǎng)上很多,貼出來我在百度云盤保存的
鏈接:https://pan.baidu.com/s/1HuBHlDmBfYH3zpvNo9c8yg 密碼:xr8j
進(jìn)階技能:VB語言
4.收藏資料
鏈接:https://pan.baidu.com/s/1lxn5OKdBzkJwQ9ptmURFLg 密碼:bdug
鏈接:https://pan.baidu.com/s/1dIVRJ0w0IOmQjYMx3b5-Ew 密碼:tjra
鏈接:https://pan.baidu.com/s/1_Mb2WdzetVNAnZfgf4KuyA 密碼:bcbp
5.簡單入門操作
- 簡單使用
1. 創(chuàng)建宏
首先注意:保存的文件格式是Microsoft Office Excel 2007 啟用宏的工作簿 (.xlsm),否則將出現(xiàn)以下提示
Range("A1").Value = "Hello,Macro!"
意思是給單元格A1賦值為”Hello,Macro!”
2. Msgbox
MsgBox是Visual Basic和VBS中的一個函數(shù),功能是彈出一個對話框,等待用戶單擊按鈕,并返回一個Integer值表示用戶單擊了哪一個按鈕。
“MsgBox”即為“Message Box”的縮寫,在英語中意為“信箱”。
『例1』單擊按鈕彈出對話框刪除所有內(nèi)容
step1:創(chuàng)建命令按鈕,開發(fā)工具->命令按鈕,在工作表合適位置拉出按鈕
Dim answer As Integeranswer = MsgBox("要清空工作表么?", vbYesNo + vbQuestion, "清空工作表")If answer = vbYes Then Cells.ClearContentsElse 'do nothingEnd If
運(yùn)行即可看到效果,在工作表中隨便輸入隨機(jī)數(shù)據(jù),單擊按鈕即可實(shí)現(xiàn)清除工作表的效果
step3:拓展
看到下拉框里面的內(nèi)容否?可以將動作設(shè)置成這些,根據(jù)命名就可以看出作用;另外,需要做其他效果搜一下Msgbox,看看相關(guān)函數(shù)就能模仿出其他效果。
Dim myValue As VariantmyValue = InputBox("輸個啥?", "InputBox函數(shù)", 1)Range("A1").Value = myValue
step1:創(chuàng)建按鈕,輸入代碼,運(yùn)行調(diào)試
3.工作簿和工作表對象
4. Range對象
5. 變量
6. IF Then語句
7. 循環(huán)
Dim i As Integeri = 1Do Until i > 6 Cells(i, 1).Value = 20 i = i + 1Loop
Dim i As LongColumns(1).Font.Color = vbBlackFor i = 1 To Rows.Count If Cells(i, 1).Value < Range("D2").Value And Not IsEmpty(Cells(i, 1).Value) Then Cells(i, 1).Font.Color = vbRed End IfNext i
實(shí)際上是把A列中下一個在B中不存在的數(shù)據(jù)不斷添加到B列中,遇到空數(shù)據(jù)直接跳出,不復(fù)制。(功能放到一個命令按鈕里面)
Dim toAdd As Boolean, uniqueNumbers As Integer, i As Integer, j As IntegerCells(1, 2).Value = Cells(1, 1).ValueuniqueNumbers = 1toAdd = True'第一個數(shù)字總是'唯一的',直接拷貝過去For i = 2 To Sheet1.Range("a65536").End(xlUp).Row'Sheet1.Range("A65536").End(xlUp).Row意思是從A65536向上找到最后一個非空單元格,返回其行號 If Cells(i, 1).Value <> "" Then '如果單元格非空,則繼續(xù) For j = 1 To uniqueNumbers '遍歷新的一列數(shù)據(jù),判斷是否重復(fù),重復(fù)則false to add,不重復(fù)跳出 If Cells(i, 1).Value = Cells(j, 2).Value Then toAdd = False End If Next j '跳出的都是true to add的,拷貝過去即可,拷完一個新列行號增一 If toAdd = True Then Cells(uniqueNumbers + 1, 2).Value = Cells(i, 1).Value uniqueNumbers = uniqueNumbers + 1 End If toAdd = True End IfNext i
Dim i As Integer, j As Integer, temp As Integer, rng As RangeSet rng = Range("A1").CurrentRegionFor i = 1 To rng.Count For j = i + 1 To rng.Count If rng.Cells(j) < rng.Cells(i) Then 'swap numbers temp = rng.Cells(i) rng.Cells(i) = rng.Cells(j) rng.Cells(j) = temp End If Next jNext i
Dim tempString As String, tempInteger As Integer, i As Integer, j As IntegerFor i = 1 To 5 Cells(i, 2).Value = WorksheetFunction.RandBetween(0, 1000)Next iFor i = 1 To 5 For j = i + 1 To 5 If Cells(j, 2).Value < Cells(i, 2).Value Then tempString = Cells(i, 1).Value Cells(i, 1).Value = Cells(j, 1).Value Cells(j, 1).Value = tempString tempInteger = Cells(i, 2).Value Cells(i, 2).Value = Cells(j, 2).Value Cells(j, 2).Value = tempInteger End If Next j
例如:5個項(xiàng)目的權(quán)重,數(shù)值和限制已給定。
Dim limit As Double, weight As Double, value As Double, totalWeight As Double, maximumValue As DoubleDim i, j, k, l, m As IntegerDim weighti, weightj, weightk, weightl, weightm As DoubleDim valuei, valuej, valuek, valuel, valuem As Doublelimit = Range("D6").valuemaximumValue = 0weighti = Range("B2").valueweightj = Range("C2").valueweightk = Range("D2").valueweightl = Range("E2").valueweightm = Range("F2").valuevaluei = Range("B3").valuevaluej = Range("C3").valuevaluek = Range("D3").valuevaluel = Range("E3").valuevaluem = Range("F3").valueFor i = 0 To 1 For j = 0 To 1 For k = 0 To 1 For l = 0 To 1 For m = 0 To 1 weight = weighti * i + weightj * j + weightk * k + weightl * l + weightm * m value = valuei * i + valuej * j + valuek * k + valuel * l + valuem * m If value > maximumValue And weight <= limit Then Range("B4").value = i Range("C4").value = j Range("D4").value = k Range("E4").value = l Range("F4").value = m totalWeight = weight maximumValue = value End If Next m Next l Next k Next jNext iRange("B6").value = totalWeightRange("B8").value = maximumValue
9. 字符串操作
分離字符串
Dim fullname As String, commaposition As Integer, i As Integer
For i = 2 To 7
fullname = Cells(i, 1).Value
commaposition = InStr(fullname, “,”)
Cells(i, 2).Value = Mid(fullname, commaposition + 2)Cells(i, 3).Value = Left(fullname, commaposition - 1)
Next i
10. 日期和時間
MsgBox Now
'新建一個宏,寫這樣一句就得出當(dāng)前時間彈出框
11. 事件
12. 數(shù)組
13. 函數(shù)和子函數(shù)
14. 用戶窗體
15. ActiveX控件
If CheckBox1.Value = True Then Range("B4").Value = 1If CheckBox1.Value = False Then Range("B6").Value = 0
TextBox1.Text = "數(shù)據(jù)錄入成功!"
一個清除文本框信息
TextBox1.Value = ""
將列表框鏈接到單元格A5,設(shè)計(jì)模式右鍵單擊列表框->屬性->LinkedCell填A(yù)5
With Sheet1.ListBox1 .AddItem "Turkey" .AddItem "Tokyo" .AddItem "Paris"End With
清除(我把清除都放在了cls按鈕里,方便)
ListBox1.Clear
效果
If OptionButton1.Value = True Then Range("A3").Value = 233
If OptionButton2.Value = True Then Range("A6").Value = 666
Range("A5").Value = SpinButton1.Value
屬性設(shè)置
16.應(yīng)用對象
Dim myFile As String, rng As Range, cellValue As Variant, i As Integer, j As IntegermyFile = Application.DefaultFilePath & "\sales.csv"Set rng = SelectionOpen myFile For Output As #1For i = 1 To rng.Rows.Count For j = 1 To rng.Columns.Count cellValue = rng.Cells(i, j).Value If j = rng.Columns.Count Then Write #1, cellValue Else Write #1, cellValue, End If Next jNext iClose #1
Dim myFile As String, text As String, textline As String, posLat As Integer, posLong As IntegermyFile = "C:\Users\Randolph\Desktop\testWriteIn.txt"'myFile = Application.GetOpenFilename()Open myFile For Input As #1 Do Until EOF(1) Line Input #1, textline text = text & textline LoopClose #1posLat = InStr(text, "ID")posLong = InStr(text, "密碼")Range("A1").Value = Mid(text, posLat + 4, 4)'后一個數(shù)是找到數(shù)據(jù)的長度,數(shù)據(jù)可以包括空格;前一個數(shù)是拷貝數(shù)據(jù)的起始字符位置,即從這行的第第個字符開始復(fù)制'當(dāng)然,每行第一個是0Range("A2").Value = Mid(text, posLong + 4, 13)'這里實(shí)驗(yàn)發(fā)現(xiàn)一個漢字算一個字母,若第二個數(shù)長于我們所需要的數(shù)據(jù),將把下次出現(xiàn)的其他數(shù)據(jù)錄入進(jìn)來
用到的測試txt文件信息:
Some information here..ID: aPig密碼: isAkindOfHAHASome more information here..
聯(lián)系客服