資源描述:
《excel vba常用代碼集》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在工程資料-天天文庫(kù)。
1、EXCELVBA常用代碼集1.顯示活動(dòng)工作簿名稱MsgBox"當(dāng)前活動(dòng)工作簿是"&ActiveWorkbook.Name2.保存活動(dòng)工作簿Activeworkbook.Save3.保存所有打開的工作簿關(guān)閉EXCELForEachWinApplication.WorkbooksW.SaveNextWApplication.Quit4.將網(wǎng)格線設(shè)置為藍(lán)色ActiveWindow.GridlineColorIndex=55.將工作表sheet1隱藏Sheet1.Visible=xlSheetVeryHidden6.將工作表Shtte1顯示Sheet1.Visible=xlS
2、heetVisible7.單擊某單元格,該單元格所在的行以藍(lán)色背景填充,字體顏色為白色PrivateSubWorksheet_SelectionChange(ByValTargetAsExcel.Range)IfTarget.Row>=2Then’第二行以下的區(qū)域OnErrorResumeNext[ChangColor_With1].FormatConditions.DeleteTarget.EntireRow.Name="ChangColor_With1"With[ChangColor_With1].FormatConditions.Delete.AddxlExpr
3、ession,,"TRUE".Item(1).Interior.ColorIndex=5.Item(1).Font.ColorIndex=2EndWithEndIfEndSub8.使窗體在啟動(dòng)的時(shí)候自動(dòng)最大化PrivateSubUserForm_Initialize()Application.WindowState=xlMaximizedWithApplicationMe.Top=.TopMe.Left=.LeftMe.Height=.HeightMe.Width=.WidthEndWithEndSub9.不保存工作簿退出EXCELApplication.Displa
4、yAlerts=FalseApplication.Quitbeconsistentwithinthesamedisk.Alternateunifiedcorerequirementsplacedontheterminalstripterminals,onlineidentityandensurethecoppercoreisnotexposed.6.4.6enclosurewithinthesametothecablecoreprovidesbindingintoacircle,harnesstiespacingisgenerally100mm;branchoffic
5、esshallbebindingonbothends,eachcore(第5頁(yè))10.使窗體的關(guān)閉按紐不好用PrivateSubUserForm_QueryClose(CancelAsInteger,CloseModeAsInteger)IfCloseMode=vbformcontrdmenuThenMsgBox"請(qǐng)用關(guān)閉按鈕關(guān)閉窗口!!",64,"提示"Cancel=TrueEndIfEndSub11.使窗體在3秒后自動(dòng)關(guān)閉PrivateSubUserForm_Activate()Application.WaitNow+TimeValue("00:00:03")Us
6、erForm1.HideEndSub12.啟動(dòng)窗體的時(shí)候自動(dòng)使Label1顯示Sheet1工作表3列,8行的內(nèi)容PrivateSubUserForm_Activate()Label1.Caption=Sheets("sheet1").Cells(3,8)EndSub13.讓按紐CommandButton1在窗體上以不可用狀態(tài)顯示CommandButton1.Enabled=False14.讓按紐Commandbutton1在窗體上以隱藏方式存在CommandButton10.Visible=False15.點(diǎn)擊Commandbutton1按紐進(jìn)入”工資”工作表Shee
7、ts("工資").Select16.在Textbox1中輸入數(shù)據(jù),窗體可顯示出”工資”工作表中與輸入內(nèi)容關(guān)聯(lián)的項(xiàng)PrivateSubTextBox1_Change()ForX=1ToApplication.CountA(Sheets("工資").Range("a:a"))IfSheets("工資").Cells(X,1)=TextBox1.TextThen’在工資表第一列查找與Textbox1輸入相符的項(xiàng)Label2.Caption=Sheets("工資").Cells(X,2)’在Label2中顯示Textbox1數(shù)據(jù)所在的第二列的數(shù)據(jù)Label7