1 有內(nèi)容的最行一行,、列 lr = Range('A' & Cells.Rows.Count).End(xlUp).Row + 1lc = Range(Cells(1, Columns.Count), Cells(1, Columns.Count)).End(xlToLeft).Column + 1 2 數(shù)組填充
3 字符串處理函數(shù) s1 = Len(s) 求長(zhǎng)度s1 = Trim(s) 去兩邊空格s1 = Replace(s,a,b) 替換字符串s1 = LCase(s) 小寫字符串s1 = UCase(s) 大寫字符串s1 = Left(s,n) 從左邊取出n個(gè)字符s1 = Right(s,n) 從右邊取出n個(gè)字符s1 = Mid(s,i,n) 從s的第i個(gè)字符開始取出n個(gè)字符s1 = Instr(s,a) 查找字符串a(chǎn)的位置s1 = Instr(i,s,a) 從第i個(gè)字符開始尋找a,,返回a首字母的位置 4 單元格操作
5 引用方式A1和R1C1轉(zhuǎn)換 'A1轉(zhuǎn)R1C1:function TransferFromat(byval rangeAdd as string) as string dim str as string str =Application.ConvertFormula(rangeAdd , xlA1, xlR1C1) TransferFromat=str end function'R1C1轉(zhuǎn)A1:function TransferFromat(byval rangeAdd as string) as string dim str as string str =Application.ConvertFormula(rangeAdd ,xlR1C1, xlA1 ) TransferFromat=str end functionApplication.ReferenceStyle = xlA1Application.ReferenceStyle = xlR1C1 6 清除密碼保護(hù)
7 空表判斷 If Application.WorksheetFunction.CountA(Cells) <> 0 Then MsgBox '活動(dòng)工作表中包含數(shù)據(jù),,請(qǐng)選擇一個(gè)空工作表,!' Exit Sub End If 8 定時(shí)運(yùn)行程序
9 Read a file Const ForReading = 1Const ForWriting = 2Const ForAppending = 8Sub ReadTextFileExample() Dim fso As Object Set fso = CreateObject('Scripting.FileSystemObject') Dim sourceFile As Object Dim myFilePath As String Dim myFileText As String myFilePath = 'C:\mypath\to\myfile.txt' GoalKicker.com – VBA Notes for Professionals 96 Set sourceFile = fso.OpenTextFile(myFilePath, ForReading) myFileText = sourceFile.ReadAll ' myFileText now contains the content of the text file sourceFile.Close ' close the file ' do whatever you might need to do with the text ' You can also read it line by line Dim line As String Set sourceFile = fso.OpenTextFile(myFilePath, ForReading) While Not sourceFile.AtEndOfStream ' while we are not finished reading through the file line = sourceFile.ReadLine ' do something with the line... Wend sourceFile.CloseEnd Sub 10 Creating and write a text file
11 設(shè)置條件格式 Sub 設(shè)置條件格式() Dim rng1 As Range Set rng1 = Sheet1.Range('C2:E6') '添加條件格式,,成績(jī)大于或等于90 的格式 With rng1.FormatConditions.Add(Type:=xlCellValue, _ Operator:=xlGreaterEqual, Formula1:=90) With .Borders .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = 6 End With With .Font .Bold = True 第4 章 Range 對(duì)象操作技巧 105 .ColorIndex = 3 End With End With '添加條件格式,,成績(jī)小于60 的格式 With rng1.FormatConditions.Add(Type:=xlCellValue, _ Operator:=xlLess, Formula1:=60) With .Font .Bold = True .ColorIndex = 10 End With End WithEnd Sub 12 清除條件格式
13 排序工作表 Sub 排序工作表() Dim i As Long, j As Long For i = 1 To Worksheets.Count For j = 1 To Worksheets.Count - 1 If UCase$(Worksheets(j).Name) > UCase$(Worksheets(j + 1).Name) Then Worksheets(j).Move After:=Worksheets(j + 1) End If Next j Next iEnd Sub 14 重命名工作表
15 工作表標(biāo)簽顏色設(shè)置與恢復(fù) Sub 設(shè)置工作表標(biāo)簽顏色() For Each sh In Worksheets r = Rnd() * 255 g = Rnd() * 255 b = Rnd() * 255 sh.Tab.Color = RGB(r, g, b) NextEnd SubSub 恢復(fù)工作表標(biāo)簽顏色() For Each sht In Worksheets sht.Tab.ColorIndex = xlColorIndexNone NextEnd Sub 16 判斷工作簿是否打開
17 工作簿備份: Sub 備份工作簿() Dim wb As Workbook, FileName As String, i As Integer, OK As Boolean Set wb = ActiveWorkbook '獲取對(duì)當(dāng)前工作簿的引用 If wb.Path = '' Then '如果還未保存 Application.Dialogs(xlDialogSaveAs).Show '顯示另存為對(duì)話框 End If FileName = wb.FullName '獲取工作簿的全路徑名稱 i = InStrRev(FileName, '.') If i > 0 Then FileName = Left(FileName, i - 1) '生成擴(kuò)展名'.bak' FileName = FileName & '.bak' OK = False On Error GoTo err1 With wb Application.StatusBar = '正在保存工作簿...' .Save '保存工作簿 Application.StatusBar = '正在備份工作簿...' .SaveCopyAs FileName '備份工作簿 OK = True End Witherr1: Set wb = Nothing Application.StatusBar = False '恢復(fù)狀態(tài)欄 If Not OK Then '如果未備份成功 MsgBox '備份工作簿操作失敗!', vbExclamation, ThisWorkbook.Name End If End Sub 18 工作簿之間數(shù)據(jù)引用:
19 鎖定和隱藏公式 Sub 鎖定和隱藏公式() If ActiveSheet.ProtectContents = True Then MsgBox '工作表已保護(hù),!' Exit Sub End If Worksheets('Sheet1').Range('A1').CurrentRegion.Select Selection.Locked = False Selection.FormulaHidden = False Selection.SpecialCells(xlCellTypeFormulas).Select Selection.Locked = True Selection.FormulaHidden = True Worksheets('Sheet1').Protect DrawingObjects:=True, Contents:=True, Scenarios:=True Worksheets('Sheet1').EnableSelection = xlNoRestrictionsEnd SubSub 取消保護(hù)() ActiveSheet.Unprotect Worksheets('Sheet1').Range('A1').CurrentRegion.Select Selection.Locked = False Selection.FormulaHidden = FalseEnd Sub 20 整點(diǎn)報(bào)時(shí)
ref: 吳永佩,,成麗君 《征服Excel VBA:讓你工作效率倍增的239 個(gè)實(shí)用技巧 》 -End- |
|