VBA常用代码-EXCEL.doc_第1页
VBA常用代码-EXCEL.doc_第2页
VBA常用代码-EXCEL.doc_第3页
VBA常用代码-EXCEL.doc_第4页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

EXCEL VBA常用代码1.显示活动工作簿名称MsgBox 当前活动工作簿是 & ActiveWorkbook.Name2.保存活动工作簿Activeworkbook.Save3.保存所有打开的工作簿关闭EXCELFor Each W in Application.WorkbooksW.SaveNext WApplication.Quit4.将网格线设置为蓝色ActiveWindow.GridlineColorIndex = 55.将工作表sheet1隐藏Sheet1.Visible = xlSheetVeryHidden6.将工作表Shtte1显示Sheet1.Visible = xlSheetVisible7.单击某单元格,该单元格所在的行以蓝色背景填充,字体颜色为白色Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range) If Target.Row = 2 Then第二行以下的区域 On Error Resume Next ChangColor_With1.FormatConditions.Delete Target.EntireRow.Name = ChangColor_With1 With ChangColor_With1.FormatConditions .Delete .Add xlExpression, , TRUE .Item(1).Interior.ColorIndex = 5 .Item(1).Font.ColorIndex = 2 End With End IfEnd Sub8.使窗体在启动的时候自动最大化Private Sub UserForm_Initialize() Application.WindowState = xlMaximized With Application Me.Top = .Top Me.Left = .Left Me.Height = .Height Me.Width = .Width End WithEnd Sub9.不保存工作簿退出EXCELApplication.DisplayAlerts = FalseApplication.Quit10.使窗体的关闭按纽不好用Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)If CloseMode = vbformcontrdmenu ThenMsgBox 请用关闭按钮关闭窗口!, 64, 提示Cancel = TrueEnd IfEnd Sub11.使窗体在3秒后自动关闭Private Sub UserForm_Activate()Application.Wait Now + TimeValue(00:00:03)UserForm1.HideEnd Sub12.启动窗体的时候自动使Label1显示Sheet1工作表3列,8行的内容Private Sub UserForm_Activate()Label1.Caption = Sheets(sheet1).Cells(3, 8)End Sub13.让按纽CommandButton1在窗体上以不可用状态显示CommandButton1.Enabled = False14.让按纽Commandbutton1在窗体上以隐藏方式存在CommandButton10.Visible = False15.点击Commandbutton1按纽进入”工资”工作表Sheets(工资).Select16.在Textbox1中输入数据,窗体可显示出”工资”工作表中与输入内容关联的项Private Sub TextBox1_Change() For X = 1 To Application.CountA(Sheets(工资).Range(a:a)If Sheets(工资).Cells(X, 1) = TextBox1.Text Then在工资表第一列查找与Textbox1输入相符的项 Label2.Caption = Sheets(工资).Cells(X, 2)在Label2中显示Textbox1数据所在的第二列的数据 Label7.Caption = Sheets(工资).Cells(X, 3) 在Label2中显示Textbox1数据所在的第三列的数据 End If NextEnd Sub17.使EXCEL启动的时候自动最小化/最大化Private Sub Workbook_Open()Application.WindowState = xlMinimized最小化Application.WindowState = xlMaximized最大化End Sub18.在Label25以数字的形式显示TextBox12Label14的结果Label25.Caption = Val(TextBox12.Text) * Val(Label14.Caption)19.单选按纽名与Sheet6工作表名相同OptionButton6.Caption = Sheet6.Name20.”登陆”窗体的显示,隐藏登陆.Show显示登陆.Hide隐藏21.使窗体的标题栏不显示(1)插入类模块” CFormChanger” 代码如下:Private Declare Function FindWindow Lib user32 Alias FindWindowA (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPrivate Declare Function GetWindowLong Lib user32 Alias GetWindowLongA (ByVal hWnd As Long, ByVal nIndex As Long) As LongPrivate Declare Function SetWindowLong Lib user32 Alias SetWindowLongA (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As LongPrivate Declare Function DrawMenuBar Lib user32 (ByVal hWnd As Long) As LongPrivate Const GWL_STYLE As Long = (-16)Private Const WS_CAPTION As Long = &HC00000Dim hWndForm As Long.Public Property Set Form(oForm As Object) 29 If Val(Application.Version) = 2 Then第二行以下的所有列 On Error Resume Next ChangColor_With2.FormatConditions.Delete ChangColor_With3.FormatConditions.Delete Target.EntireRow.Name = ChangColor_With2 Target.EntireColumn.Name = ChangColor_With3 With ChangColor_With2.FormatConditions .Delete .Add xlExpression, , TRUE .Item(1).Interior.ColorIndex = 5 End With With ChangColor_With3.FormatConditions .Delete .Add xlExpression, , TRUE .Item(1).Interior.ColorIndex = 5 End With End IfEnd Sub23.显示动态时间(1)插入窗体Userform1及Label1并在窗体声明中插入Option ExplicitPublic nextRun As Date(2)在窗体Activate事件中插入Showtime(3)在窗体QueryClose事件中插入Application.OnTime nextRun, showtime, schedule:=False(4)插入模块Module1并输入 Option ExplicitSub showtime()UserForm1.Label1 = NowUserForm1.RepaintDoEventsUserForm1.nextRun = Now + 1 / 86400Applica

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论